-
Notifications
You must be signed in to change notification settings - Fork 0
/
handler.go
231 lines (200 loc) · 4.87 KB
/
handler.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
package rest
import (
"bytes"
"encoding/json"
"io"
"log"
"net/http"
"github.com/induzo/crud"
"github.com/go-chi/render"
"github.com/induzo/gohttperror"
)
// POSTHandler will handle data from request
// and returns bytes to be written to response
func POSTHandler(
cmgr crud.MgrI,
) func(w http.ResponseWriter, r *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
var errRender error
defer func() {
if errRender != nil {
log.Fatalf("POSTHandler Render: %v", errRender)
}
}()
var payload bytes.Buffer
pl := io.TeeReader(r.Body, &payload)
ent := cmgr.NewEmptyEntity()
if errJSON := json.NewDecoder(pl).Decode(ent); errJSON != nil {
errRender = render.Render(w, r,
gohttperror.ErrBadRequest(errJSON),
)
return
}
e, err := cmgr.Create(r.Context(), ent, &payload)
if err != nil {
errRender = render.Render(w, r, gohttperror.ErrInternal(err))
return
}
w.WriteHeader(http.StatusCreated)
render.DefaultResponder(w, r, e)
}
}
// GETListHandler will handle data from request
// and returns bytes to be written to response
func GETListHandler(
cmgr crud.MgrI,
) func(w http.ResponseWriter, r *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
var errRender error
defer func() {
if errRender != nil {
log.Fatalf("GETListHandler Render: %v", errRender)
}
}()
es, errGL := cmgr.GetList(
r.Context(),
ListModifiersFromURL(r.URL),
)
if errGL != nil {
errRender = render.Render(w, r, cmgr.MapErrorToHTTPError(errGL))
return
}
render.DefaultResponder(w, r, es)
}
}
// GETHandler returns a unique entity
func GETHandler(
cmgr crud.MgrI,
) func(w http.ResponseWriter, r *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
var errRender error
defer func() {
if errRender != nil {
log.Fatalf("GETHandler Render: %v", errRender)
}
}()
ID, errParse := parseIDFromRequest(r)
if errParse != nil {
errRender = render.Render(w, r,
gohttperror.ErrBadRequest(errParse),
)
return
}
e, errG := cmgr.Get(r.Context(), ID)
if errG != nil {
errRender = render.Render(w, r, cmgr.MapErrorToHTTPError(errG))
return
}
render.DefaultResponder(w, r, e)
}
}
// DELETEHandler will delete a specific entity
func DELETEHandler(
cmgr crud.MgrI,
) func(w http.ResponseWriter, r *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
var errRender error
defer func() {
if errRender != nil {
log.Fatalf("DELETEHandler Render: %v", errRender)
}
}()
ID, errParse := parseIDFromRequest(r)
if errParse != nil {
errRender = render.Render(w, r,
gohttperror.ErrBadRequest(errParse),
)
return
}
if err := cmgr.Delete(
r.Context(),
ID,
); err != nil {
errRender = render.Render(w, r, cmgr.MapErrorToHTTPError(err))
return
}
w.WriteHeader(http.StatusAccepted)
}
}
// PUTHandler will update all data for a specific entity
func PUTHandler(
cmgr crud.MgrI,
) func(w http.ResponseWriter, r *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
var errRender error
defer func() {
if errRender != nil {
log.Fatalf("PUTHandler Render: %v", errRender)
}
}()
ID, errParse := parseIDFromRequest(r)
if errParse != nil {
errRender = render.Render(w, r,
gohttperror.ErrBadRequest(errParse),
)
return
}
var payload bytes.Buffer
pl := io.TeeReader(r.Body, &payload)
ent := cmgr.NewEmptyEntity()
if errJSON := json.NewDecoder(pl).Decode(&ent); errJSON != nil {
errRender = render.Render(w, r,
gohttperror.ErrBadRequest(errJSON),
)
return
}
e, errU := cmgr.Update(
r.Context(),
ID,
ent,
&payload,
)
if errU != nil {
errRender = render.Render(w, r, cmgr.MapErrorToHTTPError(errU))
return
}
w.WriteHeader(http.StatusOK)
render.DefaultResponder(w, r, e)
}
}
// PATCHHandler will update specific data for a specific entity
// Following https://tools.ietf.org/html/rfc7386
// Content-Type: application/merge-patch+json
func PATCHHandler(
cmgr crud.MgrI,
) func(w http.ResponseWriter, r *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
var errRender error
defer func() {
if errRender != nil {
log.Fatalf("PATCHHandler Render: %v", errRender)
}
}()
ID, errParse := parseIDFromRequest(r)
if errParse != nil {
errRender = render.Render(w, r,
gohttperror.ErrBadRequest(errParse),
)
return
}
var payload bytes.Buffer
pl := io.TeeReader(r.Body, &payload)
updates := crud.PartialUpdateData{}
if errJSON := json.NewDecoder(pl).Decode(&updates); errJSON != nil {
errRender = render.Render(w, r,
gohttperror.ErrBadRequest(errJSON),
)
return
}
if err := cmgr.PartialUpdate(
r.Context(),
ID,
updates,
&payload,
); err != nil {
errRender = render.Render(w, r, cmgr.MapErrorToHTTPError(err))
return
}
w.WriteHeader(http.StatusNoContent)
}
}