From dd9c92b7acb96793c9bbec5e579c4f2031b31e9d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ovidiu=20=C8=98tefan=20Popescu?= Date: Tue, 23 Jan 2024 18:38:59 +0200 Subject: [PATCH] Fixed model configuration issues --- backend/src/api/response/base_response.go | 8 ++++---- backend/src/data/database/migration/1_init.go | 2 +- backend/src/data/model/user.go | 6 +++--- backend/src/data/repository/base_repository.go | 13 +++++++------ backend/src/data/repository/role_repository.go | 4 ++-- .../src/data/repository/user_account_repository.go | 3 --- backend/src/data/repository/user_role_repository.go | 5 ++++- backend/src/service/user_service.go | 5 ++++- 8 files changed, 25 insertions(+), 21 deletions(-) diff --git a/backend/src/api/response/base_response.go b/backend/src/api/response/base_response.go index f6b79a1..547b78d 100644 --- a/backend/src/api/response/base_response.go +++ b/backend/src/api/response/base_response.go @@ -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 { diff --git a/backend/src/data/database/migration/1_init.go b/backend/src/data/database/migration/1_init.go index aa5c9b9..1e9a4ba 100644 --- a/backend/src/data/database/migration/1_init.go +++ b/backend/src/data/database/migration/1_init.go @@ -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) } diff --git a/backend/src/data/model/user.go b/backend/src/data/model/user.go index 36deddc..69b3ff7 100644 --- a/backend/src/data/model/user.go +++ b/backend/src/data/model/user.go @@ -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 { diff --git a/backend/src/data/repository/base_repository.go b/backend/src/data/repository/base_repository.go index 8857cfd..4ee7930 100644 --- a/backend/src/data/repository/base_repository.go +++ b/backend/src/data/repository/base_repository.go @@ -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, } } @@ -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 } diff --git a/backend/src/data/repository/role_repository.go b/backend/src/data/repository/role_repository.go index d878bad..e716e9a 100644 --- a/backend/src/data/repository/role_repository.go +++ b/backend/src/data/repository/role_repository.go @@ -13,7 +13,7 @@ func NewRoleRepository() *RoleRepository { return &RoleRepository{ BaseRepository: BaseRepository[model.Role]{ Database: database.GetDatabase(), - Preloads: []preload{}, + Preloads: []PreloadSetting{}, }, } } @@ -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 { diff --git a/backend/src/data/repository/user_account_repository.go b/backend/src/data/repository/user_account_repository.go index f2e16d2..1cc0991 100644 --- a/backend/src/data/repository/user_account_repository.go +++ b/backend/src/data/repository/user_account_repository.go @@ -13,9 +13,6 @@ func NewUserAccountRepository() *UserAccountRepository { return &UserAccountRepository{ BaseRepository: BaseRepository[model.UserAccount]{ Database: db.GetDatabase(), - Preloads: []preload{ - {string: "User"}, - }, }, } } diff --git a/backend/src/data/repository/user_role_repository.go b/backend/src/data/repository/user_role_repository.go index 1b1a546..6955ad6 100644 --- a/backend/src/data/repository/user_role_repository.go +++ b/backend/src/data/repository/user_role_repository.go @@ -13,7 +13,10 @@ func NewUserRoleRepository() *UserRoleRepository { return &UserRoleRepository{ BaseRepository: BaseRepository[model.UserRole]{ Database: database.GetDatabase(), - Preloads: []preload{}, + Preloads: []PreloadSetting{ + {EntityName: "Role"}, + {EntityName: "User"}, + }, }, } } diff --git a/backend/src/service/user_service.go b/backend/src/service/user_service.go index 8884371..e75dcce 100644 --- a/backend/src/service/user_service.go +++ b/backend/src/service/user_service.go @@ -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), }, } }