-
Notifications
You must be signed in to change notification settings - Fork 84
/
handling.go
68 lines (57 loc) · 1.72 KB
/
handling.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
package endpoint
import (
"context"
"encoding/json"
"net/http"
"time"
"github.com/go-chi/chi"
kitlog "github.com/go-kit/kit/log"
shipping "github.com/longjoy/micro-go-course/section19/cargo/model"
"github.com/longjoy/micro-go-course/section19/cargo/service/handling"
)
type handlingHandler struct {
s handling.Service
logger kitlog.Logger
}
func (h *handlingHandler) router() chi.Router {
r := chi.NewRouter()
r.Post("/incidents", h.registerIncident)
r.Method("GET", "/docs", http.StripPrefix("/handling/v1/docs", http.FileServer(http.Dir("handling/docs"))))
return r
}
func (h *handlingHandler) registerIncident(w http.ResponseWriter, r *http.Request) {
ctx := context.Background()
var request struct {
CompletionTime time.Time `json:"completion_time"`
TrackingID string `json:"tracking_id"`
VoyageNumber string `json:"voyage"`
Location string `json:"location"`
EventType string `json:"event_type"`
}
if err := json.NewDecoder(r.Body).Decode(&request); err != nil {
h.logger.Log("error", err)
encodeError(ctx, err, w)
return
}
_, err := h.s.RegisterHandlingEvent(
request.CompletionTime,
shipping.TrackingID(request.TrackingID),
shipping.VoyageNumber(request.VoyageNumber),
shipping.UNLocode(request.Location),
stringToEventType(request.EventType),
)
if err != nil {
encodeError(ctx, err, w)
return
}
}
func stringToEventType(s string) shipping.HandlingEventType {
types := map[string]shipping.HandlingEventType{
shipping.Receive.String(): shipping.Receive,
shipping.Load.String(): shipping.Load,
shipping.Unload.String(): shipping.Unload,
shipping.Customs.String(): shipping.Customs,
shipping.Claim.String(): shipping.Claim,
}
return types[s]
}