-
Notifications
You must be signed in to change notification settings - Fork 0
/
todos.go
258 lines (239 loc) · 5.48 KB
/
todos.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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
package rest
import (
"encoding/json"
"log"
"net/http"
"strconv"
"time"
"github.com/chirst/go-todo/todo"
"github.com/go-chi/chi"
)
type todoGetter interface {
GetTodos(userID int) (todo.Todos, error)
}
// GetTodos returns all todos belonging to the current user
func GetTodos(s todoGetter) func(w http.ResponseWriter, r *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
uid := getUserID(r)
todos, err := s.GetTodos(uid)
if err != nil {
log.Print(err.Error())
http.Error(w, "Unable to get todos", http.StatusInternalServerError)
return
}
jsonTodos, err := todos.ToJSON()
if err != nil {
log.Print(err.Error())
http.Error(
w,
"Unable to serialize todos",
http.StatusInternalServerError,
)
return
}
w.Header().Set("Content-Type", "application/json")
_, err = w.Write(jsonTodos)
if err != nil {
log.Print(err.Error())
http.Error(
w,
"Unable to write response",
http.StatusInternalServerError,
)
}
}
}
type todoAdder interface {
AddTodo(
name string,
completed *time.Time,
userID int,
priorityID *int,
) (*todo.Todo, error)
}
type addTodoBody struct {
Name string
Completed *time.Time
Priority *int
}
// AddTodo adds a todo for the current user
func AddTodo(s todoAdder) func(w http.ResponseWriter, r *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
bt := addTodoBody{}
if err := json.NewDecoder(r.Body).Decode(&bt); err != nil {
log.Print(err.Error())
http.Error(
w,
"Unable to decode the request body",
http.StatusBadRequest,
)
return
}
uid := getUserID(r)
addedTodo, err := s.AddTodo(bt.Name, bt.Completed, uid, bt.Priority)
if err != nil {
log.Print(err.Error())
http.Error(w, "Unable to add todo", http.StatusInternalServerError)
return
}
jsonTodo, err := addedTodo.ToJSON()
if err != nil {
log.Print(err.Error())
http.Error(
w,
"Unable to serialize added todo",
http.StatusInternalServerError,
)
return
}
w.Header().Set("Content-Type", "application/json")
_, err = w.Write(jsonTodo)
if err != nil {
log.Print(err.Error())
http.Error(
w,
"Unable to write response",
http.StatusInternalServerError,
)
}
}
}
type todoPatcher interface {
UpdatePriority(userID, todoID, priorityID int) error
CompleteTodo(userID, todoID int) error
IncompleteTodo(userID, todoID int) error
ChangeTodoName(userID, todoID int, name string) error
}
type patchTodoBody struct {
Complete *bool
Name *string
PriorityID *int
}
// PatchTodo updates the given optional fields of patchTodoBody
func PatchTodo(s todoPatcher) func(
w http.ResponseWriter,
r *http.Request,
) {
return func(w http.ResponseWriter, r *http.Request) {
id := chi.URLParam(r, "todoID")
todoID, err := strconv.Atoi(id)
if err != nil {
log.Print(err.Error())
http.Error(w, "Unable to read todo id", http.StatusBadRequest)
return
}
uid := getUserID(r)
body := patchTodoBody{}
if err := json.NewDecoder(r.Body).Decode(&body); err != nil {
log.Print(err.Error())
http.Error(w, "Unable to read body", http.StatusBadRequest)
return
}
if body.Complete != nil {
if *body.Complete {
err = s.CompleteTodo(uid, todoID)
} else {
err = s.IncompleteTodo(uid, todoID)
}
if err != nil {
log.Print(err.Error())
http.Error(
w,
"Unable to change completion status",
http.StatusInternalServerError,
)
}
}
if body.Name != nil {
err = s.ChangeTodoName(uid, todoID, *body.Name)
if err != nil {
log.Print(err.Error())
http.Error(
w,
"Unable to change todo name",
http.StatusInternalServerError,
)
}
}
if body.PriorityID != nil {
err = s.UpdatePriority(uid, todoID, *body.PriorityID)
if err != nil {
log.Print(err.Error())
http.Error(
w,
"Unable to change todo priority",
http.StatusInternalServerError,
)
}
}
}
}
type todoDeleter interface {
DeleteTodo(userID, todoID int) error
}
// DeleteTodo deletes todo with the given id
func DeleteTodo(s todoDeleter) func(w http.ResponseWriter, r *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
todoID := chi.URLParam(r, "todoID")
id, err := strconv.Atoi(todoID)
if err != nil {
log.Print(err.Error())
http.Error(w, "unable to delete todo", http.StatusBadRequest)
return
}
uid := getUserID(r)
err = s.DeleteTodo(uid, id)
if err != nil {
log.Print(err.Error())
http.Error(
w,
"unable to delete todo",
http.StatusInternalServerError,
)
return
}
w.WriteHeader(http.StatusNoContent)
w.Header().Set("Content-Type", "text/plain")
}
}
type priorityGetter interface {
GetPriorities() (todo.Priorities, error)
}
// GetPriorities returns all possible priorities.
func GetPriorities(s priorityGetter) func(
w http.ResponseWriter,
r *http.Request,
) {
return func(w http.ResponseWriter, r *http.Request) {
ps, err := s.GetPriorities()
if err != nil {
log.Print(err.Error())
http.Error(
w,
"unable to get priorities",
http.StatusInternalServerError,
)
return
}
jsonPriorities, err := ps.ToJSON()
if err != nil {
log.Print(err.Error())
http.Error(
w,
"unable to serialize priorities",
http.StatusInternalServerError,
)
return
}
w.Header().Set("Content-Type", "application/json")
_, err = w.Write(jsonPriorities)
if err != nil {
log.Print(err.Error())
http.Error(
w,
"Unable to write response",
http.StatusInternalServerError,
)
}
}
}