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

@ -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 ...

View File

@ -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 ...

View File

@ -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
}