Backend file structure modifications, blogs routes ajustement
This commit is contained in:
110
backend/api/core/schemas.go
Normal file
110
backend/api/core/schemas.go
Normal file
@@ -0,0 +1,110 @@
|
||||
package core
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"time"
|
||||
"fmt"
|
||||
|
||||
"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 User struct {
|
||||
bun.BaseModel `bun:"table:users"`
|
||||
|
||||
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"`
|
||||
Password string `bun:"password,notnull"`
|
||||
Phone string `bun:"phone,notnull"`
|
||||
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 {
|
||||
bun.BaseModel `bun:"table:events"`
|
||||
|
||||
EventID uuid.UUID `bun:"type:uuid,pk"`
|
||||
CreationDate time.Time `bun:"creation_date,notnull,default:current_timestamp"`
|
||||
ScheduleStart time.Time `bun:"schedule_start,notnull"`
|
||||
ScheduleEnd time.Time `bun:"schedule_end,notnull"`
|
||||
Status string `bun:"status,notnull"`
|
||||
}
|
||||
|
||||
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"`
|
||||
Slug string `bun:"slug,unique,notnull"`
|
||||
Content string `bun:"content,notnull"`
|
||||
Label string `bun:"label"`
|
||||
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_id=user_id"`
|
||||
}
|
||||
|
||||
type WebsiteSettings struct {
|
||||
bun.BaseModel `bun:"table:website_settings"`
|
||||
|
||||
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
|
||||
}
|
||||
Reference in New Issue
Block a user