-
Notifications
You must be signed in to change notification settings - Fork 1
/
round.go
101 lines (89 loc) · 2.7 KB
/
round.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
package handler
import (
"context"
"google.golang.org/protobuf/types/known/timestamppb"
"github.com/ScoreTrak/ScoreTrak/pkg/round"
"github.com/ScoreTrak/ScoreTrak/pkg/round/roundservice"
"github.com/golang/protobuf/ptypes/timestamp"
roundv1 "go.buf.build/library/go-grpc/scoretrak/scoretrakapis/scoretrak/round/v1"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
type RoundController struct {
svc roundservice.Serv
roundv1.UnimplementedRoundServiceServer
}
func (r RoundController) GetLastNonElapsingRound(ctx context.Context, _ *roundv1.GetLastNonElapsingRoundRequest) (*roundv1.GetLastNonElapsingRoundResponse, error) {
rnd, err := r.svc.GetLastNonElapsingRound(ctx)
if err != nil {
return nil, getErrorParser(err)
}
return &roundv1.GetLastNonElapsingRoundResponse{Round: ConvertRoundToRoundPb(rnd)}, nil
}
func (r RoundController) GetAll(ctx context.Context, _ *roundv1.GetAllRequest) (*roundv1.GetAllResponse, error) {
rnds, err := r.svc.GetAll(ctx)
if err != nil {
return nil, getErrorParser(err)
}
rndspb := make([]*roundv1.Round, 0, len(rnds))
for i := range rnds {
rndspb = append(rndspb, ConvertRoundToRoundPb(rnds[i]))
}
return &roundv1.GetAllResponse{Rounds: rndspb}, nil
}
func (r RoundController) GetByID(ctx context.Context, request *roundv1.GetByIDRequest) (*roundv1.GetByIDResponse, error) {
roundID := request.GetId()
if roundID == 0 {
return nil, status.Errorf(
codes.InvalidArgument,
"Round"+idNotSpecified,
)
}
rnd, err := r.svc.GetByID(ctx, roundID)
if err != nil {
return nil, getErrorParser(err)
}
return &roundv1.GetByIDResponse{Round: ConvertRoundToRoundPb(rnd)}, nil
}
func (r RoundController) GetLastRound(ctx context.Context, _ *roundv1.GetLastRoundRequest) (*roundv1.GetLastRoundResponse, error) {
rnd, err := r.svc.GetLastRound(ctx)
if err != nil {
return nil, getErrorParser(err)
}
return &roundv1.GetLastRoundResponse{Round: ConvertRoundToRoundPb(rnd)}, nil
}
func NewRoundController(svc roundservice.Serv) *RoundController {
return &RoundController{svc: svc}
}
func ConvertRoundPBtoRound(requireID bool, pb *roundv1.Round) (*round.Round, error) {
if pb.Id == 0 && requireID {
return nil, status.Error(
codes.InvalidArgument,
"ID is required",
)
}
tfs := pb.Finish.AsTime()
return &round.Round{
ID: pb.Id,
Start: pb.Start.AsTime(),
Note: pb.Note,
Err: pb.Err,
Finish: &tfs,
Checks: nil,
}, nil
}
func ConvertRoundToRoundPb(obj *round.Round) *roundv1.Round {
tss := timestamppb.New(obj.Start)
var tsf *timestamp.Timestamp
if obj.Finish != nil {
tsf = timestamppb.New(*obj.Finish)
}
return &roundv1.Round{
Id: obj.ID,
Start: tss,
Note: obj.Note,
Err: obj.Err,
Finish: tsf,
Checks: nil,
}
}