9148e3850b495dc495ef061e22eebd8bde662151.svn-base 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. package endpoint
  2. import (
  3. "context"
  4. "github.com/go-kit/kit/endpoint"
  5. shipping "github.com/longjoy/micro-go-course/section19/cargo/model"
  6. "github.com/longjoy/micro-go-course/section19/cargo/service/booking"
  7. "github.com/longjoy/micro-go-course/section19/cargo/service/handling"
  8. "time"
  9. )
  10. type CargoEndpoints struct {
  11. BookCargoEndpoint endpoint.Endpoint
  12. LoadCargoEndpoint endpoint.Endpoint
  13. AssignCargoToRouteEndpoint endpoint.Endpoint
  14. ChangeDestinationEndpoint endpoint.Endpoint
  15. CargosEndpoint endpoint.Endpoint
  16. LocationsEndpoint endpoint.Endpoint
  17. RegisterHandlingEventEndpoint endpoint.Endpoint
  18. //TrackEndpoint endpoint.Endpoint
  19. }
  20. type RegisterHandlingEventRequest struct {
  21. Completed time.Time
  22. Id shipping.TrackingID
  23. VoyageNumber shipping.VoyageNumber
  24. UnLocode shipping.UNLocode
  25. EventType shipping.HandlingEventType
  26. }
  27. type RegisterHandlingEventResponse struct {
  28. Res bool `json:"res"`
  29. }
  30. func RegisterHandlingEventEndpoint(handlingService handling.Service) endpoint.Endpoint {
  31. return func(ctx context.Context, request interface{}) (response interface{}, err error) {
  32. req := request.(*RegisterHandlingEventRequest)
  33. res, err := handlingService.RegisterHandlingEvent(req.Completed, req.Id, req.VoyageNumber, req.UnLocode, req.EventType)
  34. return &RegisterHandlingEventResponse{Res: res}, err
  35. }
  36. }
  37. type LocationsResponse struct {
  38. Locations []booking.Location `json:"locations"`
  39. }
  40. func LocationsEndpoint(bookService booking.Service) endpoint.Endpoint {
  41. return func(ctx context.Context, request interface{}) (response interface{}, err error) {
  42. res := bookService.Locations()
  43. return &LocationsResponse{Locations: res}, err
  44. }
  45. }
  46. type CargosResponse struct {
  47. Cargos []booking.Cargo `json:"cargos"`
  48. }
  49. func CargosEndpoint(bookService booking.Service) endpoint.Endpoint {
  50. return func(ctx context.Context, request interface{}) (response interface{}, err error) {
  51. res := bookService.Cargos()
  52. return &CargosResponse{Cargos: res}, err
  53. }
  54. }
  55. type ChangeDestinationRequest struct {
  56. Id shipping.TrackingID
  57. Destination shipping.UNLocode
  58. }
  59. type ChangeDestinationResponse struct {
  60. Res bool `json:"res"`
  61. }
  62. func ChangeDestinationEndpoint(bookService booking.Service) endpoint.Endpoint {
  63. return func(ctx context.Context, request interface{}) (response interface{}, err error) {
  64. req := request.(*ChangeDestinationRequest)
  65. res, err := bookService.ChangeDestination(req.Id, req.Destination)
  66. return &ChangeDestinationResponse{Res: res}, err
  67. }
  68. }
  69. type AssignCargoToRouteRequest struct {
  70. Id shipping.TrackingID
  71. Itinerary shipping.Itinerary
  72. }
  73. type AssignCargoToRouteResponse struct {
  74. Res bool `json:"res"`
  75. }
  76. func AssignCargoToRouteEndpoint(bookService booking.Service) endpoint.Endpoint {
  77. return func(ctx context.Context, request interface{}) (response interface{}, err error) {
  78. req := request.(*AssignCargoToRouteRequest)
  79. res, err := bookService.AssignCargoToRoute(req.Id, req.Itinerary)
  80. return &AssignCargoToRouteResponse{Res: res}, err
  81. }
  82. }
  83. type LoadCargoRequest struct {
  84. Id shipping.TrackingID
  85. }
  86. type LoadCargoResponse struct {
  87. Cargo booking.Cargo `json:"cargo"`
  88. }
  89. func MakeLoadCargoEndpoint(bookService booking.Service) endpoint.Endpoint {
  90. return func(ctx context.Context, request interface{}) (response interface{}, err error) {
  91. req := request.(*LoadCargoRequest)
  92. cargo, err := bookService.LoadCargo(req.Id)
  93. return &LoadCargoResponse{Cargo: cargo}, err
  94. }
  95. }
  96. type BookCargoRequest struct {
  97. Origin shipping.UNLocode
  98. Destination shipping.UNLocode
  99. Deadline time.Time
  100. }
  101. type BookCargoResponse struct {
  102. TrackingID shipping.TrackingID `json:"tracking_id"`
  103. }
  104. type LocationsRequest struct {
  105. }
  106. func MakeBookCargoEndpoint(bookService booking.Service) endpoint.Endpoint {
  107. return func(ctx context.Context, request interface{}) (response interface{}, err error) {
  108. req := request.(*BookCargoRequest)
  109. trackingID, err := bookService.BookNewCargo(req.Origin, req.Destination, req.Deadline)
  110. return &BookCargoResponse{TrackingID: trackingID}, err
  111. }
  112. }
  113. /*// UserEndpoint define endpoint
  114. func (ce *CargoEndpoints) BookNewCargo(ctx context.Context, origin shipping.UNLocode, destination shipping.UNLocode,
  115. deadline time.Time) (shipping.TrackingID, error) {
  116. if origin == "" || destination == "" || deadline.IsZero() {
  117. return "", booking.ErrInvalidArgument
  118. }
  119. id := shipping.NextTrackingID()
  120. rs := shipping.RouteSpecification{
  121. Origin: origin,
  122. Destination: destination,
  123. ArrivalDeadline: deadline,
  124. }
  125. c := shipping.NewCargo(id, rs)
  126. if _, err := s.cargos.Store(c); err != nil {
  127. return "", err
  128. }
  129. return c.TrackingID, nil
  130. }*/