update role

This commit is contained in:
Hoang 2021-11-12 12:16:38 +07:00
parent 1e6d2e6b2f
commit 3a20ec8cd7
8 changed files with 41 additions and 16 deletions

View File

@ -12,6 +12,7 @@ type DBRole struct {
Name string `bson:"name"`
Code string `bson:"code"`
IsAdmin bool `bson:"isAdmin"`
Level int `bson:"level"`
CreatedAt time.Time `bson:"createdAt"`
UpdatedAt time.Time `bson:"updatedAt"`
}
@ -26,7 +27,7 @@ type DBUser struct {
HashedPassword string `bson:"hashedPassword"`
Status string `bson:"status"`
RoleID primitive.ObjectID `bson:"roleId"`
Other string `bson:"other"`
Other interface{} `bson:"other"`
CreatedAt time.Time `bson:"createdAt"`
UpdatedAt time.Time `bson:"updatedAt"`
}

View File

@ -10,6 +10,7 @@ import (
// PermissionCreateOptions ...
type PermissionCreateOptions struct {
Name string
Code string
RoleID string
Desc string
}
@ -17,6 +18,7 @@ type PermissionCreateOptions struct {
// PermissionUpdateOptions ...
type PermissionUpdateOptions struct {
Name string
Code string
RoleID string
Desc string
}
@ -37,6 +39,14 @@ func (co PermissionCreateOptions) Validate() error {
return errors.New("no name data")
}
// Code
if co.Code == "" {
logger.Error("usermngmt - Permission - Create: no code data", logger.LogData{
"payload": co,
})
return errors.New("no code data")
}
// RoleID
if co.RoleID == "" {
logger.Error("usermngmt - Permission - Create: no roleID data", logger.LogData{
@ -68,6 +78,14 @@ func (co PermissionUpdateOptions) Validate() error {
return errors.New("no name data")
}
// Code
if co.Code == "" {
logger.Error("usermngmt - Permission - Update: no code data", logger.LogData{
"payload": co,
})
return errors.New("no code data")
}
// RoleID
if co.RoleID == "" {
logger.Error("usermngmt - Permission - Update: no roleID data", logger.LogData{

View File

@ -9,11 +9,13 @@ import (
// RoleCreateOptions ...
type RoleCreateOptions struct {
Name string
Level int
}
// RoleUpdateOptions ...
type RoleUpdateOptions struct {
Name string
Level int
}
// RoleAllQuery ...

View File

@ -16,6 +16,7 @@ type Role struct {
ID string `json:"_id"`
Name string `json:"name"`
Code string `json:"code"`
Level int `json:"level"`
IsAdmin bool `json:"isAdmin"`
CreatedAt time.Time `json:"createdAt"`
UpdatedAt time.Time `json:"updatedAt"`

View File

@ -14,7 +14,7 @@ type UserCreateOptions struct {
Password string
Status string
RoleID string
Other string
Other interface{}
}
// UserUpdateOptions ...
@ -23,7 +23,7 @@ type UserUpdateOptions struct {
Phone string
Email string
RoleID string
Other string
Other interface{}
}
// ChangePasswordOptions ...

View File

@ -12,7 +12,7 @@ type User struct {
Email string `json:"email"`
Status string `json:"status"`
Role RoleShort `json:"role"`
Other string `json:"other"`
Other interface{} `json:"other"`
CreatedAt time.Time `json:"createdAt"`
UpdatedAt time.Time `json:"updatedAt"`
}

View File

@ -40,7 +40,7 @@ func newPermission(payload model.PermissionCreateOptions) model.DBPermission {
return model.DBPermission{
ID: mongodb.NewObjectID(),
Name: payload.Name,
Code: internal.GenerateCode(payload.Name),
Code: payload.Code,
RoleID: roleID,
Desc: payload.Desc,
CreatedAt: timeNow,
@ -75,7 +75,7 @@ func Update(permissionID string, payload model.PermissionUpdateOptions) error {
updateData := bson.M{
"$set": bson.M{
"name": payload.Name,
"code": internal.GenerateCode(payload.Name),
"code": payload.Code,
"roleId": roleID,
"desc": payload.Desc,
"updatedAt": internal.Now(),

View File

@ -40,6 +40,7 @@ func newRole(payload model.RoleCreateOptions) model.DBRole {
ID: mongodb.NewObjectID(),
Name: payload.Name,
Code: internal.GenerateCode(payload.Name),
Level: payload.Level,
CreatedAt: timeNow,
UpdatedAt: timeNow,
}
@ -72,6 +73,7 @@ func Update(roleID string, payload model.RoleUpdateOptions) error {
"$set": bson.M{
"name": payload.Name,
"code": internal.GenerateCode(payload.Name),
"level": payload.Level,
"updatedAt": internal.Now(),
},
}
@ -125,6 +127,7 @@ func getResponseList(roles []model.DBRole) []model.Role {
ID: role.ID.Hex(),
Name: role.Name,
Code: role.Code,
Level: role.Level,
CreatedAt: role.CreatedAt,
UpdatedAt: role.UpdatedAt,
})