/
statistics.go
107 lines (95 loc) · 2.62 KB
/
statistics.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
package statistics
import (
"context"
"github.com/GoodCodingFriends/animekai/annict"
"github.com/GoodCodingFriends/animekai/api"
"github.com/GoodCodingFriends/animekai/resource"
"github.com/morikuni/failure"
"golang.org/x/sync/errgroup"
)
// Service provides animekai statistics.
type Service interface {
// GetDashboard returns stuffs for displaying animekai dashboard.
GetDashboard(ctx context.Context, req *api.GetDashboardRequest) (*api.GetDashboardResponse, error)
// ListWorks returns watching/watched works according to req.
ListWorks(ctx context.Context, req *api.ListWorksRequest) (*api.ListWorksResponse, error)
}
type service struct {
annict annict.Service
}
// New instantiates a new Service.
func New(annict annict.Service) Service {
return &service{
annict: annict,
}
}
func (s *service) GetDashboard(ctx context.Context, req *api.GetDashboardRequest) (*api.GetDashboardResponse, error) {
if err := validateGetDashboardRequest(req); err != nil {
return nil, failure.Wrap(err)
}
var (
profile *resource.Profile
watchingWorks []*resource.Work
watchedWorks []*resource.Work
nextPageToken string
eg errgroup.Group
)
eg.Go(func() error {
p, err := s.annict.GetProfile(ctx)
if err != nil {
return failure.Wrap(err)
}
profile = p
return nil
})
eg.Go(func() error {
w, _, err := s.annict.ListWorks(ctx, annict.StatusStateWatching, "", 100)
if err != nil {
return failure.Wrap(err)
}
watchingWorks = w
return nil
})
eg.Go(func() error {
w, cursor, err := s.annict.ListWorks(ctx, annict.StatusStateWatched, "", req.WorkPageSize)
if err != nil {
return failure.Wrap(err)
}
watchedWorks = w
nextPageToken = cursor
return nil
})
if err := eg.Wait(); err != nil {
return nil, failure.Wrap(err)
}
return &api.GetDashboardResponse{
Dashboard: &resource.Dashboard{
Profile: profile,
WatchingWorks: watchingWorks,
WatchedWorks: watchedWorks,
},
WorkNextPageToken: nextPageToken,
}, nil
}
func (s *service) ListWorks(ctx context.Context, req *api.ListWorksRequest) (*api.ListWorksResponse, error) {
if err := validateListWorksRequest(req); err != nil {
return nil, failure.Wrap(err)
}
var state annict.StatusState
switch req.State {
case api.WorkState_WATCHING:
state = annict.StatusStateWatching
case api.WorkState_WATCHED:
state = annict.StatusStateWatched
default:
state = annict.StatusStateNoState
}
works, nextPageToken, err := s.annict.ListWorks(ctx, state, req.PageToken, req.PageSize)
if err != nil {
return nil, failure.Wrap(err)
}
return &api.ListWorksResponse{
Works: works,
NextPageToken: nextPageToken,
}, nil
}