/
register.go
92 lines (77 loc) · 2.56 KB
/
register.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package rest
import (
"context"
"encoding/json"
"fmt"
"net/http"
"time"
"github.com/PacktPublishing/Hands-On-Dependency-Injection-in-Go/ch07/acme/internal/modules/data"
)
// RegisterModel will validate and save a registration
//go:generate mockery -name=RegisterModel -case underscore -testonly -inpkg -note @generated
type RegisterModel interface {
Do(ctx context.Context, in *data.Person) (int, error)
}
// NewRegisterHandler is the constructor for RegisterHandler
func NewRegisterHandler(model RegisterModel) *RegisterHandler {
return &RegisterHandler{
registerer: model,
}
}
// RegisterHandler is the HTTP handler for the "Register" endpoint
// In this simplified example we are assuming all possible errors are user errors and returning "bad request" HTTP 400.
// There are some programmer errors possible but hopefully these will be caught in testing.
type RegisterHandler struct {
registerer RegisterModel
}
// ServeHTTP implements http.Handler
func (h *RegisterHandler) ServeHTTP(response http.ResponseWriter, request *http.Request) {
// set latency budget for this API
subCtx, cancel := context.WithTimeout(request.Context(), 1500*time.Millisecond)
defer cancel()
// extract payload from request
requestPayload, err := h.extractPayload(request)
if err != nil {
// output error
response.WriteHeader(http.StatusBadRequest)
return
}
// call the business logic using the request data and context
id, err := h.register(subCtx, requestPayload)
if err != nil {
// not need to log here as we can expect other layers to do so
response.WriteHeader(http.StatusBadRequest)
return
}
// happy path
response.Header().Add("Location", fmt.Sprintf("/person/%d/", id))
response.WriteHeader(http.StatusCreated)
}
// extract payload from request
func (h *RegisterHandler) extractPayload(request *http.Request) (*registerRequest, error) {
requestPayload := ®isterRequest{}
decoder := json.NewDecoder(request.Body)
err := decoder.Decode(requestPayload)
if err != nil {
return nil, err
}
return requestPayload, nil
}
// call the logic layer
func (h *RegisterHandler) register(ctx context.Context, requestPayload *registerRequest) (int, error) {
person := &data.Person{
FullName: requestPayload.FullName,
Phone: requestPayload.Phone,
Currency: requestPayload.Currency,
}
return h.registerer.Do(ctx, person)
}
// register endpoint request format
type registerRequest struct {
// FullName of the person
FullName string `json:"fullName"`
// Phone of the person
Phone string `json:"phone"`
// Currency the wish to register in
Currency string `json:"currency"`
}