1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374 |
- package handling
- import (
- "errors"
- "time"
- "github.com/longjoy/micro-go-course/section19/cargo/inspection"
- shipping "github.com/longjoy/micro-go-course/section19/cargo/model"
- )
- var ErrInvalidArgument = errors.New("invalid argument")
- type EventHandler interface {
- CargoWasHandled(shipping.HandlingEvent)
- }
- type Service interface {
-
-
- RegisterHandlingEvent(completed time.Time, id shipping.TrackingID, voyageNumber shipping.VoyageNumber,
- unLocode shipping.UNLocode, eventType shipping.HandlingEventType) (bool, error)
- }
- type service struct {
- handlingEventRepository shipping.HandlingEventRepository
- handlingEventFactory shipping.HandlingEventFactory
- handlingEventHandler EventHandler
- }
- func (s *service) RegisterHandlingEvent(completed time.Time, id shipping.TrackingID, voyageNumber shipping.VoyageNumber,
- loc shipping.UNLocode, eventType shipping.HandlingEventType) (bool, error) {
- if completed.IsZero() || id == "" || loc == "" || eventType == shipping.NotHandled {
- return false, ErrInvalidArgument
- }
- e, err := s.handlingEventFactory.CreateHandlingEvent(time.Now(), completed, id, voyageNumber, loc, eventType)
- if err != nil {
- return false, err
- }
- s.handlingEventRepository.Store(e)
- s.handlingEventHandler.CargoWasHandled(e)
- return true, nil
- }
- func NewService(r shipping.HandlingEventRepository, f shipping.HandlingEventFactory, h EventHandler) Service {
- return &service{
- handlingEventRepository: r,
- handlingEventFactory: f,
- handlingEventHandler: h,
- }
- }
- type handlingEventHandler struct {
- InspectionService inspection.Service
- }
- func (h *handlingEventHandler) CargoWasHandled(event shipping.HandlingEvent) {
- h.InspectionService.InspectCargo(event.TrackingID)
- }
- func NewEventHandler(s inspection.Service) EventHandler {
- return &handlingEventHandler{
- InspectionService: s,
- }
- }
|