Fixed schemas

This commit is contained in:
cdricms
2025-01-15 09:29:18 +01:00
parent 354f8cc02e
commit d18245736f
2 changed files with 40 additions and 22 deletions

View File

@@ -10,11 +10,8 @@ import (
"context" "context"
"database/sql"
_ "github.com/lib/pq" _ "github.com/lib/pq"
"github.com/uptrace/bun" "github.com/uptrace/bun"
"github.com/uptrace/bun/dialect/pgdialect"
"github.com/uptrace/bun/driver/pgdriver"
) )
func handler(w http.ResponseWriter, r *http.Request) { func handler(w http.ResponseWriter, r *http.Request) {
@@ -32,7 +29,7 @@ type DSN struct {
} }
func (dsn *DSN) ToString() string { 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) { func handlerCreateUser(w http.ResponseWriter, r *http.Request) {
@@ -69,13 +66,15 @@ func main() {
dsn := DSN{ dsn := DSN{
Hostname: "localhost", Hostname: "localhost",
Port: port, Port: os.Getenv("POSTGRES_PORT"),
DBName: os.Getenv("POSTGRES_DB"), DBName: os.Getenv("POSTGRES_DB"),
User: os.Getenv("POSTGRES_USER"), User: os.Getenv("POSTGRES_USER"),
Password: os.Getenv("POSTGRES_PASSWORD"), Password: os.Getenv("POSTGRES_PASSWORD"),
} }
sqldb := sql.OpenDB(pgdriver.NewConnector(pgdriver.WithDSN(dsn.ToString()))) DB, err = InitDatabase(dsn)
DB = bun.NewDB(sqldb, pgdialect.New()) if err != nil {
log.Fatal(err)
}
http.HandleFunc("/", handler) http.HandleFunc("/", handler)

View File

@@ -1,9 +1,14 @@
package main package main
import ( import (
"context"
"database/sql"
"time"
"github.com/google/uuid" "github.com/google/uuid"
"github.com/uptrace/bun" "github.com/uptrace/bun"
"time" "github.com/uptrace/bun/dialect/pgdialect"
"github.com/uptrace/bun/driver/pgdriver"
) )
type Role string type Role string
@@ -16,7 +21,7 @@ const (
type User struct { type User struct {
bun.BaseModel `bun:"table:users"` 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"` FirstName string `bun:"firstname,notnull"`
LastName string `bun:"lastname,notnull"` LastName string `bun:"lastname,notnull"`
Email string `bun:"email,unique,notnull"` Email string `bun:"email,unique,notnull"`
@@ -24,6 +29,8 @@ type User struct {
Role Role `bun:"role,notnull,default:'user'"` Role Role `bun:"role,notnull,default:'user'"`
CreatedAt time.Time `bun:"created_at,default:current_timestamp"` CreatedAt time.Time `bun:"created_at,default:current_timestamp"`
UpdatedAt time.Time `bun:"updated_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 { type Event struct {
@@ -36,35 +43,30 @@ type Event struct {
Status string `bun:"status,notnull"` Status string `bun:"status,notnull"`
} }
type EventsToUsers struct { type EventToUser struct {
bun.BaseModel `bun:"table:events_to_users"` bun.BaseModel `bun:"table:events_to_users"`
EventID uuid.UUID `bun:"type:uuid,notnull"` EventID uuid.UUID `bun:"type:uuid,pk"`
UserID uuid.UUID `bun:"type:uuid,notnull"` UserID uuid.UUID `bun:"type:uuid,pk"`
Event *Event `bun:"rel:belongs_to,join:event_id=event_id"` Event *Event `bun:"rel:belongs-to,join:event_id=event_id"`
User *User `bun:"rel:belongs_to,join:user_id=user_id"` User *User `bun:"rel:belongs-to,join:user_id=user_id"`
PrimaryKey struct {
EventID uuid.UUID `bun:"pk"`
UserID uuid.UUID `bun:"pk"`
}
} }
type Blog struct { type Blog struct {
bun.BaseModel `bun:"table:blogs"` 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"` Slug string `bun:"slug,unique,notnull"`
Content string `bun:"content,notnull"` Content string `bun:"content,notnull"`
Label string `bun:"label"` 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"` Published time.Time `bun:"published,default:current_timestamp"`
Summary string `bun:"summary"` Summary string `bun:"summary"`
Image string `bun:"image"` Image string `bun:"image"`
Href string `bun:"href"` 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 { type WebsiteSettings struct {
@@ -73,3 +75,20 @@ type WebsiteSettings struct {
ID uuid.UUID `bun:"type:uuid,pk,default:gen_random_uuid()"` ID uuid.UUID `bun:"type:uuid,pk,default:gen_random_uuid()"`
AutoAcceptDemand bool `bun:"auto_accept_demand,default:false"` 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
}