aboutsummaryrefslogtreecommitdiff
path: root/modules
diff options
context:
space:
mode:
Diffstat (limited to 'modules')
-rw-r--r--modules/lists/lists.go159
1 files changed, 80 insertions, 79 deletions
diff --git a/modules/lists/lists.go b/modules/lists/lists.go
index a42c51c..3febdba 100644
--- a/modules/lists/lists.go
+++ b/modules/lists/lists.go
@@ -2,7 +2,7 @@ package lists
2 2
3import ( 3import (
4 "database/sql" 4 "database/sql"
5 "encoding/json" 5 "encoding/json"
6 "errors" 6 "errors"
7 "fagott.pw/charakterin" 7 "fagott.pw/charakterin"
8 "fagott.pw/grilist/frontend" 8 "fagott.pw/grilist/frontend"
@@ -15,8 +15,8 @@ import (
15 "log" 15 "log"
16 "net/http" 16 "net/http"
17 "net/url" 17 "net/url"
18 "strconv"
19 "sort" 18 "sort"
19 "strconv"
20) 20)
21 21
22// Module und so. 22// Module und so.
@@ -44,8 +44,9 @@ type ListGril struct {
44 44
45// ListGrils ist die Sort-Interface Implementation für Grils einer Liste. 45// ListGrils ist die Sort-Interface Implementation für Grils einer Liste.
46type ListGrils []*ListGril 46type ListGrils []*ListGril
47func (l ListGrils) Len() int { return len(l) } 47
48func (l ListGrils) Swap(i, j int) { l[i], l[j] = l[j], l[i] } 48func (l ListGrils) Len() int { return len(l) }
49func (l ListGrils) Swap(i, j int) { l[i], l[j] = l[j], l[i] }
49func (l ListGrils) Less(i, j int) bool { return l[i].Order < l[j].Order } 50func (l ListGrils) Less(i, j int) bool { return l[i].Order < l[j].Order }
50 51
51// Name gibt den Namen des Moduls zurück 52// Name gibt den Namen des Moduls zurück
@@ -68,13 +69,13 @@ func (m *Module) Init(g *grilist.Grilist) {
68 } 69 }
69 m.grils = grilsModule 70 m.grils = grilsModule
70 m.g.Router.GET("/list/:id", m.viewList) 71 m.g.Router.GET("/list/:id", m.viewList)
71 m.g.Router.POST("/list/:id/order", m.updateGrilOrder) 72 m.g.Router.POST("/list/:id/order", m.updateGrilOrder)
72 m.g.Router.POST("/list/:id", m.addGrilToList) 73 m.g.Router.POST("/list/:id", m.addGrilToList)
73 m.g.Router.DELETE("/list/:id/order", m.removeGrilFromList) 74 m.g.Router.DELETE("/list/:id/order", m.removeGrilFromList)
74 m.g.Router.GET("/new/list", m.displayCreateList) 75 m.g.Router.GET("/new/list", m.displayCreateList)
75 m.g.Router.POST("/new/list", m.createList) 76 m.g.Router.POST("/new/list", m.createList)
76 77
77 m.g.Router.GET("/api/lists/user", m.APIgetUserLists) 78 m.g.Router.GET("/api/lists/user", m.APIgetUserLists)
78} 79}
79 80
80func (m *Module) getListGrils(list *List) error { 81func (m *Module) getListGrils(list *List) error {
@@ -143,14 +144,14 @@ func (m *Module) GetUserLists(u *charakterin.User, withGrils bool) []*List {
143 if err != nil { 144 if err != nil {
144 log.Println(err) 145 log.Println(err)
145 } 146 }
146 147
147 if withGrils { 148 if withGrils {
148 for _, list := range lists { 149 for _, list := range lists {
149 if err := m.getListGrils(list); err != nil { 150 if err := m.getListGrils(list); err != nil {
150 log.Println(err) 151 log.Println(err)
151 } 152 }
152 } 153 }
153 } 154 }
154 return lists 155 return lists
155} 156}
156 157
@@ -160,14 +161,14 @@ func (m *Module) FromID(id int, withGrils bool) (*List, error) {
160 if err != nil { 161 if err != nil {
161 return nil, err 162 return nil, err
162 } 163 }
163 164
164 if withGrils { 165 if withGrils {
165 for _, list := range lists { 166 for _, list := range lists {
166 if err := m.getListGrils(list); err != nil { 167 if err := m.getListGrils(list); err != nil {
167 return nil, err 168 return nil, err
168 } 169 }
169 } 170 }
170 } 171 }
171 172
172 if len(lists) == 0 { 173 if len(lists) == 0 {
173 return nil, errors.New("no list found") 174 return nil, errors.New("no list found")
@@ -213,11 +214,11 @@ func (m *Module) ProvideDashboardData(user *charakterin.User) []grilist.Dashboar
213 log.Println(err) 214 log.Println(err)
214 return categories 215 return categories
215 } 216 }
216 for _, list := range lists { 217 for _, list := range lists {
217 if err := m.getListGrils(list); err != nil { 218 if err := m.getListGrils(list); err != nil {
218 log.Println(err) 219 log.Println(err)
219 } 220 }
220 } 221 }
221 222
222 categories = append(categories, grilist.DashboardCategory{ 223 categories = append(categories, grilist.DashboardCategory{
223 Title: "Neueste Listen", 224 Title: "Neueste Listen",
@@ -264,12 +265,12 @@ func (m *Module) viewList(w http.ResponseWriter, r *http.Request, p httprouter.P
264 265
265func (m *Module) addGrilToList(w http.ResponseWriter, r *http.Request, p httprouter.Params) { 266func (m *Module) addGrilToList(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
266 slistID := p.ByName("id") 267 slistID := p.ByName("id")
267 268
268 user, err := m.g.Charakterin.GetUserFromRequest(r) 269 user, err := m.g.Charakterin.GetUserFromRequest(r)
269 if err != nil { 270 if err != nil {
270 http.Error(w, "403", http.StatusForbidden) 271 http.Error(w, "403", http.StatusForbidden)
271 return 272 return
272 } 273 }
273 274
274 listID, err := strconv.Atoi(slistID) 275 listID, err := strconv.Atoi(slistID)
275 if err != nil { 276 if err != nil {
@@ -294,15 +295,15 @@ func (m *Module) addGrilToList(w http.ResponseWriter, r *http.Request, p httprou
294 http.Error(w, "invalid list", 404) 295 http.Error(w, "invalid list", 404)
295 return 296 return
296 } 297 }
297 298
298 if list.Owner.ID != user.ID { 299 if list.Owner.ID != user.ID {
299 http.Error(w, "403", http.StatusForbidden) 300 http.Error(w, "403", http.StatusForbidden)
300 return 301 return
301 } 302 }
302 303
303 rank := 0 304 rank := 0
304 if len(list.Grils) > 0 { 305 if len(list.Grils) > 0 {
305 rank = list.Grils[len(list.Grils) - 1].Order + 1 306 rank = list.Grils[len(list.Grils)-1].Order + 1
306 } 307 }
307 308
308 // rein in die DB damit 309 // rein in die DB damit
@@ -324,7 +325,7 @@ func (m *Module) addGrilToList(w http.ResponseWriter, r *http.Request, p httprou
324 data["Index"] = len(list.Grils) 325 data["Index"] = len(list.Grils)
325 value := make(map[string]interface{}) 326 value := make(map[string]interface{})
326 value["IsListOwner"] = true 327 value["IsListOwner"] = true
327 value["Gril"] = ListGril{ gril, rank } 328 value["Gril"] = ListGril{gril, rank}
328 data["Value"] = value 329 data["Value"] = value
329 m.g.Renderer.RenderPage("list_gril", w, data) 330 m.g.Renderer.RenderPage("list_gril", w, data)
330 return 331 return
@@ -371,8 +372,8 @@ func (m *Module) createList(w http.ResponseWriter, r *http.Request, p httprouter
371 372
372func (m *Module) updateGrilOrder(w http.ResponseWriter, r *http.Request, p httprouter.Params) { 373func (m *Module) updateGrilOrder(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
373 slistID := p.ByName("id") 374 slistID := p.ByName("id")
374 375
375 user, err := m.g.Charakterin.GetUserFromRequest(r) 376 user, err := m.g.Charakterin.GetUserFromRequest(r)
376 if err != nil { 377 if err != nil {
377 http.Error(w, "403", http.StatusForbidden) 378 http.Error(w, "403", http.StatusForbidden)
378 return 379 return
@@ -395,23 +396,23 @@ func (m *Module) updateGrilOrder(w http.ResponseWriter, r *http.Request, p httpr
395 http.Error(w, "invalid gril ID", 404) 396 http.Error(w, "invalid gril ID", 404)
396 return 397 return
397 } 398 }
398 399
399 pos, err := strconv.Atoi(values.Get("pos")) 400 pos, err := strconv.Atoi(values.Get("pos"))
400 if err != nil { 401 if err != nil {
401 http.Error(w, "invalid position", 400) 402 http.Error(w, "invalid position", 400)
402 return 403 return
403 } 404 }
404 405
405 // rein in die DB damit 406 // rein in die DB damit
406 _, err = m.g.DB.Query(`SELECT grilist.set_gril_order($1, $2, $3, $4)`, user.ID, listID, grilID, pos) 407 _, err = m.g.DB.Exec(`SELECT grilist.set_gril_order($1, $2, $3, $4)`, user.ID, listID, grilID, pos)
407 if err != nil { 408 if err != nil {
408 log.Println("error reordering gril:", err) 409 log.Println("error reordering gril:", err)
409 http.Error(w, "could not update gril order", 500) 410 http.Error(w, "could not update gril order", 500)
410 return 411 return
411 } 412 }
412 413
413 w.WriteHeader(200) 414 w.WriteHeader(200)
414 w.Write([]byte("ok")) 415 w.Write([]byte("ok"))
415 return 416 return
416} 417}
417 418
@@ -435,47 +436,47 @@ func (m *Module) removeGrilFromList(w http.ResponseWriter, r *http.Request, p ht
435 http.Error(w, "invalid gril ID", 404) 436 http.Error(w, "invalid gril ID", 404)
436 return 437 return
437 } 438 }
438 439
439 res, err := m.g.DB.Exec(`DELETE FROM grilist.lists_grils WHERE list_id=$1 AND gril_id=$2`, listID, grilID) 440 res, err := m.g.DB.Exec(`DELETE FROM grilist.lists_grils WHERE list_id=$1 AND gril_id=$2`, listID, grilID)
440 if err != nil { 441 if err != nil {
441 log.Println("error removing gril:", err) 442 log.Println("error removing gril:", err)
442 http.Error(w, "could not remove gril", 500) 443 http.Error(w, "could not remove gril", 500)
443 return 444 return
444 } 445 }
445
446 if r, err := res.RowsAffected(); err != nil || r == 0 {
447 log.Println("invalid rows affected in removing gril")
448 http.Error(w, "gril not in list / other error", 500)
449 return
450 }
451 446
452 w.WriteHeader(200) 447 if r, err := res.RowsAffected(); err != nil || r == 0 {
453 w.Write([]byte("ok")) 448 log.Println("invalid rows affected in removing gril")
449 http.Error(w, "gril not in list / other error", 500)
450 return
451 }
452
453 w.WriteHeader(200)
454 w.Write([]byte("ok"))
454 return 455 return
455} 456}
456 457
457func (m *Module) APIgetUserLists(w http.ResponseWriter, r *http.Request, p httprouter.Params) { 458func (m *Module) APIgetUserLists(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
458 user, err := m.g.Charakterin.GetUserFromRequest(r) 459 user, err := m.g.Charakterin.GetUserFromRequest(r)
459 if err != nil { 460 if err != nil {
460 http.Error(w, "403", http.StatusForbidden) 461 http.Error(w, "403", http.StatusForbidden)
461 return 462 return
462 } 463 }
463 464
464 lists := m.GetUserLists(user, false) 465 lists := m.GetUserLists(user, false)
465 466
466 for _, list := range lists { 467 for _, list := range lists {
467 // owner wegen SICHERHEIT rausfiltern (nodumppassword2k16) 468 // owner wegen SICHERHEIT rausfiltern (nodumppassword2k16)
468 list.Owner = nil 469 list.Owner = nil
469 } 470 }
470 471
471 data, err := json.Marshal(&lists) 472 data, err := json.Marshal(&lists)
472 if err != nil { 473 if err != nil {
473 log.Println(err) 474 log.Println(err)
474 http.Error(w, "500", http.StatusInternalServerError) 475 http.Error(w, "500", http.StatusInternalServerError)
475 return 476 return
476 } 477 }
477 w.WriteHeader(200) 478 w.WriteHeader(200)
478 w.Write(data) 479 w.Write(data)
479} 480}
480 481
481// New erstellt eine neue Instanz des Modules 482// New erstellt eine neue Instanz des Modules