feat(role_service): Add menu assignment functionality and related error handling
Deploy Application / deploy (push) Successful in 22s
Details
Deploy Application / deploy (push) Successful in 22s
Details
This commit is contained in:
parent
310abc99d7
commit
1a16cb3396
|
|
@ -48,10 +48,12 @@ var (
|
|||
ErrGetRoleById = errors.New("failed to get role by id")
|
||||
ErrGetRoleByName = errors.New("failed to get role by name")
|
||||
ErrRoleAlreadyExists = errors.New("role already exist")
|
||||
ErrMenuAlreadyExists = errors.New("menu already exist")
|
||||
ErrPermissionAlreadyExists = errors.New("permission already exist")
|
||||
ErrUpdateRole = errors.New("failed to update role")
|
||||
ErrRoleNotFound = errors.New("role not found")
|
||||
ErrDeleteRole = errors.New("failed to delete role")
|
||||
ErrRoleNotFound = errors.New("role not found")
|
||||
ErrMenuNotFound = errors.New("menu not found")
|
||||
ErrPermissionNotFound = errors.New("permission not found")
|
||||
ErrAssignRoleToUser = errors.New("failed to assign role to user")
|
||||
ErrRemoveRoleFromUser = errors.New("failed to remove role from user")
|
||||
|
|
|
|||
|
|
@ -57,6 +57,7 @@ func (r *roleService) GetAll(ctx context.Context) ([]dto.RoleResponse, error) {
|
|||
return responses, nil
|
||||
}
|
||||
|
||||
// AssignMenusToRole implements RoleService.
|
||||
// AssignMenusToRole implements RoleService.
|
||||
func (r *roleService) AssignMenusToRole(ctx context.Context, roleId string, menuIds []string) error {
|
||||
if len(menuIds) == 0 {
|
||||
|
|
@ -75,10 +76,38 @@ func (r *roleService) AssignMenusToRole(ctx context.Context, roleId string, menu
|
|||
return dto.ErrRoleNotFound
|
||||
}
|
||||
|
||||
// Assign menus ke role
|
||||
if err := r.roleRepo.AssignMenusToRole(ctx, tx, roleId, menuIds); err != nil {
|
||||
rows := make([]entities.M_Role_Menu, 0, len(menuIds))
|
||||
roleUUID, err := uuid.Parse(roleId)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
for _, mid := range menuIds {
|
||||
menuUUID, err := uuid.Parse(mid)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
rows = append(rows, entities.M_Role_Menu{
|
||||
RoleID: roleUUID,
|
||||
MenuID: menuUUID,
|
||||
})
|
||||
}
|
||||
if len(rows) == 0 {
|
||||
return dto.ErrMenuNotFound
|
||||
}
|
||||
|
||||
res := tx.Clauses(clause.OnConflict{
|
||||
Columns: []clause.Column{{Name: "role_id"}, {Name: "menu_id"}},
|
||||
DoNothing: true,
|
||||
}).Create(&rows)
|
||||
if res.Error != nil {
|
||||
return res.Error
|
||||
}
|
||||
|
||||
if res.RowsAffected == 0 {
|
||||
return dto.ErrMenuAlreadyExists
|
||||
}
|
||||
|
||||
return nil
|
||||
})
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue