package core import ( "context" "database/sql" "fmt" "time" "github.com/google/uuid" "github.com/uptrace/bun" "github.com/uptrace/bun/dialect/pgdialect" "github.com/uptrace/bun/driver/pgdriver" ) var DB *bun.DB type DSN struct { Hostname string Port string DBName string User string Password string } func (dsn *DSN) ToString() string { return fmt.Sprintf("postgres://%s:%s@%s:%s/%s?sslmode=disable", dsn.User, dsn.Password, dsn.Hostname, dsn.Port, dsn.DBName) } type Role string const ( AdminRole Role = "admin" UserRole Role = "user" ) type Status string const ( Active Status = "Active" Inactive Status = "Inactive" ) type User struct { bun.BaseModel `bun:"table:users"` UserID uuid.UUID `bun:"type:uuid,pk,default:gen_random_uuid()" json:"userId"` FirstName string `bun:"firstname,notnull" json:"firstname"` LastName string `bun:"lastname,notnull" json:"lastname"` Email string `bun:"email,unique,notnull" json:"email"` Password string `bun:"password,notnull" json:"password"` Phone string `bun:"phone,notnull" json:"phone"` Role Role `bun:"role,notnull,default:'user'" json:"role"` CreatedAt time.Time `bun:"created_at,default:current_timestamp" json:"createdAt"` UpdatedAt time.Time `bun:"updated_at,default:current_timestamp" json:"updatedAt"` Events []Event `bun:"m2m:events_to_users,join:User=Event" json:"events,omitempty"` Articles []*Blog `bun:"rel:has-many,join:user_id=blog_id" json:"articles,omitempty"` } type Event struct { bun.BaseModel `bun:"table:events"` EventID uuid.UUID `bun:"type:uuid,pk,default:gen_random_uuid()" json:"eventID"` CreationDate time.Time `bun:"creation_date,notnull,default:current_timestamp" json:"creationDate"` ScheduleStart time.Time `bun:"schedule_start,notnull" json:"scheduleStart"` ScheduleEnd time.Time `bun:"schedule_end,notnull" json:"scheduleEnd"` Status Status `bun:"status,notnull,default:Inactive" json:"status"` } type EventToUser struct { bun.BaseModel `bun:"table:events_to_users"` 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"` } type Blog struct { bun.BaseModel `bun:"table:blogs"` BlogID uuid.UUID `bun:"type:uuid,pk,default:gen_random_uuid()" json:"blogID"` Slug string `bun:"slug,unique,notnull" json:"slug"` Content string `bun:"content,notnull" json:"content"` Label string `bun:"label" json:"label"` AuthorID uuid.UUID `bun:"author_id,type:uuid,notnull" json:"authorID"` Published time.Time `bun:"published,default:current_timestamp" json:"published"` Summary string `bun:"summary" json:"summary"` Image string `bun:"image" json:"image"` Href string `bun:"href" json:"href"` Author User `bun:"rel:belongs-to,join:author_id=user_id" json:"author"` } type WebsiteSettings struct { bun.BaseModel `bun:"table:website_settings"` ID uuid.UUID `bun:"type:uuid,pk,default:gen_random_uuid()" json:"id"` AutoAcceptDemand bool `bun:"auto_accept_demand,default:false" json:"autoAcceptDemand"` } 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 }