OSDN Git Service

delete miner
[bytom/vapor.git] / vendor / github.com / go-kit / kit / examples / shipping / inspection / inspection.go
1 // Package inspection provides means to inspect cargos.
2 package inspection
3
4 import (
5         "github.com/go-kit/kit/examples/shipping/cargo"
6 )
7
8 // EventHandler provides means of subscribing to inspection events.
9 type EventHandler interface {
10         CargoWasMisdirected(*cargo.Cargo)
11         CargoHasArrived(*cargo.Cargo)
12 }
13
14 // Service provides cargo inspection operations.
15 type Service interface {
16         // InspectCargo inspects cargo and send relevant notifications to
17         // interested parties, for example if a cargo has been misdirected, or
18         // unloaded at the final destination.
19         InspectCargo(id cargo.TrackingID)
20 }
21
22 type service struct {
23         cargos  cargo.Repository
24         events  cargo.HandlingEventRepository
25         handler EventHandler
26 }
27
28 // TODO: Should be transactional
29 func (s *service) InspectCargo(id cargo.TrackingID) {
30         c, err := s.cargos.Find(id)
31         if err != nil {
32                 return
33         }
34
35         h := s.events.QueryHandlingHistory(id)
36
37         c.DeriveDeliveryProgress(h)
38
39         if c.Delivery.IsMisdirected {
40                 s.handler.CargoWasMisdirected(c)
41         }
42
43         if c.Delivery.IsUnloadedAtDestination {
44                 s.handler.CargoHasArrived(c)
45         }
46
47         s.cargos.Store(c)
48 }
49
50 // NewService creates a inspection service with necessary dependencies.
51 func NewService(cargos cargo.Repository, events cargo.HandlingEventRepository, handler EventHandler) Service {
52         return &service{cargos, events, handler}
53 }