-
Notifications
You must be signed in to change notification settings - Fork 0
/
handler.go
39 lines (33 loc) · 862 Bytes
/
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
package main
import (
pb "github.com/gregory-vc/vessel-service/proto/vessel"
"golang.org/x/net/context"
"gopkg.in/mgo.v2"
)
// Our gRPC service handler
type service struct {
session *mgo.Session
}
func (s *service) GetRepo() Repository {
return &VesselRepository{s.session.Clone()}
}
func (s *service) FindAvailable(ctx context.Context, req *pb.Specification, res *pb.Response) error {
defer s.GetRepo().Close()
// Find the next available vessel
vessel, err := s.GetRepo().FindAvailable(req)
if err != nil {
return err
}
// Set the vessel as part of the response message type
res.Vessel = vessel
return nil
}
func (s *service) Create(ctx context.Context, req *pb.Vessel, res *pb.Response) error {
defer s.GetRepo().Close()
if err := s.GetRepo().Create(req); err != nil {
return err
}
res.Vessel = req
res.Created = true
return nil
}