-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.go
310 lines (240 loc) · 5.79 KB
/
api.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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
package main
import(
"net/http"
"strconv"
"log"
"encoding/json"
"time"
)
func getTasks(w http.ResponseWriter, r *http.Request) {
if r.Method != "GET" {
w.WriteHeader(404)
return
}
var tasks []Task
var user_id int
userCookie, _ := r.Cookie("user_id")
user_id, _ = strconv.Atoi(userCookie.Value)
rows, err := readTasks(user_id)
if err != nil {
writeErrorToResponse(w, err)
return
}
defer rows.Close()
for rows.Next() {
var task Task
err := rows.Scan(&task.Task_id, &task.Name, &task.Category_id, &task.Done, &task.User_id)
if err != nil {
logError("getTasks scan", err)
writeErrorToResponse(w, err)
return
}
tasks = append(tasks, task)
}
json.NewEncoder(w).Encode(tasks)
}
func getCategories(w http.ResponseWriter, r *http.Request) {
if r.Method != "GET" {
w.WriteHeader(404)
return
}
var categories []Category
userCookie, _ := r.Cookie("user_id")
user_id, _ := strconv.Atoi(userCookie.Value)
rows, err := readCategories(user_id)
if err != nil {
writeErrorToResponse(w, err)
return
}
defer rows.Close()
for rows.Next() {
var category Category
err := rows.Scan(&category.Category_id, &category.Name, &category.Color, &category.User_id)
if err != nil {
logError("getCategories scan", err)
writeErrorToResponse(w, err)
return
}
categories = append(categories, category)
}
json.NewEncoder(w).Encode(categories)
}
func newTask(w http.ResponseWriter, r *http.Request) {
if r.Method != "POST" {
w.WriteHeader(404)
return
}
userCookie, _ := r.Cookie("user_id")
user_id, _ := strconv.Atoi(userCookie.Value)
decoder := json.NewDecoder(r.Body)
var task NewTask
err := decoder.Decode(&task)
logError("newTask", err)
if task.Name == "" {
log.Print("empty name")
writeErrorMessageToResponse(w, "no name provided")
return
}
if task.Category_id == 0 {
log.Print("empty category id")
writeErrorMessageToResponse(w, "no category_id provided")
return
}
err2 := createTask(task.Name, user_id, task.Category_id)
logError("newTask createTask", err2)
if err2 != nil {
writeErrorToResponse(w, err2)
return
}
}
func doneTask(w http.ResponseWriter, r *http.Request) {
if r.Method != "PUT" {
w.WriteHeader(404)
return
}
task_id, _ := strconv.Atoi(r.Header.Get("task_id"))
done, _ := strconv.ParseBool(r.Header.Get("done"))
log.Print(r.Header.Get("done"))
log.Print(done)
err := updateTaskDone(task_id, done)
if err != nil {
logError("doneTask updateTaskDone", err)
writeErrorToResponse(w, err)
return
}
}
func newCategory(w http.ResponseWriter, r *http.Request) {
if r.Method != "POST" {
w.WriteHeader(404)
return
}
userCookie, _ := r.Cookie("user_id")
user_id, _ := strconv.Atoi(userCookie.Value)
decoder := json.NewDecoder(r.Body)
var category NewCategory
err := decoder.Decode(&category)
logError("postCategory", err)
if category.Name == "" {
log.Print("empty name")
writeErrorMessageToResponse(w, "no name provided")
return
}
cColor , _ := strconv.Atoi(category.Color)
err2 := createCategory(category.Name, cColor, user_id)
if err2 != nil {
logError("newCategory newCategory", err2)
writeErrorToResponse(w, err2)
return
}
}
func removeCategory(w http.ResponseWriter, r *http.Request) {
if r.Method != "DELETE" {
w.WriteHeader(404)
return
}
cId, _ := strconv.Atoi(r.Header.Get("category_id"))
log.Print(r.Header.Get("category_id"))
err := deleteCategory(cId)
if err != nil {
logError("removeCategory deteleCategory", err)
writeErrorToResponse(w, err)
return
}
}
func removeDoneTasks(w http.ResponseWriter, r *http.Request) {
if r.Method != "DELETE" {
w.WriteHeader(404)
return
}
userCookie, _ := r.Cookie("user_id")
user_id, _ := strconv.Atoi(userCookie.Value)
err := deleteDoneTasks(user_id)
if err != nil {
logError("removeDoneTasks deleteDoneTasks", err)
writeErrorToResponse(w, err)
return
}
}
func login(w http.ResponseWriter, r *http.Request) {
if r.Method != "POST" {
w.WriteHeader(404)
return
}
username := r.Header.Get("username")
password := r.Header.Get("password")
log.Print(username)
log.Print(password)
if username == "" {
writeErrorMessageToResponse(w, "no username provided")
return
}
if password == "" {
writeErrorMessageToResponse(w, "no password provided")
return
}
rows, err := readUser(username)
if err != nil {
logError("login readUser", err)
writeErrorToResponse(w, err)
return
}
defer rows.Close()
if !rows.Next() {
log.Print("login readUser; user not found")
writeErrorMessageToResponse(w, "user not found")
return
}
var user User
err2 := rows.Scan(&user.User_id, &user.Username, &user.Password)
if err2 != nil {
logError("login scan", err2)
return
}
err3 := checkPassword(password, user.Password)
if err3 != nil {
logError("login checkPasswordHash", err3)
writeErrorMessageToResponse(w, "wrong password")
return
} else { //login success!
token := GetJWT(username)
log.Print("got jwt")
http.SetCookie(w, &http.Cookie{
Name: "token",
Value: token,
Secure: true,
SameSite: http.SameSiteNoneMode,
Path: "/",
Expires: time.Now().AddDate(0, 6, 0),
})
http.SetCookie(w, &http.Cookie{
Name: "user_id",
Value: strconv.Itoa(user.User_id),
Secure: true,
SameSite: http.SameSiteNoneMode,
Path: "/",
Expires: time.Now().AddDate(0, 6, 0),
})
}
}
func signup(w http.ResponseWriter, r *http.Request) {
if r.Method != "POST" {
w.WriteHeader(404)
return
}
username := r.Header.Get("username")
password := r.Header.Get("password")
if username == "" {
writeErrorMessageToResponse(w, "no username provided")
return
}
if password == "" {
writeErrorMessageToResponse(w, "no password provided")
return
}
err := createUser(username, password)
if err != nil {
logError("signup createUser", err)
writeErrorToResponse(w, err)
return
}
}