-
Notifications
You must be signed in to change notification settings - Fork 0
/
user.go
127 lines (115 loc) · 3.79 KB
/
user.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
package grpcserver
import (
"context"
"github.com/n-creativesystem/rbns/domain/model"
"github.com/n-creativesystem/rbns/protobuf"
"github.com/n-creativesystem/rbns/protoconv"
"github.com/n-creativesystem/rbns/service"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"google.golang.org/protobuf/types/known/emptypb"
)
type userServer struct {
*protobuf.UnimplementedUserServer
svc service.UserService
orgSvc service.OrganizationService
}
var _ protobuf.UserServer = (*userServer)(nil)
func newUserServer(svc service.UserService, orgSvc service.OrganizationService) protobuf.UserServer {
return &userServer{svc: svc, orgSvc: orgSvc}
}
// User
func (s *userServer) Create(ctx context.Context, in *protobuf.UserEntity) (*emptypb.Empty, error) {
roles := make([]string, len(in.GetRoles()))
for idx, role := range in.GetRoles() {
roles[idx] = role.GetId()
}
err := s.svc.Create(ctx, in.GetKey(), in.GetOrganizationId(), roles...)
if err != nil {
if err == model.ErrNoData {
return nil, status.Error(codes.NotFound, err.Error())
}
return nil, status.Error(codes.Internal, err.Error())
}
return &emptypb.Empty{}, nil
}
func (s *userServer) Delete(ctx context.Context, in *protobuf.UserKey) (*emptypb.Empty, error) {
err := s.svc.Delete(ctx, in.GetKey(), in.GetOrganizationId())
if err != nil {
if err == model.ErrNoData {
return nil, status.Error(codes.NotFound, err.Error())
}
return nil, status.Error(codes.Internal, err.Error())
}
return &emptypb.Empty{}, err
}
func (s *userServer) FindByKey(ctx context.Context, in *protobuf.UserKey) (*protobuf.UserEntity, error) {
u, err := s.svc.FindByKey(ctx, in.GetKey(), in.GetOrganizationId())
if err != nil {
if err == model.ErrNoData {
return nil, status.Error(codes.NotFound, err.Error())
}
return nil, status.Error(codes.Internal, err.Error())
}
out := protoconv.NewUserEntityByModel(*u)
out.OrganizationId = in.GetOrganizationId()
return out, nil
}
func (s *userServer) FindByOrganizationNameAndUserKey(ctx context.Context, in *protobuf.UserKeyByName) (*protobuf.UserEntity, error) {
org, err := s.orgSvc.FindByName(ctx, in.OrganizationName)
if err != nil {
if err == model.ErrNoData {
return nil, status.Error(codes.NotFound, err.Error())
}
return nil, status.Error(codes.Internal, err.Error())
}
return s.FindByKey(ctx, &protobuf.UserKey{
Key: in.Key,
OrganizationId: *org.GetID(),
})
}
func (s *userServer) AddRoles(ctx context.Context, in *protobuf.UserRole) (*emptypb.Empty, error) {
roles := make([]string, len(in.GetRoles()))
if len(roles) == 0 {
return &emptypb.Empty{}, nil
}
for idx, role := range in.GetRoles() {
roles[idx] = role.GetId()
}
err := s.svc.AddRole(ctx, in.GetKey(), in.GetOrganizationId(), roles)
if err != nil {
if err == model.ErrNoData {
return &emptypb.Empty{}, status.Error(codes.NotFound, err.Error())
}
return &emptypb.Empty{}, status.Error(codes.Internal, err.Error())
}
return &emptypb.Empty{}, nil
}
func (s *userServer) DeleteRoles(ctx context.Context, in *protobuf.UserRole) (*emptypb.Empty, error) {
roles := make([]string, len(in.GetRoles()))
if len(roles) == 0 {
return &emptypb.Empty{}, nil
}
for idx, role := range in.GetRoles() {
roles[idx] = role.GetId()
}
err := s.svc.DeleteRole(ctx, in.GetKey(), in.GetOrganizationId(), roles)
if err != nil {
if err == model.ErrNoData {
return &emptypb.Empty{}, status.Error(codes.NotFound, err.Error())
}
return &emptypb.Empty{}, status.Error(codes.Internal, err.Error())
}
return &emptypb.Empty{}, nil
}
func (s *userServer) DeleteRole(ctx context.Context, in *protobuf.UserRoleDelete) (*emptypb.Empty, error) {
return s.DeleteRoles(ctx, &protobuf.UserRole{
Key: in.Key,
OrganizationId: in.OrganizationId,
Roles: []*protobuf.RoleKey{
{
Id: in.RoleId,
},
},
})
}