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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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