Content and relations

This commit is contained in:
IRHM
2023-03-23 19:25:15 +00:00
parent 84c3abf5bf
commit 7d31b875ae
3 changed files with 15 additions and 7 deletions
+4 -3
View File
@@ -19,9 +19,10 @@ import (
type User struct {
bun.BaseModel `bun:"table:users"`
ID int `bun:"id,pk,autoincrement" json:"id"`
Username string `bun:"username,notnull,unique" json:"username" binding:"required"`
Password string `bun:"password,notnull" json:"password" binding:"required"`
ID int `bun:"id,pk,autoincrement" json:"id"`
Username string `bun:"username,notnull,unique" json:"username" binding:"required"`
Password string `bun:"password,notnull" json:"password" binding:"required"`
Lists []*List `bun:"rel:has-many,join:id=user_id"`
}
type AuthResponse struct {
+10 -4
View File
@@ -8,24 +8,30 @@ import (
)
type Content struct {
bun.BaseModel `bun:"table:content"`
ID int `bun:"id,pk,autoincrement" json:"id"`
Type string `json:"-"`
Name string `json:"name"`
}
type List struct {
bun.BaseModel `bun:"table:lists"`
ID int `bun:"id,pk,autoincrement" json:"id"`
ImdbID int `bun:"imdb_id" json:"imdbId"`
ID int `bun:"id,pk,autoincrement" json:"id"`
Watched bool `bun:"watched" json:"watched"`
UserID int `bun:"user_id" json:"-"`
ContentID int
Content *Content `bun:"rel:belongs-to,join:content_id=id" json:"content"`
}
func getContent(db *bun.DB) List {
ctx := context.TODO()
list := new(List)
err := db.NewSelect().Model(list).Where("id = ?", 1).Scan(ctx)
err := db.NewSelect().Model(list).Relation("Content").Where("user_id = ?", 8).Scan(ctx)
if err != nil {
panic(err)
}
fmt.Println(list.ID)
fmt.Println(list.ImdbID)
return *list
}
+1
View File
@@ -34,6 +34,7 @@ func main() {
// Create tables if they don't exist
db.NewCreateTable().Model((*User)(nil)).IfNotExists().Exec(context.TODO())
db.NewCreateTable().Model((*Content)(nil)).IfNotExists().Exec(context.TODO())
db.NewCreateTable().Model((*List)(nil)).IfNotExists().Exec(context.TODO())
gin := gin.Default()