-
Notifications
You must be signed in to change notification settings - Fork 162
/
rest.go
131 lines (119 loc) · 3.19 KB
/
rest.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
package rest
import (
"encoding/json"
"net/http"
"strconv"
"github.com/anz-bank/go-course/10_rest/mohitnag/pkg/puppy"
"github.com/anz-bank/go-course/10_rest/mohitnag/pkg/puppy/store"
"github.com/go-chi/chi"
"github.com/go-chi/chi/middleware"
"github.com/go-chi/render"
)
type Handler struct {
storer store.Storer
router chi.Router
}
func (rh *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
rh.router.ServeHTTP(w, r)
}
func NewRestHandler(s store.Storer) http.Handler {
r := chi.NewRouter()
r.Use(middleware.RequestID)
r.Use(middleware.Logger)
r.Use(middleware.Recoverer)
r.Use(middleware.URLFormat)
r.Use(render.SetContentType(render.ContentTypeJSON))
rh := Handler{
storer: s,
router: r}
rh.handleRoutes()
return &rh
}
func (rh *Handler) handleRoutes() {
r := rh.router
r.Route("/api/puppy", func(r chi.Router) {
r.Route("/", func(r chi.Router) {
r.Post("/", rh.handlePostPuppy)
r.Put("/", rh.handleUpdatePuppy)
})
r.Route("/{id}", func(r chi.Router) {
r.Get("/", rh.handleGetPuppy)
r.Delete("/", rh.handleDeletePuppy)
})
})
}
func (rh *Handler) handlePostPuppy(w http.ResponseWriter, r *http.Request) {
var puppy puppy.Puppy
if err := render.DecodeJSON(r.Body, &puppy); err != nil {
renderDecodeJSONErrorResponse(w, r, err)
return
}
if err := rh.storer.CreatePuppy(puppy); err != nil {
renderErrorResponse(w, r, err)
return
}
w.WriteHeader(http.StatusCreated)
}
func (rh *Handler) handleGetPuppy(w http.ResponseWriter, r *http.Request) {
var puppy puppy.Puppy
ID := chi.URLParam(r, "id")
puppyID, err := strconv.Atoi(ID)
if err != nil {
w.WriteHeader(http.StatusNotFound)
}
puppy, err = rh.storer.ReadPuppy(uint32(puppyID))
if err != nil {
renderErrorResponse(w, r, err)
return
}
render.JSON(w, r, puppy)
}
func (rh *Handler) handleUpdatePuppy(w http.ResponseWriter, r *http.Request) {
var puppy puppy.Puppy
if err := render.DecodeJSON(r.Body, &puppy); err != nil {
renderDecodeJSONErrorResponse(w, r, err)
return
}
err := rh.storer.UpdatePuppy(puppy)
if err != nil {
renderErrorResponse(w, r, err)
return
}
render.JSON(w, r, puppy)
}
func (rh *Handler) handleDeletePuppy(w http.ResponseWriter, r *http.Request) {
ID := chi.URLParam(r, "id")
puppyID, err := strconv.Atoi(ID)
if err != nil {
w.WriteHeader(http.StatusNotFound)
return
}
err = rh.storer.DeletePuppy(uint32(puppyID))
if err != nil {
renderErrorResponse(w, r, err)
return
}
w.WriteHeader(http.StatusNoContent)
}
func renderErrorResponse(w http.ResponseWriter, r *http.Request, err error) {
status := http.StatusInternalServerError
body := map[string]interface{}{"error": err.Error()}
if puppyError, ok := err.(*puppy.Error); ok {
body["error"] = puppyError.Message
body["code"] = puppyError.Code
switch puppyError.Code {
case puppy.NotFound:
status = http.StatusNotFound
case puppy.Invalid, puppy.Duplicate:
status = http.StatusBadRequest
}
}
render.Status(r, status)
render.JSON(w, r, body)
}
func renderDecodeJSONErrorResponse(w http.ResponseWriter, r *http.Request, err error) {
if _, ok := err.(*json.UnmarshalTypeError); ok {
err = puppy.ErrorF(puppy.Invalid, "Invalid JSON for required data type")
}
renderErrorResponse(w, r, err)
}