OSDN Git Service

new repo
[bytom/vapor.git] / vendor / github.com / go-kit / kit / examples / shipping / booking / endpoint.go
1 package booking
2
3 import (
4         "context"
5         "time"
6
7         "github.com/go-kit/kit/endpoint"
8
9         "github.com/go-kit/kit/examples/shipping/cargo"
10         "github.com/go-kit/kit/examples/shipping/location"
11 )
12
13 type bookCargoRequest struct {
14         Origin          location.UNLocode
15         Destination     location.UNLocode
16         ArrivalDeadline time.Time
17 }
18
19 type bookCargoResponse struct {
20         ID  cargo.TrackingID `json:"tracking_id,omitempty"`
21         Err error            `json:"error,omitempty"`
22 }
23
24 func (r bookCargoResponse) error() error { return r.Err }
25
26 func makeBookCargoEndpoint(s Service) endpoint.Endpoint {
27         return func(ctx context.Context, request interface{}) (interface{}, error) {
28                 req := request.(bookCargoRequest)
29                 id, err := s.BookNewCargo(req.Origin, req.Destination, req.ArrivalDeadline)
30                 return bookCargoResponse{ID: id, Err: err}, nil
31         }
32 }
33
34 type loadCargoRequest struct {
35         ID cargo.TrackingID
36 }
37
38 type loadCargoResponse struct {
39         Cargo *Cargo `json:"cargo,omitempty"`
40         Err   error  `json:"error,omitempty"`
41 }
42
43 func (r loadCargoResponse) error() error { return r.Err }
44
45 func makeLoadCargoEndpoint(s Service) endpoint.Endpoint {
46         return func(ctx context.Context, request interface{}) (interface{}, error) {
47                 req := request.(loadCargoRequest)
48                 c, err := s.LoadCargo(req.ID)
49                 return loadCargoResponse{Cargo: &c, Err: err}, nil
50         }
51 }
52
53 type requestRoutesRequest struct {
54         ID cargo.TrackingID
55 }
56
57 type requestRoutesResponse struct {
58         Routes []cargo.Itinerary `json:"routes,omitempty"`
59         Err    error             `json:"error,omitempty"`
60 }
61
62 func (r requestRoutesResponse) error() error { return r.Err }
63
64 func makeRequestRoutesEndpoint(s Service) endpoint.Endpoint {
65         return func(ctx context.Context, request interface{}) (interface{}, error) {
66                 req := request.(requestRoutesRequest)
67                 itin := s.RequestPossibleRoutesForCargo(req.ID)
68                 return requestRoutesResponse{Routes: itin, Err: nil}, nil
69         }
70 }
71
72 type assignToRouteRequest struct {
73         ID        cargo.TrackingID
74         Itinerary cargo.Itinerary
75 }
76
77 type assignToRouteResponse struct {
78         Err error `json:"error,omitempty"`
79 }
80
81 func (r assignToRouteResponse) error() error { return r.Err }
82
83 func makeAssignToRouteEndpoint(s Service) endpoint.Endpoint {
84         return func(ctx context.Context, request interface{}) (interface{}, error) {
85                 req := request.(assignToRouteRequest)
86                 err := s.AssignCargoToRoute(req.ID, req.Itinerary)
87                 return assignToRouteResponse{Err: err}, nil
88         }
89 }
90
91 type changeDestinationRequest struct {
92         ID          cargo.TrackingID
93         Destination location.UNLocode
94 }
95
96 type changeDestinationResponse struct {
97         Err error `json:"error,omitempty"`
98 }
99
100 func (r changeDestinationResponse) error() error { return r.Err }
101
102 func makeChangeDestinationEndpoint(s Service) endpoint.Endpoint {
103         return func(ctx context.Context, request interface{}) (interface{}, error) {
104                 req := request.(changeDestinationRequest)
105                 err := s.ChangeDestination(req.ID, req.Destination)
106                 return changeDestinationResponse{Err: err}, nil
107         }
108 }
109
110 type listCargosRequest struct{}
111
112 type listCargosResponse struct {
113         Cargos []Cargo `json:"cargos,omitempty"`
114         Err    error   `json:"error,omitempty"`
115 }
116
117 func (r listCargosResponse) error() error { return r.Err }
118
119 func makeListCargosEndpoint(s Service) endpoint.Endpoint {
120         return func(ctx context.Context, request interface{}) (interface{}, error) {
121                 _ = request.(listCargosRequest)
122                 return listCargosResponse{Cargos: s.Cargos(), Err: nil}, nil
123         }
124 }
125
126 type listLocationsRequest struct {
127 }
128
129 type listLocationsResponse struct {
130         Locations []Location `json:"locations,omitempty"`
131         Err       error      `json:"error,omitempty"`
132 }
133
134 func makeListLocationsEndpoint(s Service) endpoint.Endpoint {
135         return func(ctx context.Context, request interface{}) (interface{}, error) {
136                 _ = request.(listLocationsRequest)
137                 return listLocationsResponse{Locations: s.Locations(), Err: nil}, nil
138         }
139 }