From d18245736f496fd236571825edb881aeee11842c Mon Sep 17 00:00:00 2001 From: cdricms <36056008+cdricms@users.noreply.github.com> Date: Wed, 15 Jan 2025 09:29:18 +0100 Subject: [PATCH] Fixed schemas --- backend/main.go | 13 ++++++------ backend/schemas.go | 49 ++++++++++++++++++++++++++++++++-------------- 2 files changed, 40 insertions(+), 22 deletions(-) diff --git a/backend/main.go b/backend/main.go index 0893b44..bb322db 100644 --- a/backend/main.go +++ b/backend/main.go @@ -10,11 +10,8 @@ import ( "context" - "database/sql" _ "github.com/lib/pq" "github.com/uptrace/bun" - "github.com/uptrace/bun/dialect/pgdialect" - "github.com/uptrace/bun/driver/pgdriver" ) func handler(w http.ResponseWriter, r *http.Request) { @@ -32,7 +29,7 @@ type DSN struct { } func (dsn *DSN) ToString() string { - return fmt.Sprintf("posgres://%s:%s@%s:%s/%s?sslmode=disable", dsn.User, dsn.Password, dsn.Hostname, dsn.Port, dsn.DBName) + return fmt.Sprintf("postgres://%s:%s@%s:%s/%s?sslmode=disable", dsn.User, dsn.Password, dsn.Hostname, dsn.Port, dsn.DBName) } func handlerCreateUser(w http.ResponseWriter, r *http.Request) { @@ -69,13 +66,15 @@ func main() { dsn := DSN{ Hostname: "localhost", - Port: port, + Port: os.Getenv("POSTGRES_PORT"), DBName: os.Getenv("POSTGRES_DB"), User: os.Getenv("POSTGRES_USER"), Password: os.Getenv("POSTGRES_PASSWORD"), } - sqldb := sql.OpenDB(pgdriver.NewConnector(pgdriver.WithDSN(dsn.ToString()))) - DB = bun.NewDB(sqldb, pgdialect.New()) + DB, err = InitDatabase(dsn) + if err != nil { + log.Fatal(err) + } http.HandleFunc("/", handler) diff --git a/backend/schemas.go b/backend/schemas.go index b6f618b..5e7ee67 100644 --- a/backend/schemas.go +++ b/backend/schemas.go @@ -1,9 +1,14 @@ package main import ( + "context" + "database/sql" + "time" + "github.com/google/uuid" "github.com/uptrace/bun" - "time" + "github.com/uptrace/bun/dialect/pgdialect" + "github.com/uptrace/bun/driver/pgdriver" ) type Role string @@ -16,7 +21,7 @@ const ( type User struct { bun.BaseModel `bun:"table:users"` - ID uuid.UUID `bun:"type:uuid,pk,default:gen_random_uuid()"` + UserID uuid.UUID `bun:"type:uuid,pk,default:gen_random_uuid()"` FirstName string `bun:"firstname,notnull"` LastName string `bun:"lastname,notnull"` Email string `bun:"email,unique,notnull"` @@ -24,6 +29,8 @@ type User struct { Role Role `bun:"role,notnull,default:'user'"` CreatedAt time.Time `bun:"created_at,default:current_timestamp"` UpdatedAt time.Time `bun:"updated_at,default:current_timestamp"` + Events []Event `bun:"m2m:events_to_users,join:User=Event"` + Articles []*Blog `bun:"rel:has-many,join:user_id=blog_id"` } type Event struct { @@ -36,35 +43,30 @@ type Event struct { Status string `bun:"status,notnull"` } -type EventsToUsers struct { +type EventToUser struct { bun.BaseModel `bun:"table:events_to_users"` - EventID uuid.UUID `bun:"type:uuid,notnull"` - UserID uuid.UUID `bun:"type:uuid,notnull"` + EventID uuid.UUID `bun:"type:uuid,pk"` + UserID uuid.UUID `bun:"type:uuid,pk"` - Event *Event `bun:"rel:belongs_to,join:event_id=event_id"` - User *User `bun:"rel:belongs_to,join:user_id=user_id"` - - PrimaryKey struct { - EventID uuid.UUID `bun:"pk"` - UserID uuid.UUID `bun:"pk"` - } + Event *Event `bun:"rel:belongs-to,join:event_id=event_id"` + User *User `bun:"rel:belongs-to,join:user_id=user_id"` } type Blog struct { bun.BaseModel `bun:"table:blogs"` - UUID uuid.UUID `bun:"type:uuid,pk"` + BlogID uuid.UUID `bun:"type:uuid,pk"` Slug string `bun:"slug,unique,notnull"` Content string `bun:"content,notnull"` Label string `bun:"label"` - AuthorID uuid.UUID `bun:"author,notnull"` + AuthorID uuid.UUID `bun:"author_id,notnull"` Published time.Time `bun:"published,default:current_timestamp"` Summary string `bun:"summary"` Image string `bun:"image"` Href string `bun:"href"` - Author *User `bun:"rel:belongs_to,join:author=author_id"` + Author *User `bun:"rel:belongs-to,join:author_id=user_id"` } type WebsiteSettings struct { @@ -73,3 +75,20 @@ type WebsiteSettings struct { ID uuid.UUID `bun:"type:uuid,pk,default:gen_random_uuid()"` AutoAcceptDemand bool `bun:"auto_accept_demand,default:false"` } + +func InitDatabase(dsn DSN) (*bun.DB, error) { + sqldb := sql.OpenDB(pgdriver.NewConnector(pgdriver.WithDSN(dsn.ToString()))) + db := bun.NewDB(sqldb, pgdialect.New()) + + db.RegisterModel((*EventToUser)(nil)) + _, err := db.NewCreateTable().Model((*User)(nil)).IfNotExists().Exec(context.Background()) + _, err = db.NewCreateTable().Model((*Event)(nil)).IfNotExists().Exec(context.Background()) + _, err = db.NewCreateTable().Model((*EventToUser)(nil)).IfNotExists().Exec(context.Background()) + _, err = db.NewCreateTable().Model((*Blog)(nil)).IfNotExists().Exec(context.Background()) + _, err = db.NewCreateTable().Model((*WebsiteSettings)(nil)).IfNotExists().Exec(context.Background()) + if err != nil { + return nil, err + } + + return db, nil +}