1package booking
2
3import (
4	"time"
5
6	"github.com/go-kit/kit/log"
7
8	"github.com/go-kit/kit/examples/shipping/cargo"
9	"github.com/go-kit/kit/examples/shipping/location"
10)
11
12type loggingService struct {
13	logger log.Logger
14	Service
15}
16
17// NewLoggingService returns a new instance of a logging Service.
18func NewLoggingService(logger log.Logger, s Service) Service {
19	return &loggingService{logger, s}
20}
21
22func (s *loggingService) BookNewCargo(origin location.UNLocode, destination location.UNLocode, deadline time.Time) (id cargo.TrackingID, err error) {
23	defer func(begin time.Time) {
24		s.logger.Log(
25			"method", "book",
26			"origin", origin,
27			"destination", destination,
28			"arrival_deadline", deadline,
29			"took", time.Since(begin),
30			"err", err,
31		)
32	}(time.Now())
33	return s.Service.BookNewCargo(origin, destination, deadline)
34}
35
36func (s *loggingService) LoadCargo(id cargo.TrackingID) (c Cargo, err error) {
37	defer func(begin time.Time) {
38		s.logger.Log(
39			"method", "load",
40			"tracking_id", id,
41			"took", time.Since(begin),
42			"err", err,
43		)
44	}(time.Now())
45	return s.Service.LoadCargo(id)
46}
47
48func (s *loggingService) RequestPossibleRoutesForCargo(id cargo.TrackingID) []cargo.Itinerary {
49	defer func(begin time.Time) {
50		s.logger.Log(
51			"method", "request_routes",
52			"tracking_id", id,
53			"took", time.Since(begin),
54		)
55	}(time.Now())
56	return s.Service.RequestPossibleRoutesForCargo(id)
57}
58
59func (s *loggingService) AssignCargoToRoute(id cargo.TrackingID, itinerary cargo.Itinerary) (err error) {
60	defer func(begin time.Time) {
61		s.logger.Log(
62			"method", "assign_to_route",
63			"tracking_id", id,
64			"took", time.Since(begin),
65			"err", err,
66		)
67	}(time.Now())
68	return s.Service.AssignCargoToRoute(id, itinerary)
69}
70
71func (s *loggingService) ChangeDestination(id cargo.TrackingID, l location.UNLocode) (err error) {
72	defer func(begin time.Time) {
73		s.logger.Log(
74			"method", "change_destination",
75			"tracking_id", id,
76			"destination", l,
77			"took", time.Since(begin),
78			"err", err,
79		)
80	}(time.Now())
81	return s.Service.ChangeDestination(id, l)
82}
83
84func (s *loggingService) Cargos() []Cargo {
85	defer func(begin time.Time) {
86		s.logger.Log(
87			"method", "list_cargos",
88			"took", time.Since(begin),
89		)
90	}(time.Now())
91	return s.Service.Cargos()
92}
93
94func (s *loggingService) Locations() []Location {
95	defer func(begin time.Time) {
96		s.logger.Log(
97			"method", "list_locations",
98			"took", time.Since(begin),
99		)
100	}(time.Now())
101	return s.Service.Locations()
102}
103