OSDN Git Service

new repo
[bytom/vapor.git] / vendor / github.com / go-kit / kit / examples / shipping / handling / service.go
1 // Package handling provides the use-case for registering incidents. Used by
2 // views facing the people handling the cargo along its route.
3 package handling
4
5 import (
6         "errors"
7         "time"
8
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"
13 )
14
15 // ErrInvalidArgument is returned when one or more arguments are invalid.
16 var ErrInvalidArgument = errors.New("invalid argument")
17
18 // EventHandler provides a means of subscribing to registered handling events.
19 type EventHandler interface {
20         CargoWasHandled(cargo.HandlingEvent)
21 }
22
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
29 }
30
31 type service struct {
32         handlingEventRepository cargo.HandlingEventRepository
33         handlingEventFactory    cargo.HandlingEventFactory
34         handlingEventHandler    EventHandler
35 }
36
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
41         }
42
43         e, err := s.handlingEventFactory.CreateHandlingEvent(time.Now(), completed, id, voyageNumber, loc, eventType)
44         if err != nil {
45                 return err
46         }
47
48         s.handlingEventRepository.Store(e)
49         s.handlingEventHandler.CargoWasHandled(e)
50
51         return nil
52 }
53
54 // NewService creates a handling event service with necessary dependencies.
55 func NewService(r cargo.HandlingEventRepository, f cargo.HandlingEventFactory, h EventHandler) Service {
56         return &service{
57                 handlingEventRepository: r,
58                 handlingEventFactory:    f,
59                 handlingEventHandler:    h,
60         }
61 }
62
63 type handlingEventHandler struct {
64         InspectionService inspection.Service
65 }
66
67 func (h *handlingEventHandler) CargoWasHandled(event cargo.HandlingEvent) {
68         h.InspectionService.InspectCargo(event.TrackingID)
69 }
70
71 // NewEventHandler returns a new instance of a EventHandler.
72 func NewEventHandler(s inspection.Service) EventHandler {
73         return &handlingEventHandler{
74                 InspectionService: s,
75         }
76 }