-
-
Notifications
You must be signed in to change notification settings - Fork 253
/
update_user_logic.go
75 lines (61 loc) · 1.95 KB
/
update_user_logic.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package user
import (
"context"
"github.com/suyuan32/simple-admin-core/pkg/ent"
"github.com/suyuan32/simple-admin-core/pkg/utils/errorhandler"
"github.com/suyuan32/simple-admin-core/rpc/internal/svc"
"github.com/suyuan32/simple-admin-core/rpc/types/core"
"github.com/zeromicro/go-zero/core/logx"
"github.com/suyuan32/simple-admin-core/pkg/i18n"
"github.com/suyuan32/simple-admin-core/pkg/utils"
"github.com/suyuan32/simple-admin-core/pkg/uuidx"
)
type UpdateUserLogic struct {
ctx context.Context
svcCtx *svc.ServiceContext
logx.Logger
}
func NewUpdateUserLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UpdateUserLogic {
return &UpdateUserLogic{
ctx: ctx,
svcCtx: svcCtx,
Logger: logx.WithContext(ctx),
}
}
func (l *UpdateUserLogic) UpdateUser(in *core.UserInfo) (*core.BaseResp, error) {
err := utils.WithTx(l.ctx, l.svcCtx.DB, func(tx *ent.Tx) error {
updateQuery := tx.User.UpdateOneID(uuidx.ParseUUIDString(in.Id)).
SetNotEmptyUsername(in.Username).
SetNotEmptyNickname(in.Nickname).
SetNotEmptyEmail(in.Email).
SetNotEmptyMobile(in.Mobile).
SetNotEmptyAvatar(in.Avatar).
SetNotEmptyHomePath(in.HomePath).
SetNotEmptyDescription(in.Description).
SetNotEmptyDepartmentID(in.DepartmentId)
if in.Password != "" {
updateQuery = updateQuery.SetNotEmptyPassword(utils.BcryptEncrypt(in.Password))
}
if in.RoleIds != nil {
err := l.svcCtx.DB.User.UpdateOneID(uuidx.ParseUUIDString(in.Id)).ClearRoles().Exec(l.ctx)
if err != nil {
return err
}
updateQuery = updateQuery.AddRoleIDs(in.RoleIds...)
}
if in.PositionIds != nil {
err := l.svcCtx.DB.User.UpdateOneID(uuidx.ParseUUIDString(in.Id)).ClearPositions().Exec(l.ctx)
if err != nil {
return err
}
updateQuery = updateQuery.AddPositionIDs(in.PositionIds...)
}
return updateQuery.Exec(l.ctx)
})
if err != nil {
return nil, errorhandler.DefaultEntError(err, in)
}
return &core.BaseResp{
Msg: i18n.Success,
}, nil
}