aboutsummaryrefslogtreecommitdiff
path: root/modules/lists
diff options
context:
space:
mode:
authorJan C <jan@ruken.pw>2016-02-15 08:13:46 (UTC)
committerJan C <jan@ruken.pw>2016-02-15 08:13:46 (UTC)
commite0676fb384b1a0f634025156db1233f51b5819e7 (patch)
tree1b86c6cbdcbcbbad38c090a08db6c4332b75d54a /modules/lists
parent889adc748ec9a723bca605dcba45d974ec362999 (diff)
grils können nun auch im backend herumgeworfen werden. sobald die datenbankfunktion funktioniert geht das auch so richtig.
Diffstat (limited to 'modules/lists')
-rw-r--r--modules/lists/lists.go80
1 files changed, 80 insertions, 0 deletions
diff --git a/modules/lists/lists.go b/modules/lists/lists.go
index ec29ea8..b40ee18 100644
--- a/modules/lists/lists.go
+++ b/modules/lists/lists.go
@@ -67,6 +67,7 @@ func (m *Module) Init(g *grilist.Grilist) {
67 } 67 }
68 m.grils = grilsModule 68 m.grils = grilsModule
69 m.g.Router.GET("/list/:id", m.viewList) 69 m.g.Router.GET("/list/:id", m.viewList)
70 m.g.Router.POST("/list/:id/order", m.updateGrilOrder)
70 m.g.Router.POST("/list/:id", m.addGrilToList) 71 m.g.Router.POST("/list/:id", m.addGrilToList)
71 m.g.Router.GET("/new/list", m.displayCreateList) 72 m.g.Router.GET("/new/list", m.displayCreateList)
72 m.g.Router.POST("/new/list", m.createList) 73 m.g.Router.POST("/new/list", m.createList)
@@ -341,6 +342,85 @@ func (m *Module) createList(w http.ResponseWriter, r *http.Request, p httprouter
341 http.Redirect(w, r, fmt.Sprintf("/list/%d", id), 302) 342 http.Redirect(w, r, fmt.Sprintf("/list/%d", id), 302)
342} 343}
343 344
345func (m *Module) updateGrilOrder(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
346 slistID := p.ByName("id")
347
348 user, err := m.g.Charakterin.GetUserFromRequest(r)
349 if err != nil {
350 http.Error(w, "403", http.StatusForbidden)
351 return
352 }
353
354 listID, err := strconv.Atoi(slistID)
355 if err != nil {
356 http.Error(w, "invalid list ID (type mismatch)", 400)
357 return
358 }
359
360 values, err := readBody(r)
361 if err != nil {
362 http.Error(w, "invalid POST data", 400)
363 return
364 }
365
366 grilID, err := strconv.Atoi(values.Get("gril"))
367 if err != nil {
368 http.Error(w, "invalid gril ID", 404)
369 return
370 }
371
372 diff, err := strconv.Atoi(values.Get("pos"))
373 if err != nil {
374 http.Error(w, "invalid position diff", 400)
375 return
376 }
377
378 list, err := m.FromID(listID)
379 if err != nil {
380 http.Error(w, "invalid list", 404)
381 return
382 }
383
384 // find the gril in our list
385 var lgril *ListGril = nil
386 log.Println(list.Grils)
387 for _, g := range list.Grils {
388 if g.Gril.ID == grilID {
389 lgril = g
390 break
391 }
392 }
393 if lgril == nil {
394 http.Error(w, "gril not found in the list", 404)
395 return
396 }
397
398 newPos := lgril.Order + diff
399
400 if newPos < 0 {
401 newPos = 0
402 } else if newPos >= len(list.Grils) {
403 newPos = len(list.Grils) - 1
404 }
405
406 if newPos == lgril.Order {
407 http.Error(w, "no change in order", 400)
408 return
409 }
410
411 // rein in die DB damit
412 _, err = m.g.DB.Query(`SELECT grilist.set_gril_order($1, $2, $3, $4)`, user.ID, listID, grilID, newPos)
413 if err != nil {
414 log.Println("error reordering gril:", err)
415 http.Error(w, "could not update gril order", 500)
416 return
417 }
418
419 w.WriteHeader(200)
420 w.Write([]byte("ok"))
421 return
422}
423
344// New erstellt eine neue Instanz des Modules 424// New erstellt eine neue Instanz des Modules
345func New() *Module { 425func New() *Module {
346 return &Module{} 426 return &Module{}