-
Notifications
You must be signed in to change notification settings - Fork 1
/
handler.go
60 lines (50 loc) · 1.67 KB
/
handler.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
// shippy-service-consignment/handler.go
package main
import (
"context"
"log"
pb "github.com/chanho3114/shippy-service-consignment/proto/consignment"
vesselProto "github.com/chanho3114/shippy-service-vessel/proto/vessel"
"github.com/pkg/errors"
)
type handler struct {
repository
vesselClient vesselProto.VesselService
}
// CreateConsignment - we created just one method on our service,
// which is a create method, which takes a context and a request as an
// argument, these are handled by the gRPC server.
func (s *handler) CreateConsignment(ctx context.Context, req *pb.Consignment, res *pb.Response) error {
// Here we call a client instance of our vessel service with our consignment weight,
// and the amount of containers as the capacity value
vesselResponse, err := s.vesselClient.FindAvailable(context.Background(), &vesselProto.Specification{
MaxWeight: req.Weight,
Capacity: int32(len(req.Containers)),
})
log.Printf("s.vesselClient.FindAvailable Response : %v", vesselResponse)
if vesselResponse == nil {
return errors.New("error fetching vessel, returned nil")
}
if err != nil {
return err
}
// We set the VesselId as the vessel we got back from our
// vessel service
req.VesselId = vesselResponse.Vessel.Id
// Save our consignment
if err = s.repository.Create(ctx, MarshalConsignment(req)); err != nil {
return err
}
res.Created = true
res.Consignment = req
return nil
}
// GetConsignments -
func (s *handler) GetConsignments(ctx context.Context, req *pb.GetRequest, res *pb.Response) error {
consignments, err := s.repository.GetAll(ctx)
if err != nil {
return err
}
res.Consignments = UnmarshalConsignmentCollection(consignments)
return nil
}