1ead9c18cac767cf7420a19e9a5a5df1ed753ae6.svn-base 1.3 KB

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