Reorganized API + added db migrations
Read the README file for more informations
This commit is contained in:
@@ -27,13 +27,6 @@ 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 (
|
||||
@@ -41,20 +34,35 @@ const (
|
||||
Inactive Status = "Inactive"
|
||||
)
|
||||
|
||||
type Group string
|
||||
|
||||
const (
|
||||
LatosaGroup Group = "latosa"
|
||||
WingTsunGroup Group = "wing-tsun"
|
||||
)
|
||||
|
||||
type UserAttributes struct {
|
||||
Groups []Group `json:"groups"`
|
||||
}
|
||||
|
||||
type PermissionConditions struct {
|
||||
Groups *[]Group `json:"groups,omitempty"`
|
||||
}
|
||||
|
||||
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,omitempty"`
|
||||
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"`
|
||||
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,omitempty"`
|
||||
Phone string `bun:"phone,notnull" json:"phone"`
|
||||
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"`
|
||||
Attributes UserAttributes `bun:"attributes,type:jsonb" json:"attributes"`
|
||||
}
|
||||
|
||||
func (u *User) Insert(ctx context.Context) (sql.Result, error) {
|
||||
@@ -66,15 +74,6 @@ func (u *User) Insert(ctx context.Context) (sql.Result, error) {
|
||||
}
|
||||
|
||||
func Verify(ctx context.Context, email, password string) (*User, error) {
|
||||
// var user User
|
||||
// query := `
|
||||
// SELECT *
|
||||
// FROM users
|
||||
// WHERE email = ? AND password = crypt(?, password)
|
||||
// `
|
||||
//
|
||||
// err := DB.NewRaw(query, email, password).Scan(ctx, user)
|
||||
|
||||
var user User
|
||||
count, err := DB.NewSelect().
|
||||
Model(&user).
|
||||
@@ -94,6 +93,40 @@ func Verify(ctx context.Context, email, password string) (*User, error) {
|
||||
return &user, nil
|
||||
}
|
||||
|
||||
type Permission struct {
|
||||
bun.BaseModel `bun:"table:permissions"`
|
||||
ID int `bun:"id,pk,autoincrement" json:"id"`
|
||||
Resource string `bun:"resource,notnull" json:"resource"`
|
||||
Action string `bun:"action,notnull" json:"action"`
|
||||
Conditions PermissionConditions `bun:"conditions,type:jsonb" json:"conditions"`
|
||||
}
|
||||
|
||||
type Role struct {
|
||||
bun.BaseModel `bun:"table:roles"`
|
||||
ID uuid.UUID `bun:"id,pk,type:uuid,default:gen_random_uuid()" json:"id"`
|
||||
Name string `bun:"name,unique,notnull" json:"name"`
|
||||
}
|
||||
|
||||
type PermissionToRole struct {
|
||||
bun.BaseModel `bun:"table:permissions_to_users"`
|
||||
|
||||
PermissionID int `bun:"permission_id,pk"`
|
||||
RoleID uuid.UUID `bun:"type:uuid,pk"`
|
||||
|
||||
Permission *Permission `bun:"rel:belongs-to,join:permission_id=id"`
|
||||
Role *Role `bun:"rel:belongs-to,join:role_id=id"`
|
||||
}
|
||||
|
||||
type UserToRole struct {
|
||||
bun.BaseModel `bun:"table:users_to_roles"`
|
||||
|
||||
UserID uuid.UUID `bun:"user_id,type:uuid,pk"`
|
||||
RoleID uuid.UUID `bun:"type:uuid,pk"`
|
||||
|
||||
User *User `bun:"rel:belongs-to,join:user_id=user_id"`
|
||||
Role *Role `bun:"rel:belongs-to,join:role_id=id"`
|
||||
}
|
||||
|
||||
type Event struct {
|
||||
bun.BaseModel `bun:"table:events"`
|
||||
|
||||
@@ -188,6 +221,8 @@ func InitDatabase(dsn DSN) (*bun.DB, error) {
|
||||
return nil, err
|
||||
}
|
||||
db.RegisterModel((*EventToUser)(nil))
|
||||
db.RegisterModel((*PermissionToRole)(nil))
|
||||
db.RegisterModel((*UserToRole)(nil))
|
||||
_, err = db.NewCreateTable().Model((*User)(nil)).IfNotExists().Exec(ctx)
|
||||
_, err = db.NewCreateTable().Model((*Event)(nil)).IfNotExists().Exec(ctx)
|
||||
_, err = db.NewCreateTable().Model((*EventToUser)(nil)).IfNotExists().Exec(ctx)
|
||||
@@ -195,6 +230,10 @@ func InitDatabase(dsn DSN) (*bun.DB, error) {
|
||||
_, err = db.NewCreateTable().Model((*WebsiteSettings)(nil)).IfNotExists().Exec(ctx)
|
||||
_, err = db.NewCreateTable().Model((*Media)(nil)).IfNotExists().Exec(ctx)
|
||||
_, err = db.NewCreateTable().Model((*Shortcode)(nil)).IfNotExists().Exec(ctx)
|
||||
_, err = db.NewCreateTable().Model((*Role)(nil)).IfNotExists().Exec(ctx)
|
||||
_, err = db.NewCreateTable().Model((*Permission)(nil)).IfNotExists().Exec(ctx)
|
||||
_, err = db.NewCreateTable().Model((*PermissionToRole)(nil)).IfNotExists().Exec(ctx)
|
||||
_, err = db.NewCreateTable().Model((*UserToRole)(nil)).IfNotExists().Exec(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user