Skip to content
This repository was archived by the owner on Feb 4, 2021. It is now read-only.

Commit df9776e

Browse files
committed
Impl ContributionConllectionServiceServer
1 parent 93372ca commit df9776e

File tree

2 files changed

+58
-6
lines changed

2 files changed

+58
-6
lines changed

app/run.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ func Run() error {
6161
server.NewRoleServiceServer(store),
6262
server.NewDepartmentServiceServer(store),
6363
server.NewInvitationServiceServer(store, cli),
64-
server.NewContributionConllectionServiceServer(store),
64+
server.NewContributionConllectionServiceServer(store, cfg),
6565
),
6666
)
6767

app/server/contribution_conllections_server.go

+57-5
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,13 @@ import (
44
"context"
55

66
"github.com/izumin5210/grapi/pkg/grapiserver"
7-
"google.golang.org/grpc/codes"
8-
"google.golang.org/grpc/status"
7+
"google.golang.org/grpc/grpclog"
98

109
api_pb "github.com/ProgrammingLab/prolab-accounts/api"
10+
"github.com/ProgrammingLab/prolab-accounts/app/config"
1111
"github.com/ProgrammingLab/prolab-accounts/app/di"
12+
"github.com/ProgrammingLab/prolab-accounts/infra/record"
13+
"github.com/ProgrammingLab/prolab-accounts/model"
1214
)
1315

1416
// ContributionConllectionServiceServer is a composite interface of api_pb.ContributionConllectionServiceServer and grapiserver.Server.
@@ -18,17 +20,67 @@ type ContributionConllectionServiceServer interface {
1820
}
1921

2022
// NewContributionConllectionServiceServer creates a new ContributionConllectionServiceServer instance.
21-
func NewContributionConllectionServiceServer(store di.StoreComponent) ContributionConllectionServiceServer {
23+
func NewContributionConllectionServiceServer(store di.StoreComponent, cfg *config.Config) ContributionConllectionServiceServer {
2224
return &contributionConllectionServiceServerImpl{
2325
StoreComponent: store,
26+
cfg: cfg,
2427
}
2528
}
2629

2730
type contributionConllectionServiceServerImpl struct {
2831
di.StoreComponent
32+
cfg *config.Config
2933
}
3034

3135
func (s *contributionConllectionServiceServerImpl) ListContributionConllections(ctx context.Context, req *api_pb.ListContributionConllectionsRequest) (*api_pb.ListContributionConllectionsResponse, error) {
32-
// TODO: Not yet implemented.
33-
return nil, status.Error(codes.Unimplemented, "TODO: You should implement it!")
36+
gs := s.GitHubStore(ctx)
37+
grpclog.Info(req.GetUsersCount())
38+
cols, err := gs.ListContributionCollections(int(req.GetUsersCount()))
39+
if err != nil {
40+
return nil, err
41+
}
42+
43+
resp := contributionConllectionsToResponse(cols, s.cfg)
44+
return &api_pb.ListContributionConllectionsResponse{
45+
ContributionConllections: resp,
46+
}, nil
47+
}
48+
49+
func contributionConllectionsToResponse(cols []*model.GitHubContributionCollection, cfg *config.Config) []*api_pb.ContributionConllection {
50+
resp := make([]*api_pb.ContributionConllection, 0, len(cols))
51+
for _, c := range cols {
52+
rc := contributionConllectionToResponse(c, cfg)
53+
if rc == nil {
54+
continue
55+
}
56+
resp = append(resp, rc)
57+
}
58+
59+
return resp
60+
}
61+
62+
func contributionConllectionToResponse(col *model.GitHubContributionCollection, cfg *config.Config) *api_pb.ContributionConllection {
63+
if len(col.Days) == 0 {
64+
return nil
65+
}
66+
67+
u := col.Days[0].R.User
68+
return &api_pb.ContributionConllection{
69+
User: userToResponse(u, false, cfg),
70+
TotalCount: int32(col.TotalCount),
71+
Days: contributionDaysToResponse(col.Days),
72+
}
73+
}
74+
75+
func contributionDaysToResponse(days []*record.GithubContributionDay) []*api_pb.ContributionDay {
76+
resp := make([]*api_pb.ContributionDay, 0, len(days))
77+
for _, d := range days {
78+
rd := &api_pb.ContributionDay{
79+
Date: timeToResponse(d.Date),
80+
Count: int32(d.Count),
81+
}
82+
resp = append(resp, rd)
83+
}
84+
85+
return resp
3486
}

0 commit comments

Comments
 (0)