-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandler.go
63 lines (49 loc) · 1.39 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
61
62
63
// shippy-consignment-service/handler.go
package main
import (
"context"
"log"
"gopkg.in/mgo.v2"
pb "github.com/seiji-thirdbridge/shippy-consignment-service/proto/consignment"
vesselProto "github.com/seiji-thirdbridge/shippy-vessel-service/proto/vessel"
)
type service struct {
session *mgo.Session
vesselClient vesselProto.VesselServiceClient
}
func (s *service) getRepo() repository {
return &ConsignmentRepository{s.session.Clone()}
}
// CreateConsignment - Stores the provided consignment
func (s *service) CreateConsignment(ctx context.Context, req *pb.Consignment, res *pb.Response) error {
repo := s.getRepo()
defer repo.Close()
vesselResponse, err := s.vesselClient.FindAvailable(context.Background(), &vesselProto.Specification{
MaxWeight: req.Weight,
Capacity: int32(len(req.Containers)),
})
if err != nil {
return err
}
log.Printf("Found vessel: %s\n", vesselResponse.Vessel.Name)
req.VesselId = vesselResponse.Vessel.Id
// Save the consignment
err = repo.Create(req)
if err != nil {
return err
}
res.Created = true
res.Consignment = req
return nil
}
// GetConsignments - Get a list of all consignments
func (s *service) GetConsignments(ctx context.Context, req *pb.GetRequest, res *pb.Response) error {
repo := s.getRepo()
defer repo.Close()
consignments, err := repo.GetAll()
if err != nil {
return err
}
res.Consignments = consignments
return nil
}