Repository navigation
Expand file tree
/
Copy pathrest.go
More file actions
176 lines (145 loc) · 4.8 KB
/
Copy pathrest.go
File metadata and controls
176 lines (145 loc) · 4.8 KB
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
// Package controller provides application controllers that convert incoming
// requests to domain models, run business logic on them and return the results.
package controller
import (
"encoding/json"
"net/http"
"strconv"
"github.com/dominikbraun/todo/core"
"github.com/dominikbraun/todo/model"
"github.com/dominikbraun/todo/storage"
"github.com/go-chi/chi"
"github.com/go-chi/render"
)
type errorResponse struct {
Error string `json:"error"`
}
// RESTController represents a controller capable of handling HTTP requests and
// yielding a corresponding JSON result.
type RESTController struct {
app *core.App
}
// NewRESTController returns a new REST controller instance that will use the
// provided app instance for running business logic.
func NewRESTController(app *core.App) *RESTController {
return &RESTController{
app: app,
}
}
// CreateToDo processes a POST request for creating a ToDo item. It expects a
// ToDo item without ID and returns an item containing the ID.
func (r *RESTController) CreateToDo() http.HandlerFunc {
return func(writer http.ResponseWriter, request *http.Request) {
var toDo model.ToDo
if err := json.NewDecoder(request.Body).Decode(&toDo); err != nil {
respond(writer, request, http.StatusUnprocessableEntity, err)
return
}
createdToDo, err := r.app.CreateToDo(toDo)
if err != nil {
respond(writer, request, statusCodeForError(err), err)
return
}
respond(writer, request, http.StatusOK, createdToDo)
}
}
// GetToDos processes a GET request for listing all ToDo items.
func (r *RESTController) GetToDos() http.HandlerFunc {
return func(writer http.ResponseWriter, request *http.Request) {
toDos, err := r.app.GetToDos()
if err != nil {
respond(writer, request, statusCodeForError(err), err)
return
}
respond(writer, request, http.StatusOK, toDos)
}
}
// GetToDo processes a GET request for retrieving a single ToDo item by ID.
//
// Expects the `id` URL parameter.
func (r *RESTController) GetToDo() http.HandlerFunc {
return func(writer http.ResponseWriter, request *http.Request) {
id, err := strconv.Atoi(chi.URLParam(request, "id"))
if err != nil {
respond(writer, request, statusCodeForError(err), err)
return
}
toDo, err := r.app.GetToDo(int64(id))
if err != nil {
respond(writer, request, statusCodeForError(err), err)
return
}
respond(writer, request, http.StatusOK, toDo)
}
}
// UpdateToDo processes a PUT request for updating a ToDo item. The item with
// the given ID will be overridden by the item in the request body.
//
// Expects the `id` URL parameter.
func (r *RESTController) UpdateToDo() http.HandlerFunc {
return func(writer http.ResponseWriter, request *http.Request) {
id, err := strconv.Atoi(chi.URLParam(request, "id"))
if err != nil {
respond(writer, request, statusCodeForError(err), err)
return
}
var toDo model.ToDo
if err := json.NewDecoder(request.Body).Decode(&toDo); err != nil {
respond(writer, request, http.StatusUnprocessableEntity, err)
return
}
err = r.app.UpdateToDo(int64(id), toDo)
if err != nil {
respond(writer, request, statusCodeForError(err), err)
return
}
respond(writer, request, http.StatusOK, nil)
}
}
// DeleteToDo processes a DELETE request for deleting a single ToDo item. This
// will also delete all of the item's sub-tasks.
//
// Expects the `id` URL parameter.
func (r *RESTController) DeleteToDo() http.HandlerFunc {
return func(writer http.ResponseWriter, request *http.Request) {
id, err := strconv.Atoi(chi.URLParam(request, "id"))
if err != nil {
respond(writer, request, statusCodeForError(err), err)
return
}
err = r.app.DeleteToDo(int64(id))
if err != nil {
respond(writer, request, statusCodeForError(err), err)
return
}
respond(writer, request, http.StatusOK, nil)
}
}
// respond writes the status code as well as the JSON body to an HTTP response.
//
// If v is nil, the response body will be empty. In addition, an errorResponse
// instance will be rendered automatically if v is an error value.
func respond(writer http.ResponseWriter, request *http.Request, status int, v interface{}) {
render.Status(request, status)
response := v
if err, isError := v.(error); isError {
response = errorResponse{Error: err.Error()}
}
if response != nil {
render.JSON(writer, request, response)
}
}
// statusCodeForError returns an appropriate HTTP status code for a given error.
func statusCodeForError(err error) int {
statusCodes := map[error]int{
storage.ErrToDoNotFound: http.StatusNotFound,
core.ErrNameMustNotBeEmpty: http.StatusUnprocessableEntity,
nil: http.StatusOK,
}
statusCode, isRegistered := statusCodes[err]
// Return status 500 for all errors that are not nil and not registered.
if err != nil && !isRegistered {
return http.StatusInternalServerError
}
return statusCode
}