-
Notifications
You must be signed in to change notification settings - Fork 44
/
achievement.go
108 lines (97 loc) · 3.25 KB
/
achievement.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
package biz
import (
"context"
"github.com/go-kratos/kratos/v2/log"
"golang.org/x/sync/errgroup"
)
type AchievementRepo interface {
GetAchievementList(ctx context.Context, uuids []string) ([]*Achievement, error)
GetUserAchievement(ctx context.Context, uuid string) (*Achievement, error)
GetUserActive(ctx context.Context, uuid string) (*Active, error)
GetUserMedal(ctx context.Context, uuid string) (*Medal, error)
SetUserMedal(ctx context.Context, medal, uuid string) error
CancelUserMedalSet(ctx context.Context, medal, uuid string) error
AccessUserMedal(ctx context.Context, medal, uuid string) error
}
type AchievementUseCase struct {
repo AchievementRepo
creationRepo CreationRepo
commentRepo CommentRepo
re Recovery
log *log.Helper
}
func NewAchievementUseCase(repo AchievementRepo, creationRepo CreationRepo, commentRepo CommentRepo, re Recovery, logger log.Logger) *AchievementUseCase {
return &AchievementUseCase{
repo: repo,
creationRepo: creationRepo,
commentRepo: commentRepo,
re: re,
log: log.NewHelper(log.With(logger, "module", "bff/biz/AchievementUseCase")),
}
}
func (r *AchievementUseCase) GetAchievementList(ctx context.Context, uuids []string) ([]*Achievement, error) {
return r.repo.GetAchievementList(ctx, uuids)
}
func (r *AchievementUseCase) GetUserAchievement(ctx context.Context, uuid string) (*Achievement, error) {
return r.repo.GetUserAchievement(ctx, uuid)
}
func (r *AchievementUseCase) GetUserMedal(ctx context.Context, uuid string) (*Medal, error) {
return r.repo.GetUserMedal(ctx, uuid)
}
func (r *AchievementUseCase) GetUserMedalProgress(ctx context.Context) (*MedalProgress, error) {
uuid := ctx.Value("uuid").(string)
mp := &MedalProgress{}
g, _ := errgroup.WithContext(ctx)
g.Go(r.re.GroupRecover(ctx, func(ctx context.Context) error {
achievement, err := r.repo.GetUserAchievement(ctx, uuid)
if err != nil {
return err
}
mp.Agree = achievement.Agree
mp.View = achievement.View
return nil
}))
g.Go(r.re.GroupRecover(ctx, func(ctx context.Context) error {
active, err := r.repo.GetUserActive(ctx, uuid)
if err != nil {
return err
}
mp.ActiveAgree = active.Agree
return nil
}))
g.Go(r.re.GroupRecover(ctx, func(ctx context.Context) error {
creation, err := r.creationRepo.GetCreationUser(ctx, uuid)
if err != nil {
return err
}
mp.Article = creation.Article
mp.Talk = creation.Talk
mp.Collect = creation.Collect
return nil
}))
g.Go(r.re.GroupRecover(ctx, func(ctx context.Context) error {
commentUser, err := r.commentRepo.GetCommentUser(ctx, uuid)
if err != nil {
return err
}
mp.Comment = commentUser.Comment
return nil
}))
err := g.Wait()
if err != nil {
return nil, err
}
return mp, nil
}
func (r *AchievementUseCase) SetUserMedal(ctx context.Context, medal string) error {
uuid := ctx.Value("uuid").(string)
return r.repo.SetUserMedal(ctx, medal, uuid)
}
func (r *AchievementUseCase) CancelUserMedalSet(ctx context.Context, medal string) error {
uuid := ctx.Value("uuid").(string)
return r.repo.CancelUserMedalSet(ctx, medal, uuid)
}
func (r *AchievementUseCase) AccessUserMedal(ctx context.Context, medal string) error {
uuid := ctx.Value("uuid").(string)
return r.repo.AccessUserMedal(ctx, medal, uuid)
}