forked from go-kit/kit
/
inspection.go
51 lines (40 loc) · 1.47 KB
/
inspection.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
// Package inspection provides means to inspect cargos.
package inspection
import "github.com/go-kit/kit/examples/shipping/cargo"
// EventHandler provides means of subscribing to inspection events.
type EventHandler interface {
CargoWasMisdirected(*cargo.Cargo)
CargoHasArrived(*cargo.Cargo)
}
// Service provides cargo inspection operations.
type Service interface {
// InspectCargo inspects cargo and send relevant notifications to
// interested parties, for example if a cargo has been misdirected, or
// unloaded at the final destination.
InspectCargo(trackingID cargo.TrackingID)
}
type service struct {
cargoRepository cargo.Repository
handlingEventRepository cargo.HandlingEventRepository
cargoEventHandler EventHandler
}
// TODO: Should be transactional
func (s *service) InspectCargo(trackingID cargo.TrackingID) {
c, err := s.cargoRepository.Find(trackingID)
if err != nil {
return
}
h := s.handlingEventRepository.QueryHandlingHistory(trackingID)
c.DeriveDeliveryProgress(h)
if c.Delivery.IsMisdirected {
s.cargoEventHandler.CargoWasMisdirected(c)
}
if c.Delivery.IsUnloadedAtDestination {
s.cargoEventHandler.CargoHasArrived(c)
}
s.cargoRepository.Store(c)
}
// NewService creates a inspection service with necessary dependencies.
func NewService(cargoRepository cargo.Repository, handlingEventRepository cargo.HandlingEventRepository, eventHandler EventHandler) Service {
return &service{cargoRepository, handlingEventRepository, eventHandler}
}