-
Notifications
You must be signed in to change notification settings - Fork 0
/
userExample.go
283 lines (241 loc) · 10.8 KB
/
userExample.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
package service
import (
"context"
"errors"
"strings"
serverNameExampleV1 "github.com/i2dou/sponge/api/serverNameExample/v1"
"github.com/i2dou/sponge/internal/cache"
"github.com/i2dou/sponge/internal/dao"
"github.com/i2dou/sponge/internal/ecode"
"github.com/i2dou/sponge/internal/model"
"github.com/i2dou/sponge/pkg/grpc/interceptor"
"github.com/i2dou/sponge/pkg/logger"
"github.com/i2dou/sponge/pkg/mysql/query"
"github.com/jinzhu/copier"
"google.golang.org/grpc"
)
func init() {
registerFns = append(registerFns, func(server *grpc.Server) {
serverNameExampleV1.RegisterUserExampleServer(server, NewUserExampleServer()) // register service to the rpc service
})
}
var _ serverNameExampleV1.UserExampleServer = (*userExample)(nil)
type userExample struct {
serverNameExampleV1.UnimplementedUserExampleServer
iDao dao.UserExampleDao
}
// NewUserExampleServer create a new service
func NewUserExampleServer() serverNameExampleV1.UserExampleServer {
return &userExample{
iDao: dao.NewUserExampleDao(
model.GetDB(),
cache.NewUserExampleCache(model.GetCacheType()),
),
}
}
// Create a record
func (s *userExample) Create(ctx context.Context, req *serverNameExampleV1.CreateUserExampleRequest) (*serverNameExampleV1.CreateUserExampleReply, error) {
err := req.Validate()
if err != nil {
logger.Warn("req.Validate error", logger.Err(err), logger.Any("req", req), interceptor.ServerCtxRequestIDField(ctx))
return nil, ecode.StatusInvalidParams.Err()
}
record := &model.UserExample{}
err = copier.Copy(record, req)
if err != nil {
return nil, ecode.StatusCreateUserExample.Err()
}
ctx = context.WithValue(ctx, interceptor.ContextRequestIDKey, interceptor.ServerCtxRequestID(ctx)) //nolint
err = s.iDao.Create(ctx, record)
if err != nil {
logger.Error("Create error", logger.Err(err), logger.Any("userExample", record), interceptor.ServerCtxRequestIDField(ctx))
return nil, ecode.StatusInternalServerError.ToRPCErr()
}
return &serverNameExampleV1.CreateUserExampleReply{Id: record.ID}, nil
}
// DeleteByID delete a record by id
func (s *userExample) DeleteByID(ctx context.Context, req *serverNameExampleV1.DeleteUserExampleByIDRequest) (*serverNameExampleV1.DeleteUserExampleByIDReply, error) {
err := req.Validate()
if err != nil {
logger.Warn("req.Validate error", logger.Err(err), logger.Any("req", req), interceptor.ServerCtxRequestIDField(ctx))
return nil, ecode.StatusInvalidParams.Err()
}
ctx = context.WithValue(ctx, interceptor.ContextRequestIDKey, interceptor.ServerCtxRequestID(ctx)) //nolint
err = s.iDao.DeleteByID(ctx, req.Id)
if err != nil {
logger.Error("DeleteByID error", logger.Err(err), logger.Any("id", req.Id), interceptor.ServerCtxRequestIDField(ctx))
return nil, ecode.StatusInternalServerError.ToRPCErr()
}
return &serverNameExampleV1.DeleteUserExampleByIDReply{}, nil
}
// DeleteByIDs delete records by batch id
func (s *userExample) DeleteByIDs(ctx context.Context, req *serverNameExampleV1.DeleteUserExampleByIDsRequest) (*serverNameExampleV1.DeleteUserExampleByIDsReply, error) {
err := req.Validate()
if err != nil {
logger.Warn("req.Validate error", logger.Err(err), logger.Any("req", req), interceptor.ServerCtxRequestIDField(ctx))
return nil, ecode.StatusInvalidParams.Err()
}
ctx = context.WithValue(ctx, interceptor.ContextRequestIDKey, interceptor.ServerCtxRequestID(ctx)) //nolint
err = s.iDao.DeleteByIDs(ctx, req.Ids)
if err != nil {
logger.Error("DeleteByID error", logger.Err(err), logger.Any("ids", req.Ids), interceptor.ServerCtxRequestIDField(ctx))
return nil, ecode.StatusInternalServerError.ToRPCErr()
}
return &serverNameExampleV1.DeleteUserExampleByIDsReply{}, nil
}
// UpdateByID update a record by id
func (s *userExample) UpdateByID(ctx context.Context, req *serverNameExampleV1.UpdateUserExampleByIDRequest) (*serverNameExampleV1.UpdateUserExampleByIDReply, error) {
err := req.Validate()
if err != nil {
logger.Warn("req.Validate error", logger.Err(err), logger.Any("req", req), interceptor.ServerCtxRequestIDField(ctx))
return nil, ecode.StatusInvalidParams.Err()
}
record := &model.UserExample{}
err = copier.Copy(record, req)
if err != nil {
return nil, ecode.StatusUpdateByIDUserExample.Err()
}
record.ID = req.Id
ctx = context.WithValue(ctx, interceptor.ContextRequestIDKey, interceptor.ServerCtxRequestID(ctx)) //nolint
err = s.iDao.UpdateByID(ctx, record)
if err != nil {
logger.Error("UpdateByID error", logger.Err(err), logger.Any("userExample", record), interceptor.ServerCtxRequestIDField(ctx))
return nil, ecode.StatusInternalServerError.ToRPCErr()
}
return &serverNameExampleV1.UpdateUserExampleByIDReply{}, nil
}
// GetByID get a record by id
func (s *userExample) GetByID(ctx context.Context, req *serverNameExampleV1.GetUserExampleByIDRequest) (*serverNameExampleV1.GetUserExampleByIDReply, error) {
err := req.Validate()
if err != nil {
logger.Warn("req.Validate error", logger.Err(err), logger.Any("req", req), interceptor.ServerCtxRequestIDField(ctx))
return nil, ecode.StatusInvalidParams.Err()
}
ctx = context.WithValue(ctx, interceptor.ContextRequestIDKey, interceptor.ServerCtxRequestID(ctx)) //nolint
record, err := s.iDao.GetByID(ctx, req.Id)
if err != nil {
if errors.Is(err, query.ErrNotFound) {
logger.Warn("GetByID error", logger.Err(err), logger.Any("id", req.Id), interceptor.ServerCtxRequestIDField(ctx))
return nil, ecode.StatusNotFound.Err()
}
logger.Error("GetByID error", logger.Err(err), logger.Any("id", req.Id), interceptor.ServerCtxRequestIDField(ctx))
return nil, ecode.StatusInternalServerError.ToRPCErr()
}
data, err := convertUserExample(record)
if err != nil {
logger.Warn("convertUserExample error", logger.Err(err), logger.Any("userExample", record), interceptor.ServerCtxRequestIDField(ctx))
return nil, ecode.StatusGetByIDUserExample.Err()
}
return &serverNameExampleV1.GetUserExampleByIDReply{UserExample: data}, nil
}
// GetByCondition get a record by id
func (s *userExample) GetByCondition(ctx context.Context, req *serverNameExampleV1.GetUserExampleByConditionRequest) (*serverNameExampleV1.GetUserExampleByConditionReply, error) {
err := req.Validate()
if err != nil {
logger.Warn("req.Validate error", logger.Err(err), logger.Any("req", req), interceptor.ServerCtxRequestIDField(ctx))
return nil, ecode.StatusInvalidParams.Err()
}
ctx = context.WithValue(ctx, interceptor.ContextRequestIDKey, interceptor.ServerCtxRequestID(ctx)) //nolint
conditions := &query.Conditions{}
for _, v := range req.Conditions.GetColumns() {
column := query.Column{}
_ = copier.Copy(&column, v)
conditions.Columns = append(conditions.Columns, column)
}
err = conditions.CheckValid()
if err != nil {
logger.Warn("Parameters error", logger.Err(err), logger.Any("conditions", conditions), interceptor.ServerCtxRequestIDField(ctx))
return nil, ecode.StatusInvalidParams.Err()
}
record, err := s.iDao.GetByCondition(ctx, conditions)
if err != nil {
if errors.Is(err, query.ErrNotFound) {
logger.Warn("GetByCondition error", logger.Err(err), logger.Any("req", req), interceptor.ServerCtxRequestIDField(ctx))
return nil, ecode.StatusNotFound.Err()
}
logger.Error("GetByCondition error", logger.Err(err), logger.Any("req", req), interceptor.ServerCtxRequestIDField(ctx))
return nil, ecode.StatusInternalServerError.ToRPCErr()
}
data, err := convertUserExample(record)
if err != nil {
logger.Warn("convertUserExample error", logger.Err(err), logger.Any("userExample", record), interceptor.ServerCtxRequestIDField(ctx))
return nil, ecode.StatusGetByConditionUserExample.Err()
}
return &serverNameExampleV1.GetUserExampleByConditionReply{
UserExample: data,
}, nil
}
// ListByIDs list of records by batch id
func (s *userExample) ListByIDs(ctx context.Context, req *serverNameExampleV1.ListUserExampleByIDsRequest) (*serverNameExampleV1.ListUserExampleByIDsReply, error) {
err := req.Validate()
if err != nil {
logger.Warn("req.Validate error", logger.Err(err), logger.Any("req", req), interceptor.ServerCtxRequestIDField(ctx))
return nil, ecode.StatusInvalidParams.Err()
}
ctx = context.WithValue(ctx, interceptor.ContextRequestIDKey, interceptor.ServerCtxRequestID(ctx)) //nolint
userExampleMap, err := s.iDao.GetByIDs(ctx, req.Ids)
if err != nil {
logger.Error("GetByIDs error", logger.Err(err), logger.Any("ids", req.Ids), interceptor.ServerCtxRequestIDField(ctx))
return nil, ecode.StatusInternalServerError.ToRPCErr()
}
userExamples := []*serverNameExampleV1.UserExample{}
for _, id := range req.Ids {
if v, ok := userExampleMap[id]; ok {
record, err := convertUserExample(v)
if err != nil {
logger.Warn("convertUserExample error", logger.Err(err), logger.Any("userExample", v), interceptor.ServerCtxRequestIDField(ctx))
return nil, ecode.StatusInternalServerError.ToRPCErr()
}
userExamples = append(userExamples, record)
}
}
return &serverNameExampleV1.ListUserExampleByIDsReply{UserExamples: userExamples}, nil
}
// List of records by query parameters
func (s *userExample) List(ctx context.Context, req *serverNameExampleV1.ListUserExampleRequest) (*serverNameExampleV1.ListUserExampleReply, error) {
err := req.Validate()
if err != nil {
logger.Warn("req.Validate error", logger.Err(err), logger.Any("req", req), interceptor.ServerCtxRequestIDField(ctx))
return nil, ecode.StatusInvalidParams.Err()
}
params := &query.Params{}
err = copier.Copy(params, req.Params)
if err != nil {
return nil, ecode.StatusListUserExample.Err()
}
params.Size = int(req.Params.Limit)
ctx = context.WithValue(ctx, interceptor.ContextRequestIDKey, interceptor.ServerCtxRequestID(ctx)) //nolint
records, total, err := s.iDao.GetByColumns(ctx, params)
if err != nil {
if strings.Contains(err.Error(), "query params error:") {
logger.Warn("GetByColumns error", logger.Err(err), logger.Any("params", params), interceptor.ServerCtxRequestIDField(ctx))
return nil, ecode.StatusInvalidParams.Err()
}
logger.Error("GetByColumns error", logger.Err(err), logger.Any("params", params), interceptor.ServerCtxRequestIDField(ctx))
return nil, ecode.StatusInternalServerError.ToRPCErr()
}
userExamples := []*serverNameExampleV1.UserExample{}
for _, record := range records {
data, err := convertUserExample(record)
if err != nil {
logger.Warn("convertUserExample error", logger.Err(err), logger.Any("id", record.ID), interceptor.ServerCtxRequestIDField(ctx))
continue
}
userExamples = append(userExamples, data)
}
return &serverNameExampleV1.ListUserExampleReply{
Total: total,
UserExamples: userExamples,
}, nil
}
func convertUserExample(record *model.UserExample) (*serverNameExampleV1.UserExample, error) {
value := &serverNameExampleV1.UserExample{}
err := copier.Copy(value, record)
if err != nil {
return nil, err
}
value.Id = record.ID
value.CreatedAt = record.CreatedAt.Unix()
value.UpdatedAt = record.UpdatedAt.Unix()
return value, nil
}