forked from marcusolsson/goddd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
service.go
76 lines (62 loc) · 2.33 KB
/
service.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
// Package handling provides the use-case for registering incidents. Used by
// views facing the people handling the cargo along its route.
package handling
import (
"errors"
"time"
"github.com/marcusolsson/goddd/cargo"
"github.com/marcusolsson/goddd/inspection"
"github.com/marcusolsson/goddd/location"
"github.com/marcusolsson/goddd/voyage"
)
// ErrInvalidArgument is returned when one or more arguments are invalid.
var ErrInvalidArgument = errors.New("invalid argument")
// EventHandler provides a means of subscribing to registered handling events.
type EventHandler interface {
CargoWasHandled(cargo.HandlingEvent)
}
// Service provides handling operations.
type Service interface {
// RegisterHandlingEvent registers a handling event in the system, and
// notifies interested parties that a cargo has been handled.
RegisterHandlingEvent(completed time.Time, id cargo.TrackingID, voyageNumber voyage.Number,
unLocode location.UNLocode, eventType cargo.HandlingEventType) error
}
type service struct {
handlingEventRepository cargo.HandlingEventRepository
handlingEventFactory cargo.HandlingEventFactory
handlingEventHandler EventHandler
}
func (s *service) RegisterHandlingEvent(completed time.Time, id cargo.TrackingID, voyageNumber voyage.Number,
loc location.UNLocode, eventType cargo.HandlingEventType) error {
if completed.IsZero() || id == "" || loc == "" || eventType == cargo.NotHandled {
return ErrInvalidArgument
}
e, err := s.handlingEventFactory.CreateHandlingEvent(time.Now(), completed, id, voyageNumber, loc, eventType)
if err != nil {
return err
}
s.handlingEventRepository.Store(e)
s.handlingEventHandler.CargoWasHandled(e)
return nil
}
// NewService creates a handling event service with necessary dependencies.
func NewService(r cargo.HandlingEventRepository, f cargo.HandlingEventFactory, h EventHandler) Service {
return &service{
handlingEventRepository: r,
handlingEventFactory: f,
handlingEventHandler: h,
}
}
type handlingEventHandler struct {
InspectionService inspection.Service
}
func (h *handlingEventHandler) CargoWasHandled(event cargo.HandlingEvent) {
h.InspectionService.InspectCargo(event.TrackingID)
}
// NewEventHandler returns a new instance of a EventHandler.
func NewEventHandler(s inspection.Service) EventHandler {
return &handlingEventHandler{
InspectionService: s,
}
}