Skip to content

Commit

Permalink
refactoring models dir position
Browse files Browse the repository at this point in the history
  • Loading branch information
juunini committed Oct 25, 2023
1 parent d12af53 commit 9437100
Show file tree
Hide file tree
Showing 10 changed files with 25 additions and 23 deletions.
12 changes: 0 additions & 12 deletions db/migrate.go

This file was deleted.

2 changes: 1 addition & 1 deletion generate/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"os"
"sample/constants"
"sample/db"
"sample/db/models"
"sample/models"

signature_header "github.com/cloudmatelabs/go-activitypub-signature-header"
)
Expand Down
3 changes: 2 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"sample/constants"
"sample/db"
"sample/models"
"sample/router"

"github.com/gofiber/fiber/v2"
Expand All @@ -12,7 +13,7 @@ func main() {
constants.LoadEnv()
db.Connect()
if constants.DB_MIGRATE == "true" {
db.Migrate()
models.Migrate()
}

app := fiber.New()
Expand Down
12 changes: 12 additions & 0 deletions models/migrate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package models

import "sample/db"

func Migrate() {
db.DB.AutoMigrate(
&User{},
&UserProfile{},
&UserKeyPair{},
&UserInbox{},
)
}
7 changes: 7 additions & 0 deletions db/models/user.go → models/user.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package models

import (
"sample/db"
"time"

"gorm.io/gorm"
Expand All @@ -14,3 +15,9 @@ type User struct {
CreatedAt time.Time
DeletedAt gorm.DeletedAt
}

func (u User) Exists() bool {
var count int64
db.DB.Model(&User{}).Where("id = ?", u.ID).Count(&count)
return count > 0
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
10 changes: 2 additions & 8 deletions router/user/inbox/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"fmt"
"net/http"
"sample/db"
"sample/db/models"
"sample/models"
"strings"

jsonld_helper "github.com/cloudmatelabs/go-jsonld-helper"
Expand All @@ -20,7 +20,7 @@ import (
func Route(c *fiber.Ctx) error {
id := c.Params("id")

if !userExists(id) {
if !(models.User{ID: id}.Exists()) {
return c.SendStatus(fiber.StatusNotFound)
}

Expand Down Expand Up @@ -61,12 +61,6 @@ func Route(c *fiber.Ctx) error {
return c.SendStatus(fiber.StatusAccepted)
}

func userExists(id string) bool {
var count int64
db.DB.Model(&models.User{}).Where("id = ?", id).Count(&count)
return count > 0
}

func useContextCache(body []byte) (origin []byte, cached []byte) {
origin = body
cached = bytes.Replace(
Expand Down
2 changes: 1 addition & 1 deletion router/user/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"fmt"
"sample/constants"
"sample/db"
"sample/db/models"
"sample/models"
"strings"

"github.com/gofiber/fiber/v2"
Expand Down

0 comments on commit 9437100

Please sign in to comment.