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) 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 // Role
// //

View File

@ -19,17 +19,20 @@ type DBRole struct {
// DBUser ... // DBUser ...
type DBUser struct { type DBUser struct {
ID primitive.ObjectID `bson:"_id"` ID primitive.ObjectID `bson:"_id"`
Name string `bson:"name"` Name string `bson:"name"`
SearchString string `bson:"searchString"` SearchString string `bson:"searchString"`
Phone string `bson:"phone"` // unique Phone string `bson:"phone"` // unique
Email string `bson:"email"` // unique Email string `bson:"email"` // unique
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 interface{} `bson:"other"` RequireToChangePassword bool `bson:"requireToChangePassword"`
CreatedAt time.Time `bson:"createdAt"` Avatar string `bson:"avatar"`
UpdatedAt time.Time `bson:"updatedAt"` Deleted bool `bson:"deleted"`
Other interface{} `bson:"other"`
CreatedAt time.Time `bson:"createdAt"`
UpdatedAt time.Time `bson:"updatedAt"`
} }
// DBPermission ... // DBPermission ...

View File

@ -8,13 +8,14 @@ import (
// UserCreateOptions ... // UserCreateOptions ...
type UserCreateOptions struct { type UserCreateOptions struct {
Name string Name string
Phone string Phone string
Email string Email string
Password string Password string
Status string Status string
RoleID string RoleID string
Other interface{} RequireToChangePassword bool
Other interface{}
} }
// UserUpdateOptions ... // UserUpdateOptions ...

View File

@ -60,17 +60,18 @@ func newUser(payload model.UserCreateOptions) model.DBUser {
timeNow := internal.Now() timeNow := internal.Now()
roleID, _ := mongodb.NewIDFromString(payload.RoleID) roleID, _ := mongodb.NewIDFromString(payload.RoleID)
return model.DBUser{ return model.DBUser{
ID: mongodb.NewObjectID(), ID: mongodb.NewObjectID(),
Name: payload.Name, Name: payload.Name,
SearchString: internal.GetSearchString(payload.Name, payload.Phone, payload.Email), SearchString: internal.GetSearchString(payload.Name, payload.Phone, payload.Email),
Phone: payload.Phone, Phone: payload.Phone,
Email: payload.Email, Email: payload.Email,
HashedPassword: internal.HashPassword(payload.Password), HashedPassword: internal.HashPassword(payload.Password),
Status: payload.Status, RequireToChangePassword: payload.RequireToChangePassword,
RoleID: roleID, Status: payload.Status,
Other: payload.Other, RoleID: roleID,
CreatedAt: timeNow, Other: payload.Other,
UpdatedAt: timeNow, CreatedAt: timeNow,
UpdatedAt: timeNow,
} }
} }
@ -391,3 +392,81 @@ func checkUserHasPermissionFromCache(roleID primitive.ObjectID, permission strin
return false 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
}