Skip to content

Commit

Permalink
Fixed model configuration issues
Browse files Browse the repository at this point in the history
  • Loading branch information
Stefan923 committed Jan 23, 2024
1 parent a8e6fed commit dd9c92b
Show file tree
Hide file tree
Showing 8 changed files with 25 additions and 21 deletions.
8 changes: 4 additions & 4 deletions backend/src/api/response/base_response.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ import (
)

type BaseHttpResponse struct {
Result any `json:"result"`
Success bool `json:"success"`
ValidationErrors *[]validator.ValidationError
Error any `json:"error"`
Result any `json:"result"`
Success bool `json:"success"`
ValidationErrors *[]validator.ValidationError `json:"validationErrors"`
Error any `json:"error"`
}

func GenerateResponse(result any, success bool) *BaseHttpResponse {
Expand Down
2 changes: 1 addition & 1 deletion backend/src/data/database/migration/1_init.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func createRolesAndAdminUser(database *gorm.DB) {
adminRole := model2.Role{Name: "ADMIN"}

createRoleIfNotExists(database, &adminRole)
createRoleIfNotExists(database, &model2.Role{Name: "USER"})
createRoleIfNotExists(database, &model2.Role{Name: "USER", DefaultRole: true})
createAdminUserInformation(database, adminRole)
}

Expand Down
6 changes: 3 additions & 3 deletions backend/src/data/model/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ type UserAccount struct {

type Role struct {
BaseModel
Name string `gorm:"type:string; size:10; not null; unique;"`
Default bool `gorm:"type:bool; default:false;"`
UserRoles *[]UserRole
Name string `gorm:"type:string; size:10; not null; unique;"`
DefaultRole bool `gorm:"type:bool; default:false;"`
UserRoles *[]UserRole
}

type UserRole struct {
Expand Down
13 changes: 7 additions & 6 deletions backend/src/data/repository/base_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,19 @@ import (

const userIdFiledName = "UserId"

type preload struct {
string
type PreloadSetting struct {
EntityName string
}

type BaseRepository[T any] struct {
Database *gorm.DB
Preloads []preload
Preloads []PreloadSetting
}

func NewBaseRepository[T any]() *BaseRepository[T] {
func NewBaseRepository[T any](preloads []PreloadSetting) *BaseRepository[T] {
return &BaseRepository[T]{
Database: db.GetDatabase(),
Preloads: preloads,
}
}

Expand Down Expand Up @@ -106,9 +107,9 @@ func (repository *BaseRepository[T]) Delete(context context.Context, id uint) er
return nil
}

func Preload(database *gorm.DB, preloads []preload) *gorm.DB {
func Preload(database *gorm.DB, preloads []PreloadSetting) *gorm.DB {
for _, preload := range preloads {
database = database.Preload(preload.string)
database = database.Preload(preload.EntityName)
}
return database
}
4 changes: 2 additions & 2 deletions backend/src/data/repository/role_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ func NewRoleRepository() *RoleRepository {
return &RoleRepository{
BaseRepository: BaseRepository[model.Role]{
Database: database.GetDatabase(),
Preloads: []preload{},
Preloads: []PreloadSetting{},
},
}
}
Expand All @@ -22,7 +22,7 @@ func (repository *RoleRepository) FindDefault() (*model.Role, error) {
role := new(model.Role)

err := repository.Database.
Where("default = ? and deleted_at is null", true).
Where("default_role = ? and deleted_at is null", true).
First(role).
Error
if err != nil {
Expand Down
3 changes: 0 additions & 3 deletions backend/src/data/repository/user_account_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,6 @@ func NewUserAccountRepository() *UserAccountRepository {
return &UserAccountRepository{
BaseRepository: BaseRepository[model.UserAccount]{
Database: db.GetDatabase(),
Preloads: []preload{
{string: "User"},
},
},
}
}
Expand Down
5 changes: 4 additions & 1 deletion backend/src/data/repository/user_role_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@ func NewUserRoleRepository() *UserRoleRepository {
return &UserRoleRepository{
BaseRepository: BaseRepository[model.UserRole]{
Database: database.GetDatabase(),
Preloads: []preload{},
Preloads: []PreloadSetting{
{EntityName: "Role"},
{EntityName: "User"},
},
},
}
}
5 changes: 4 additions & 1 deletion backend/src/service/user_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,12 @@ type UserService struct {
}

func NewUserService() *UserService {
preloads := []repository.PreloadSetting{
{EntityName: "UserRoles"},
}
return &UserService{
BaseService: BaseService[model.User, model.User, model.User, model.User]{
Repository: repository.NewBaseRepository[model.User](),
Repository: repository.NewBaseRepository[model.User](preloads),
},
}
}

0 comments on commit dd9c92b

Please sign in to comment.