-
-
Notifications
You must be signed in to change notification settings - Fork 253
/
get_user_by_id_logic.go
81 lines (68 loc) · 1.91 KB
/
get_user_by_id_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
76
77
78
79
80
81
package user
import (
"context"
"github.com/suyuan32/simple-admin-common/utils/uuidx"
"github.com/suyuan32/simple-admin-core/rpc/ent"
"github.com/suyuan32/simple-admin-core/rpc/ent/user"
"github.com/suyuan32/simple-admin-core/rpc/internal/svc"
"github.com/suyuan32/simple-admin-core/rpc/internal/utils/errorhandler"
"github.com/suyuan32/simple-admin-core/rpc/types/core"
"github.com/zeromicro/go-zero/core/logx"
)
type GetUserByIdLogic struct {
ctx context.Context
svcCtx *svc.ServiceContext
logx.Logger
}
func NewGetUserByIdLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetUserByIdLogic {
return &GetUserByIdLogic{
ctx: ctx,
svcCtx: svcCtx,
Logger: logx.WithContext(ctx),
}
}
func (l *GetUserByIdLogic) GetUserById(in *core.UUIDReq) (*core.UserInfo, error) {
result, err := l.svcCtx.DB.User.Query().Where(user.IDEQ(uuidx.ParseUUIDString(in.Id))).WithRoles().First(l.ctx)
if err != nil {
return nil, errorhandler.DefaultEntError(l.Logger, err, in)
}
if err != nil {
return nil, err
}
return &core.UserInfo{
Nickname: result.Nickname,
Avatar: result.Avatar,
RoleIds: GetRoleIds(result.Edges.Roles),
Mobile: result.Mobile,
Email: result.Email,
Status: uint32(result.Status),
Id: result.ID.String(),
Username: result.Username,
HomePath: result.HomePath,
Description: result.Description,
DepartmentId: result.DepartmentID,
CreatedAt: result.CreatedAt.Unix(),
UpdatedAt: result.UpdatedAt.Unix(),
}, nil
}
func GetRoleIds(data []*ent.Role) []uint64 {
var ids []uint64
for _, v := range data {
ids = append(ids, v.ID)
}
return ids
}
func GetRoleCodes(data []*ent.Role) []string {
var codes []string
for _, v := range data {
codes = append(codes, v.Code)
}
return codes
}
func GetPositionIds(data []*ent.Position) []uint64 {
var ids []uint64
for _, v := range data {
ids = append(ids, v.ID)
}
return ids
}