update User

This commit is contained in:
Hoang 2021-11-17 13:32:29 +07:00
parent 72f30fce59
commit 2a5e05c3ad
4 changed files with 122 additions and 29 deletions

View File

@ -54,6 +54,16 @@ func (s Service) HasPermission(userID, permission string) bool {
return user.HasPermission(userID, permission)
}
// UpdateUserAvatar ...
func (s Service) UpdateUserAvatar(userID, avatar string) error {
return user.UpdateAvatar(userID, avatar)
}
// DeleteUser ...
func (s Service) DeleteUser(userID string) error {
return user.Delete(userID)
}
//
// Role
//

View File

@ -27,6 +27,9 @@ type DBUser struct {
HashedPassword string `bson:"hashedPassword"`
Status string `bson:"status"`
RoleID primitive.ObjectID `bson:"roleId"`
RequireToChangePassword bool `bson:"requireToChangePassword"`
Avatar string `bson:"avatar"`
Deleted bool `bson:"deleted"`
Other interface{} `bson:"other"`
CreatedAt time.Time `bson:"createdAt"`
UpdatedAt time.Time `bson:"updatedAt"`

View File

@ -14,6 +14,7 @@ type UserCreateOptions struct {
Password string
Status string
RoleID string
RequireToChangePassword bool
Other interface{}
}

View File

@ -66,6 +66,7 @@ func newUser(payload model.UserCreateOptions) model.DBUser {
Phone: payload.Phone,
Email: payload.Email,
HashedPassword: internal.HashPassword(payload.Password),
RequireToChangePassword: payload.RequireToChangePassword,
Status: payload.Status,
RoleID: roleID,
Other: payload.Other,
@ -391,3 +392,81 @@ func checkUserHasPermissionFromCache(roleID primitive.ObjectID, permission strin
return false
}
// UpdateAvatar ...
func UpdateAvatar(userID string, avatar string) error {
var (
ctx = context.Background()
)
if avatar == "" {
return errors.New("no avatar data")
}
// Find user exists or not
id, isValid := mongodb.NewIDFromString(userID)
if !isValid {
return errors.New("invalid role id data")
}
user, _ := findByID(ctx, id)
if user.ID.IsZero() {
return errors.New("user not found")
}
// Setup condition
cond := bson.M{
"_id": id,
}
// Setup update data
updateData := bson.M{
"$set": bson.M{
"avatar": avatar,
"updatedAt": internal.Now(),
},
}
// Update
if err := updateOneByCondition(ctx, cond, updateData); err != nil {
return err
}
return nil
}
// Delete ...
func Delete(userID string) error {
var (
ctx = context.Background()
)
// Find user exists or not
id, isValid := mongodb.NewIDFromString(userID)
if !isValid {
return errors.New("invalid role id data")
}
user, _ := findByID(ctx, id)
if user.ID.IsZero() {
return errors.New("user not found")
}
// Setup condition
cond := bson.M{
"_id": id,
}
// Setup update data
updateData := bson.M{
"$set": bson.M{
"deleted": true,
"updatedAt": internal.Now(),
},
}
// Update
if err := updateOneByCondition(ctx, cond, updateData); err != nil {
return err
}
return nil
}