-
Notifications
You must be signed in to change notification settings - Fork 0
/
contacts-grpc.go
156 lines (125 loc) · 3.75 KB
/
contacts-grpc.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
package goautowp
import (
"context"
"github.com/autowp/goautowp/users"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"google.golang.org/protobuf/types/known/emptypb"
)
type ContactsGRPCServer struct {
UnimplementedContactsServer
auth *Auth
contactsRepository *ContactsRepository
userRepository *users.Repository
userExtractor *users.UserExtractor
}
func NewContactsGRPCServer(
auth *Auth,
contactsRepository *ContactsRepository,
userRepository *users.Repository,
userExtractor *users.UserExtractor,
) *ContactsGRPCServer {
return &ContactsGRPCServer{
auth: auth,
contactsRepository: contactsRepository,
userRepository: userRepository,
userExtractor: userExtractor,
}
}
func (s *ContactsGRPCServer) CreateContact(ctx context.Context, in *CreateContactRequest) (*emptypb.Empty, error) {
userID, _, err := s.auth.ValidateGRPC(ctx)
if err != nil {
return nil, status.Error(codes.Internal, err.Error())
}
if userID == 0 {
return nil, status.Errorf(codes.PermissionDenied, "PermissionDenied")
}
if in.UserId == userID {
return nil, status.Errorf(codes.InvalidArgument, "InvalidArgument")
}
deleted := false
user, err := s.userRepository.User(users.GetUsersOptions{ID: in.UserId, Deleted: &deleted})
if err != nil {
return nil, status.Error(codes.Internal, err.Error())
}
if user == nil {
return nil, status.Error(codes.NotFound, "NotFound")
}
err = s.contactsRepository.create(ctx, userID, in.UserId)
if err != nil {
return nil, status.Error(codes.Internal, err.Error())
}
return &emptypb.Empty{}, nil
}
func (s *ContactsGRPCServer) DeleteContact(ctx context.Context, in *DeleteContactRequest) (*emptypb.Empty, error) {
userID, _, err := s.auth.ValidateGRPC(ctx)
if err != nil {
return nil, status.Error(codes.Internal, err.Error())
}
if userID == 0 {
return nil, status.Error(codes.PermissionDenied, "PermissionDenied")
}
err = s.contactsRepository.delete(ctx, userID, in.UserId)
if err != nil {
return nil, status.Error(codes.Internal, err.Error())
}
return &emptypb.Empty{}, nil
}
func (s *ContactsGRPCServer) GetContact(ctx context.Context, in *GetContactRequest) (*Contact, error) {
userID, _, err := s.auth.ValidateGRPC(ctx)
if err != nil {
return nil, status.Error(codes.Internal, err.Error())
}
if userID == 0 {
return nil, status.Error(codes.PermissionDenied, "PermissionDenied")
}
if in.UserId == userID {
return nil, status.Error(codes.InvalidArgument, "InvalidArgument")
}
exists, err := s.contactsRepository.isExists(ctx, userID, in.UserId)
if err != nil {
return nil, status.Error(codes.Internal, err.Error())
}
if !exists {
return nil, status.Error(codes.NotFound, "NotFound")
}
return &Contact{
ContactUserId: in.UserId,
}, nil
}
func (s *ContactsGRPCServer) GetContacts(ctx context.Context, in *GetContactsRequest) (*ContactItems, error) {
userID, _, err := s.auth.ValidateGRPC(ctx)
if err != nil {
return nil, status.Error(codes.Internal, err.Error())
}
if userID == 0 {
return nil, status.Error(codes.PermissionDenied, "PermissionDenied")
}
fields := in.Fields
m := make(map[string]bool)
for _, e := range fields {
m[e] = true
}
userRows, err := s.userRepository.Users(users.GetUsersOptions{
InContacts: userID,
Order: []string{"users.deleted", "users.name"},
Fields: m,
})
if err != nil {
return nil, status.Error(codes.Internal, err.Error())
}
items := make([]*Contact, len(userRows))
for idx := range userRows {
user, err := s.userExtractor.Extract(ctx, &userRows[idx], m)
if err != nil {
return nil, status.Error(codes.Internal, err.Error())
}
items[idx] = &Contact{
ContactUserId: user.ID,
User: APIUserToGRPC(user),
}
}
return &ContactItems{
Items: items,
}, nil
}