forked from free-lancers/electricity_bill_calc_service
feat(user):增加修改用户可用性状态的功能。
This commit is contained in:
@@ -169,10 +169,17 @@ func (_UserService) IsUserExists(uid string) (bool, error) {
|
||||
return global.DBConn.ID(uid).Exist(&model.User{})
|
||||
}
|
||||
|
||||
func (_UserService) CreateUser(user *model.User, detail *model.UserDetail) (string, error) {
|
||||
func (u _UserService) CreateUser(user *model.User, detail *model.UserDetail) (string, error) {
|
||||
if len(user.Id) == 0 {
|
||||
user.Id = uuid.New().String()
|
||||
}
|
||||
exists, err := u.IsUserExists(user.Id)
|
||||
if exists {
|
||||
return "", exceptions.NewNotFoundError("user already exists")
|
||||
}
|
||||
if err != nil {
|
||||
return "", nil
|
||||
}
|
||||
detail.Id = user.Id
|
||||
|
||||
verifyCode := utils.RandStr(10)
|
||||
@@ -186,7 +193,7 @@ func (_UserService) CreateUser(user *model.User, detail *model.UserDetail) (stri
|
||||
if err := tx.Begin(); err != nil {
|
||||
return "", err
|
||||
}
|
||||
_, err := tx.Insert(user)
|
||||
_, err = tx.Insert(user)
|
||||
if err != nil {
|
||||
tx.Rollback()
|
||||
return "", fmt.Errorf("user create failed: %w", err)
|
||||
@@ -203,3 +210,17 @@ func (_UserService) CreateUser(user *model.User, detail *model.UserDetail) (stri
|
||||
}
|
||||
return verifyCode, nil
|
||||
}
|
||||
|
||||
func (u _UserService) SwitchUserState(uid string, enabled bool) error {
|
||||
exists, err := u.IsUserExists(uid)
|
||||
if !exists {
|
||||
return exceptions.NewNotFoundError("user not exists")
|
||||
}
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
newStateUser := new(model.User)
|
||||
newStateUser.Enabled = enabled
|
||||
_, err = global.DBConn.ID(uid).Cols("enabled").Update(newStateUser)
|
||||
return err
|
||||
}
|
||||
|
Reference in New Issue
Block a user