Skip to content

Commit

Permalink
Add User+Rating models
Browse files Browse the repository at this point in the history
  • Loading branch information
federicotdn committed Nov 29, 2024
1 parent da962a2 commit 288df0d
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 0 deletions.
11 changes: 11 additions & 0 deletions pkg/database/migrations/catalog/20230829142500_initialize_db.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ func init() {
&model.Ingredient{},
&model.Dough{},
&model.Tool{},
&model.User{},
}
for _, i := range models {
if _, err := db.NewCreateTable().Model(i).IfNotExists().Exec(ctx); err != nil {
Expand All @@ -29,6 +30,16 @@ func init() {
if err != nil {
return err
}

_, err = db.NewCreateTable().
Model(&model.Rating{}).
ForeignKey(`("user_id") REFERENCES "users" ("id") ON DELETE CASCADE`).
ForeignKey(`("pizza_id") REFERENCES "pizzas" ("id") ON DELETE CASCADE`).
IfNotExists().
Exec(ctx)
if err != nil {
return err
}
_, err = db.NewCreateTable().
Model(&model.PizzaToIngredients{}).
ForeignKey(`("pizza_id") REFERENCES "pizzas" ("id") ON DELETE CASCADE`).
Expand Down
11 changes: 11 additions & 0 deletions pkg/database/migrations/catalog/testdata.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -234,3 +234,14 @@
ingredient_id: 24
- pizza_id: 1
ingredient_id: 18
- model: User
rows:
- id: 1
name: global
token: ""
- model: Rating
rows:
- id: 1
stars: 5
user_id: 1
pizza_id: 1
15 changes: 15 additions & 0 deletions pkg/model/rating.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package model

import (
"github.com/uptrace/bun"
)

type Rating struct {
bun.BaseModel
ID int64 `bun:",pk,autoincrement"`
Stars int `json:"stars" bun:",pk"`
UserID int64 `json:"-"`
User *User `bun:"rel:belongs-to,join:user_id=id"`
PizzaID int64 `json:"-"`
Pizza *Pizza `bun:"rel:belongs-to,join:pizza_id=id"`
}
12 changes: 12 additions & 0 deletions pkg/model/user.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package model

import (
"github.com/uptrace/bun"
)

type User struct {
bun.BaseModel
ID int64 `bun:",pk,autoincrement"`
Name string `json:"name"`
Token string `json:"token"`
}

0 comments on commit 288df0d

Please sign in to comment.