-
Notifications
You must be signed in to change notification settings - Fork 44
/
creation.go
522 lines (457 loc) · 18.3 KB
/
creation.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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
package biz
import (
"context"
kerrors "github.com/go-kratos/kratos/v2/errors"
"github.com/go-kratos/kratos/v2/log"
v1 "github.com/the-zion/matrix-core/api/creation/service/v1"
"golang.org/x/sync/errgroup"
"sort"
)
type CreationRepo interface {
GetLeaderBoard(ctx context.Context) ([]*LeaderBoard, error)
GetLastCollectionsDraft(ctx context.Context, uuid string) (*CollectionsDraft, error)
GetCollectionsContentReview(ctx context.Context, page int32, uuid string) ([]*TextReview, error)
GetCollectArticleList(ctx context.Context, id, page int32) ([]*Article, error)
GetCollectArticleCount(ctx context.Context, id int32) (int32, error)
GetCollectTalkList(ctx context.Context, id, page int32) ([]*Talk, error)
GetCollectTalkCount(ctx context.Context, id int32) (int32, error)
GetCollectColumnList(ctx context.Context, id, page int32) ([]*Column, error)
GetCollectColumnCount(ctx context.Context, id int32) (int32, error)
GetCollectCount(ctx context.Context, id int32) (int64, error)
GetCollections(ctx context.Context, id int32, uuid string) (*Collections, error)
GetCollectionListInfo(ctx context.Context, ids []int32) ([]*Collections, error)
GetCollectionsList(ctx context.Context, uuid string, page int32) ([]*Collections, error)
GetCollectionsListAll(ctx context.Context, uuid string) ([]*Collections, error)
GetCollectionsListByVisitor(ctx context.Context, uuid string, page int32) ([]*Collections, error)
GetCollectionsCount(ctx context.Context, uuid string) (int32, error)
GetCollectionsVisitorCount(ctx context.Context, uuid string) (int32, error)
GetCreationUser(ctx context.Context, uuid string) (*CreationUser, error)
GetCreationUserVisitor(ctx context.Context, uuid string) (*CreationUser, error)
GetUserTimeLineList(ctx context.Context, page int32, uuid string) ([]*TimeLine, error)
GetCollectionsAuth(ctx context.Context, id int32) (int32, error)
GetUserTimeLine(ctx context.Context, creationId, mode int32) (int32, error)
CreateCollectionsDraft(ctx context.Context, uuid string) (int32, error)
CreateCollectionsFolder(ctx context.Context, id int32, uuid string) error
CreateCollections(ctx context.Context, id, auth int32, uuid string) error
CreateCollectionsCache(ctx context.Context, id, auth int32, uuid, mode string) error
CreateTimeLine(ctx context.Context, creationsId, auth, mode int32, uuid string) (int32, error)
CreateTimeLineCache(ctx context.Context, id, creationId, mode int32, uuid string) error
AddCreationUserCollections(ctx context.Context, uuid string, auth int32) error
EditCollectionsCos(ctx context.Context, id int32, uuid string) error
UpdateCollectionsCache(ctx context.Context, id, auth int32, uuid, mode string) error
DeleteCollections(ctx context.Context, id int32, uuid string) error
DeleteCollect(ctx context.Context, id int32) error
DeleteCollectionsDraft(ctx context.Context, id int32, uuid string) error
DeleteCreationCache(ctx context.Context, id, auth int32, uuid string) error
DeleteTimeLine(ctx context.Context, id int32) error
DeleteTimeLineCache(ctx context.Context, id, creationId, mode int32, uuid string) error
ReduceCreationUserCollections(ctx context.Context, auth int32, uuid string) error
SetRecord(ctx context.Context, id, mode int32, uuid, operation, ip string) error
SetLeaderBoardToCache(ctx context.Context, boardList []*LeaderBoard)
SendReviewToMq(ctx context.Context, review *CollectionsReview) error
SendCollectionsToMq(ctx context.Context, collections *Collections, mode string) error
SendCollections(ctx context.Context, id int32, uuid string) (*CollectionsDraft, error)
SendCollectionsContentIrregularToMq(ctx context.Context, review *TextReview) error
SetCollectionsContentIrregular(ctx context.Context, review *TextReview) (*TextReview, error)
SetCollectionsContentIrregularToCache(ctx context.Context, review *TextReview) error
}
type CreationUseCase struct {
repo CreationRepo
articleRepo ArticleRepo
talkRepo TalkRepo
columnRepo ColumnRepo
tm Transaction
re Recovery
log *log.Helper
}
func NewCreationUseCase(repo CreationRepo, articleRepo ArticleRepo, talkRepo TalkRepo, columnRepo ColumnRepo, tm Transaction, re Recovery, logger log.Logger) *CreationUseCase {
return &CreationUseCase{
repo: repo,
articleRepo: articleRepo,
talkRepo: talkRepo,
columnRepo: columnRepo,
re: re,
tm: tm,
log: log.NewHelper(log.With(logger, "module", "creation/biz/creationUseCase")),
}
}
func (r *CreationUseCase) GetLeaderBoard(ctx context.Context) ([]*LeaderBoard, error) {
boardList, err := r.repo.GetLeaderBoard(ctx)
if err != nil {
return nil, v1.ErrorGetLeaderBoardFailed("get leader board failed: %s", err.Error())
}
if len(boardList) != 0 {
return boardList, nil
}
g, _ := errgroup.WithContext(ctx)
g.Go(r.re.GroupRecover(ctx, func(ctx context.Context) error {
return r.getLeaderBoardFromArticle(ctx, &boardList)
}))
g.Go(r.re.GroupRecover(ctx, func(ctx context.Context) error {
return r.getLeaderBoardFromTalk(ctx, &boardList)
}))
g.Go(r.re.GroupRecover(ctx, func(ctx context.Context) error {
return r.getLeaderBoardFromColumn(ctx, &boardList)
}))
err = g.Wait()
if err != nil {
return nil, err
}
if len(boardList) != 0 {
r.re.GroupRecover(ctx, func(ctx context.Context) error {
r.repo.SetLeaderBoardToCache(context.Background(), boardList)
return nil
})
}
sort.SliceStable(boardList, func(i, j int) bool {
return boardList[i].Agree > boardList[j].Agree
})
return boardList, nil
}
func (r *CreationUseCase) GetLastCollectionsDraft(ctx context.Context, uuid string) (*CollectionsDraft, error) {
draft, err := r.repo.GetLastCollectionsDraft(ctx, uuid)
if kerrors.IsNotFound(err) {
return nil, v1.ErrorRecordNotFound("last draft not found: %s", err.Error())
}
if err != nil {
return nil, v1.ErrorGetTalkDraftFailed("get last draft failed: %s", err.Error())
}
return draft, nil
}
func (r *CreationUseCase) GetCollectionsContentReview(ctx context.Context, page int32, uuid string) ([]*TextReview, error) {
reviewList, err := r.repo.GetCollectionsContentReview(ctx, page, uuid)
if err != nil {
return nil, v1.ErrorGetContentReviewFailed("get collections content review failed: %s", err.Error())
}
return reviewList, nil
}
func (r *CreationUseCase) getLeaderBoardFromArticle(ctx context.Context, boardList *[]*LeaderBoard) error {
articleList, err := r.articleRepo.GetArticleHotFromDB(ctx, 1)
if err != nil {
return err
}
for _, item := range articleList {
*boardList = append(*boardList, &LeaderBoard{
Id: item.ArticleId,
Agree: item.Agree,
Uuid: item.Uuid,
Mode: "article",
})
}
return nil
}
func (r *CreationUseCase) getLeaderBoardFromTalk(ctx context.Context, boardList *[]*LeaderBoard) error {
talkList, err := r.talkRepo.GetTalkHotFromDB(ctx, 1)
if err != nil {
return err
}
for _, item := range talkList {
*boardList = append(*boardList, &LeaderBoard{
Id: item.TalkId,
Agree: item.Agree,
Uuid: item.Uuid,
Mode: "talk",
})
}
return nil
}
func (r *CreationUseCase) getLeaderBoardFromColumn(ctx context.Context, boardList *[]*LeaderBoard) error {
columnList, err := r.columnRepo.GetColumnHotFromDB(ctx, 1)
if err != nil {
return err
}
for _, item := range columnList {
*boardList = append(*boardList, &LeaderBoard{
Id: item.ColumnId,
Agree: item.Agree,
Uuid: item.Uuid,
Mode: "column",
})
}
return nil
}
func (r *CreationUseCase) GetCollectArticleList(ctx context.Context, id, page int32) ([]*Article, error) {
articleList, err := r.repo.GetCollectArticleList(ctx, id, page)
if err != nil {
return nil, v1.ErrorGetCollectArticleFailed("get collect article list failed: %s", err.Error())
}
return articleList, nil
}
func (r *CreationUseCase) GetCollectArticleCount(ctx context.Context, id int32) (int32, error) {
count, err := r.repo.GetCollectArticleCount(ctx, id)
if err != nil {
return 0, v1.ErrorGetCountFailed("get collect article count failed: %s", err.Error())
}
return count, nil
}
func (r *CreationUseCase) GetCollectTalkList(ctx context.Context, id, page int32) ([]*Talk, error) {
talkList, err := r.repo.GetCollectTalkList(ctx, id, page)
if err != nil {
return nil, v1.ErrorGetTalkListFailed("get collect talk list failed: %s", err.Error())
}
return talkList, nil
}
func (r *CreationUseCase) GetCollectTalkCount(ctx context.Context, id int32) (int32, error) {
count, err := r.repo.GetCollectTalkCount(ctx, id)
if err != nil {
return 0, v1.ErrorGetCountFailed("get collect talk count failed: %s", err.Error())
}
return count, nil
}
func (r *CreationUseCase) GetCollectColumnList(ctx context.Context, id, page int32) ([]*Column, error) {
columnList, err := r.repo.GetCollectColumnList(ctx, id, page)
if err != nil {
return nil, v1.ErrorGetColumnListFailed("get collect column list failed: %s", err.Error())
}
return columnList, nil
}
func (r *CreationUseCase) GetCollectColumnCount(ctx context.Context, id int32) (int32, error) {
count, err := r.repo.GetCollectColumnCount(ctx, id)
if err != nil {
return 0, v1.ErrorGetCountFailed("get collect column count failed: %s", err.Error())
}
return count, nil
}
func (r *CreationUseCase) GetCollections(ctx context.Context, id int32, uuid string) (*Collections, error) {
collection, err := r.repo.GetCollections(ctx, id, uuid)
if err != nil {
return nil, v1.ErrorGetCollectionFailed("get collection failed: %s", err.Error())
}
return collection, nil
}
func (r *CreationUseCase) GetCollectionListInfo(ctx context.Context, ids []int32) ([]*Collections, error) {
collectionListInfo, err := r.repo.GetCollectionListInfo(ctx, ids)
if err != nil {
return nil, v1.ErrorGetCollectionFailed("get collection failed: %s", err.Error())
}
return collectionListInfo, nil
}
func (r *CreationUseCase) GetCollectionsList(ctx context.Context, uuid string, page int32) ([]*Collections, error) {
collections, err := r.repo.GetCollectionsList(ctx, uuid, page)
if err != nil {
return nil, v1.ErrorGetCollectionsListFailed("get collections failed: %s", err.Error())
}
return collections, nil
}
func (r *CreationUseCase) GetCollectionsListAll(ctx context.Context, uuid string) ([]*Collections, error) {
collections, err := r.repo.GetCollectionsListAll(ctx, uuid)
if err != nil {
return nil, v1.ErrorGetCollectionsListFailed("get collections all failed: %s", err.Error())
}
return collections, nil
}
func (r *CreationUseCase) GetCollectionsListByVisitor(ctx context.Context, uuid string, page int32) ([]*Collections, error) {
collections, err := r.repo.GetCollectionsListByVisitor(ctx, uuid, page)
if err != nil {
return nil, v1.ErrorGetCollectionsListFailed("get collections failed: %s", err.Error())
}
return collections, nil
}
func (r *CreationUseCase) GetCollectionsCount(ctx context.Context, uuid string) (int32, error) {
count, err := r.repo.GetCollectionsCount(ctx, uuid)
if err != nil {
return 0, v1.ErrorGetCountFailed("get collections count failed: %s", err.Error())
}
return count, nil
}
func (r *CreationUseCase) GetCollectionsVisitorCount(ctx context.Context, uuid string) (int32, error) {
count, err := r.repo.GetCollectionsVisitorCount(ctx, uuid)
if err != nil {
return 0, v1.ErrorGetCountFailed("get collections count failed: %s", err.Error())
}
return count, nil
}
func (r *CreationUseCase) GetCreationUser(ctx context.Context, uuid string) (*CreationUser, error) {
creationUser, err := r.repo.GetCreationUser(ctx, uuid)
if err != nil {
return nil, v1.ErrorGetCreationUserFailed("get creation user failed: %s", err.Error())
}
return creationUser, nil
}
func (r *CreationUseCase) GetCreationUserVisitor(ctx context.Context, uuid string) (*CreationUser, error) {
creationUser, err := r.repo.GetCreationUserVisitor(ctx, uuid)
if err != nil {
return nil, v1.ErrorGetCreationUserFailed("get creation user failed: %s", err.Error())
}
return creationUser, nil
}
func (r *CreationUseCase) GetUserTimeLineList(ctx context.Context, page int32, uuid string) ([]*TimeLine, error) {
timeline, err := r.repo.GetUserTimeLineList(ctx, page, uuid)
if err != nil {
return nil, v1.ErrorGetTimelineListFailed("get user timeline list failed: %s", err.Error())
}
return timeline, nil
}
func (r *CreationUseCase) SendCollections(ctx context.Context, id int32, uuid, ip string) error {
return r.tm.ExecTx(ctx, func(ctx context.Context) error {
draft, err := r.repo.SendCollections(ctx, id, uuid)
if err != nil {
return v1.ErrorCreateCollectionsFailed("create collections failed: %s", err.Error())
}
err = r.repo.SetRecord(ctx, id, 4, uuid, "create", ip)
if err != nil {
return v1.ErrorSetRecordFailed("set record failed: %s", err.Error())
}
err = r.repo.SendReviewToMq(ctx, &CollectionsReview{
Uuid: draft.Uuid,
Id: draft.Id,
Mode: "collections_create_review",
})
if err != nil {
return v1.ErrorCreateCollectionsFailed("send create review to mq failed: %s", err.Error())
}
return nil
})
}
func (r *CreationUseCase) CreateCollectionsDraft(ctx context.Context, uuid string) (int32, error) {
var id int32
err := r.tm.ExecTx(ctx, func(ctx context.Context) error {
var err error
id, err = r.repo.CreateCollectionsDraft(ctx, uuid)
if err != nil {
return v1.ErrorCreateDraftFailed("create collections draft failed: %s", err.Error())
}
err = r.repo.CreateCollectionsFolder(ctx, id, uuid)
if err != nil {
return v1.ErrorCreateDraftFailed("create collections folder failed: %s", err.Error())
}
return nil
})
if err != nil {
return 0, err
}
return id, nil
}
func (r *CreationUseCase) CreateCollections(ctx context.Context, id, auth int32, uuid string) error {
err := r.repo.SendCollectionsToMq(ctx, &Collections{
CollectionsId: id,
Uuid: uuid,
Auth: auth,
}, "create_collections_db_and_cache")
if err != nil {
return v1.ErrorCreateCollectionsFailed("create collections to mq failed: %s", err.Error())
}
return nil
}
func (r *CreationUseCase) CreateCollectionsDbAndCache(ctx context.Context, id, auth int32, uuid string) error {
return r.tm.ExecTx(ctx, func(ctx context.Context) error {
var err error
err = r.repo.DeleteCollectionsDraft(ctx, id, uuid)
if err != nil {
return v1.ErrorCreateCollectionsFailed("delete collections draft failed: %s", err.Error())
}
err = r.repo.CreateCollections(ctx, id, auth, uuid)
if err != nil {
return v1.ErrorCreateCollectionsFailed("create collections failed: %s", err.Error())
}
err = r.repo.AddCreationUserCollections(ctx, uuid, auth)
if err != nil {
return v1.ErrorCreateCollectionsFailed("add creation collections failed: %s", err.Error())
}
err = r.repo.CreateCollectionsCache(ctx, id, auth, uuid, "create")
if err != nil {
return v1.ErrorCreateCollectionsFailed("create collections cache failed: %s", err.Error())
}
return nil
})
}
func (r *CreationUseCase) CollectionsContentIrregular(ctx context.Context, review *TextReview) error {
err := r.repo.SendCollectionsContentIrregularToMq(ctx, review)
if err != nil {
return v1.ErrorSetContentIrregularFailed("set collections content irregular to mq failed: %s", err.Error())
}
return nil
}
func (r *CreationUseCase) AddCollectionsContentReviewDbAndCache(ctx context.Context, review *TextReview) error {
return r.tm.ExecTx(ctx, func(ctx context.Context) error {
review, err := r.repo.SetCollectionsContentIrregular(ctx, review)
if err != nil {
return v1.ErrorSetContentIrregularFailed("set collections content irregular failed: %s", err.Error())
}
err = r.repo.SetCollectionsContentIrregularToCache(ctx, review)
if err != nil {
return v1.ErrorSetContentIrregularFailed("set collections content irregular to cache failed: %s", err.Error())
}
return nil
})
}
func (r *CreationUseCase) SendCollectionsEdit(ctx context.Context, id int32, uuid, ip string) error {
collections, err := r.repo.GetCollections(ctx, id, uuid)
if err != nil {
return v1.ErrorGetCollectionFailed("get collections failed: %s", err.Error())
}
if collections.Uuid != uuid {
return v1.ErrorEditCollectionsFailed("send collections edit failed: no auth")
}
err = r.repo.SetRecord(ctx, id, 4, uuid, "edit", ip)
if err != nil {
return v1.ErrorSetRecordFailed("set record failed: %s", err.Error())
}
err = r.repo.SendReviewToMq(ctx, &CollectionsReview{
Uuid: collections.Uuid,
Id: id,
Mode: "collections_edit_review",
})
if err != nil {
return v1.ErrorEditCollectionsFailed("send edit review to mq failed: %s", err.Error())
}
return nil
}
func (r *CreationUseCase) EditCollections(ctx context.Context, id, auth int32, uuid string) error {
err := r.repo.SendCollectionsToMq(ctx, &Collections{
CollectionsId: id,
Auth: auth,
Uuid: uuid,
}, "edit_collections_cos")
if err != nil {
return v1.ErrorEditTalkFailed("edit collections to mq failed: %s", err.Error())
}
return nil
}
func (r *CreationUseCase) EditCollectionsCos(ctx context.Context, id, auth int32, uuid string) error {
err := r.repo.UpdateCollectionsCache(ctx, id, auth, uuid, "edit")
if err != nil {
return v1.ErrorEditTalkFailed("edit collections cache failed: %s", err.Error())
}
err = r.repo.EditCollectionsCos(ctx, id, uuid)
if err != nil {
return v1.ErrorEditTalkFailed("edit collections cache failed: %s", err.Error())
}
return nil
}
func (r *CreationUseCase) DeleteCollections(ctx context.Context, id int32, uuid string) error {
err := r.repo.SendCollectionsToMq(ctx, &Collections{
CollectionsId: id,
Uuid: uuid,
}, "delete_collections_cache")
if err != nil {
return v1.ErrorDeleteCollectionsFailed("delete collections to mq failed: %s", err.Error())
}
return nil
}
func (r *CreationUseCase) DeleteCollectionsCache(ctx context.Context, id int32, uuid string) error {
return r.tm.ExecTx(ctx, func(ctx context.Context) error {
var err error
auth, err := r.repo.GetCollectionsAuth(ctx, id)
if err != nil {
return v1.ErrorDeleteCollectionsFailed("get collections auth failed: %s", err.Error())
}
err = r.repo.DeleteCollections(ctx, id, uuid)
if err != nil {
return v1.ErrorDeleteCollectionsFailed("delete collections failed: %s", err.Error())
}
err = r.repo.DeleteCollect(ctx, id)
if err != nil {
return v1.ErrorDeleteCollectionsFailed("delete collect failed: %s", err.Error())
}
err = r.repo.ReduceCreationUserCollections(ctx, auth, uuid)
if err != nil {
return v1.ErrorDeleteCollectionsFailed("delete creation user collections failed: %s", err.Error())
}
err = r.repo.DeleteCreationCache(ctx, id, auth, uuid)
if err != nil {
return v1.ErrorDeleteCollectionsFailed("delete creation cache failed: %s", err.Error())
}
return nil
})
}