1 // Package handling provides the use-case for registering incidents. Used by
2 // views facing the people handling the cargo along its route.
9 "github.com/go-kit/kit/examples/shipping/cargo"
10 "github.com/go-kit/kit/examples/shipping/inspection"
11 "github.com/go-kit/kit/examples/shipping/location"
12 "github.com/go-kit/kit/examples/shipping/voyage"
15 // ErrInvalidArgument is returned when one or more arguments are invalid.
16 var ErrInvalidArgument = errors.New("invalid argument")
18 // EventHandler provides a means of subscribing to registered handling events.
19 type EventHandler interface {
20 CargoWasHandled(cargo.HandlingEvent)
23 // Service provides handling operations.
24 type Service interface {
25 // RegisterHandlingEvent registers a handling event in the system, and
26 // notifies interested parties that a cargo has been handled.
27 RegisterHandlingEvent(completed time.Time, id cargo.TrackingID, voyageNumber voyage.Number,
28 unLocode location.UNLocode, eventType cargo.HandlingEventType) error
32 handlingEventRepository cargo.HandlingEventRepository
33 handlingEventFactory cargo.HandlingEventFactory
34 handlingEventHandler EventHandler
37 func (s *service) RegisterHandlingEvent(completed time.Time, id cargo.TrackingID, voyageNumber voyage.Number,
38 loc location.UNLocode, eventType cargo.HandlingEventType) error {
39 if completed.IsZero() || id == "" || loc == "" || eventType == cargo.NotHandled {
40 return ErrInvalidArgument
43 e, err := s.handlingEventFactory.CreateHandlingEvent(time.Now(), completed, id, voyageNumber, loc, eventType)
48 s.handlingEventRepository.Store(e)
49 s.handlingEventHandler.CargoWasHandled(e)
54 // NewService creates a handling event service with necessary dependencies.
55 func NewService(r cargo.HandlingEventRepository, f cargo.HandlingEventFactory, h EventHandler) Service {
57 handlingEventRepository: r,
58 handlingEventFactory: f,
59 handlingEventHandler: h,
63 type handlingEventHandler struct {
64 InspectionService inspection.Service
67 func (h *handlingEventHandler) CargoWasHandled(event cargo.HandlingEvent) {
68 h.InspectionService.InspectCargo(event.TrackingID)
71 // NewEventHandler returns a new instance of a EventHandler.
72 func NewEventHandler(s inspection.Service) EventHandler {
73 return &handlingEventHandler{