This repository has been archived by the owner on May 17, 2022. It is now read-only.
/
server_create.go
76 lines (66 loc) · 1.96 KB
/
server_create.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
package api
import (
"context"
"github.com/FrancescoIlario/usplay/internal/services/report/storage"
"github.com/FrancescoIlario/usplay/pkg/services/activitycomm"
"github.com/FrancescoIlario/usplay/pkg/services/reportcomm"
"github.com/golang/protobuf/ptypes"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
func (s *reportServer) Create(ctx context.Context, req *reportcomm.CreateReportRequest) (*reportcomm.CreateReportReply, error) {
period := req.GetPeriod()
if period == nil || period.GetFrom() == nil || period.GetTo() == nil {
return nil, status.Errorf(codes.InvalidArgument, `error creating report: provided period is null or invalid`)
}
rep, err := s.activityCli.ListInInterval(ctx,
&activitycomm.ListInIntervalActivitiesRequest{
Period: &activitycomm.Interval{
From: period.GetFrom(),
To: period.GetTo(),
},
})
if err != nil {
return nil, err
}
acts := make(storage.Activities, len(rep.Activities))
if len(rep.GetActivities()) > 0 {
for idx, act := range rep.Activities {
from, _ := ptypes.Timestamp(act.GetPeriod().GetFrom())
to, _ := ptypes.Timestamp(act.GetPeriod().GetTo())
acts[idx] = storage.Activity{
Order: storage.Order{
ID: act.GetOrder().GetId(),
Name: act.GetOrder().GetName(),
},
ID: act.GetId(),
Period: storage.Interval{
From: from,
To: to,
},
Type: storage.ActivityType{
ID: act.GetActType().GetId(),
Code: int(act.GetActType().GetCode()),
Name: act.GetActType().GetName(),
},
}
}
}
from, _ := ptypes.Timestamp(req.GetPeriod().GetFrom())
to, _ := ptypes.Timestamp(req.GetPeriod().GetTo())
report := storage.Report{
Name: req.GetName(),
Activities: acts,
Period: storage.Interval{
From: from,
To: to,
},
}
id, err := s.repo.Create(ctx, report)
if err != nil {
return nil, status.Errorf(codes.Internal, "error creating report: %v", err)
}
return &reportcomm.CreateReportReply{
Id: id.String(),
}, nil
}