From 2a5e05c3ad1b7edd8c5038a02b4ddd6510b1850e Mon Sep 17 00:00:00 2001 From: Hoang Date: Wed, 17 Nov 2021 13:32:29 +0700 Subject: [PATCH] update User --- action.go | 10 +++++ model/db.go | 25 ++++++----- model/user_request.go | 15 ++++--- user/handle.go | 101 +++++++++++++++++++++++++++++++++++++----- 4 files changed, 122 insertions(+), 29 deletions(-) diff --git a/action.go b/action.go index e10eefa..3b50fdd 100644 --- a/action.go +++ b/action.go @@ -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 // diff --git a/model/db.go b/model/db.go index b4b0795..e4b4d16 100644 --- a/model/db.go +++ b/model/db.go @@ -19,17 +19,20 @@ type DBRole struct { // DBUser ... type DBUser struct { - ID primitive.ObjectID `bson:"_id"` - Name string `bson:"name"` - SearchString string `bson:"searchString"` - Phone string `bson:"phone"` // unique - Email string `bson:"email"` // unique - HashedPassword string `bson:"hashedPassword"` - Status string `bson:"status"` - RoleID primitive.ObjectID `bson:"roleId"` - Other interface{} `bson:"other"` - CreatedAt time.Time `bson:"createdAt"` - UpdatedAt time.Time `bson:"updatedAt"` + ID primitive.ObjectID `bson:"_id"` + Name string `bson:"name"` + SearchString string `bson:"searchString"` + Phone string `bson:"phone"` // unique + Email string `bson:"email"` // unique + 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"` } // DBPermission ... diff --git a/model/user_request.go b/model/user_request.go index ab95ea5..3c7f3f6 100644 --- a/model/user_request.go +++ b/model/user_request.go @@ -8,13 +8,14 @@ import ( // UserCreateOptions ... type UserCreateOptions struct { - Name string - Phone string - Email string - Password string - Status string - RoleID string - Other interface{} + Name string + Phone string + Email string + Password string + Status string + RoleID string + RequireToChangePassword bool + Other interface{} } // UserUpdateOptions ... diff --git a/user/handle.go b/user/handle.go index 0b252cc..3d62dca 100644 --- a/user/handle.go +++ b/user/handle.go @@ -60,17 +60,18 @@ func newUser(payload model.UserCreateOptions) model.DBUser { timeNow := internal.Now() roleID, _ := mongodb.NewIDFromString(payload.RoleID) return model.DBUser{ - ID: mongodb.NewObjectID(), - Name: payload.Name, - SearchString: internal.GetSearchString(payload.Name, payload.Phone, payload.Email), - Phone: payload.Phone, - Email: payload.Email, - HashedPassword: internal.HashPassword(payload.Password), - Status: payload.Status, - RoleID: roleID, - Other: payload.Other, - CreatedAt: timeNow, - UpdatedAt: timeNow, + ID: mongodb.NewObjectID(), + Name: payload.Name, + SearchString: internal.GetSearchString(payload.Name, payload.Phone, payload.Email), + Phone: payload.Phone, + Email: payload.Email, + HashedPassword: internal.HashPassword(payload.Password), + RequireToChangePassword: payload.RequireToChangePassword, + Status: payload.Status, + RoleID: roleID, + Other: payload.Other, + CreatedAt: timeNow, + UpdatedAt: timeNow, } } @@ -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 +}