aboutsummaryrefslogtreecommitdiff
path: root/modules/lists
diff options
context:
space:
mode:
Diffstat (limited to 'modules/lists')
-rw-r--r--modules/lists/lists.go72
1 files changed, 69 insertions, 3 deletions
diff --git a/modules/lists/lists.go b/modules/lists/lists.go
index 5934e9a..65e969f 100644
--- a/modules/lists/lists.go
+++ b/modules/lists/lists.go
@@ -14,6 +14,7 @@ import (
14 14
15 "fagott.pw/charakterin" 15 "fagott.pw/charakterin"
16 "fagott.pw/grilist/cache" 16 "fagott.pw/grilist/cache"
17 "fagott.pw/grilist/eventlogging"
17 "fagott.pw/grilist/frontend" 18 "fagott.pw/grilist/frontend"
18 "fagott.pw/grilist/grilist" 19 "fagott.pw/grilist/grilist"
19 "fagott.pw/grilist/models" 20 "fagott.pw/grilist/models"
@@ -260,6 +261,7 @@ func (m *Module) ProvideDashboardData(user *charakterin.User) []grilist.Dashboar
260 261
261func (m *Module) viewList(w http.ResponseWriter, r *http.Request, p httprouter.Params) { 262func (m *Module) viewList(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
262 user, _ := m.g.Charakterin.GetUserFromRequest(r) 263 user, _ := m.g.Charakterin.GetUserFromRequest(r)
264 el := m.g.EventLogger(r)
263 sid := p.ByName("id") 265 sid := p.ByName("id")
264 266
265 id, err := strconv.Atoi(sid) 267 id, err := strconv.Atoi(sid)
@@ -286,9 +288,13 @@ func (m *Module) viewList(w http.ResponseWriter, r *http.Request, p httprouter.P
286 } 288 }
287 289
288 m.g.Renderer.RenderPage("list", w, data) 290 m.g.Renderer.RenderPage("list", w, data)
291 el.ViewList(user, eventlogging.ViewListData{
292 ListID: list.ID,
293 })
289} 294}
290 295
291func (m *Module) deleteList(w http.ResponseWriter, r *http.Request, p httprouter.Params) { 296func (m *Module) deleteList(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
297 el := m.g.EventLogger(r)
292 user, err := m.g.Charakterin.GetUserFromRequest(r) 298 user, err := m.g.Charakterin.GetUserFromRequest(r)
293 if err != nil { 299 if err != nil {
294 log.Println("invalid deleteList user") 300 log.Println("invalid deleteList user")
@@ -334,6 +340,9 @@ func (m *Module) deleteList(w http.ResponseWriter, r *http.Request, p httprouter
334 340
335 log.Printf("list %d has been deleted by the owner %d(%s)", list.ID, list.Owner.ID, list.Owner.GetName()) 341 log.Printf("list %d has been deleted by the owner %d(%s)", list.ID, list.Owner.ID, list.Owner.GetName())
336 http.Redirect(w, r, "/", 302) 342 http.Redirect(w, r, "/", 302)
343 el.DeleteList(user, eventlogging.DeleteListData{
344 ListID: list.ID,
345 })
337} 346}
338 347
339func (m *Module) viewListSettings(w http.ResponseWriter, r *http.Request, p httprouter.Params) { 348func (m *Module) viewListSettings(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
@@ -372,6 +381,7 @@ func (m *Module) viewListSettings(w http.ResponseWriter, r *http.Request, p http
372} 381}
373 382
374func (m *Module) updateListSettings(w http.ResponseWriter, r *http.Request, p httprouter.Params) { 383func (m *Module) updateListSettings(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
384 el := m.g.EventLogger(r)
375 user, err := m.g.Charakterin.GetUserFromRequest(r) 385 user, err := m.g.Charakterin.GetUserFromRequest(r)
376 if err != nil { 386 if err != nil {
377 log.Println("invalid updateListSettings user") 387 log.Println("invalid updateListSettings user")
@@ -436,6 +446,18 @@ func (m *Module) updateListSettings(w http.ResponseWriter, r *http.Request, p ht
436 return 446 return
437 } 447 }
438 448
449 rows, err := m.g.DB.Query("SELECT name, description FROM grilist.lists WHERE id = $1;", id)
450 if err != nil {
451 log.Println("could not get list info", id)
452 renderWithError("interner fehler", false, false)
453 return
454 }
455 defer rows.Close()
456
457 var oldName string
458 var oldDescription string
459 rows.Scan(&oldName, &oldDescription)
460
439 if list.Name != name && list.Description != description { 461 if list.Name != name && list.Description != description {
440 _, err = m.g.DB.Query(`UPDATE grilist.lists SET name = $2, description = $3 WHERE id = $1`, id, name, description) 462 _, err = m.g.DB.Query(`UPDATE grilist.lists SET name = $2, description = $3 WHERE id = $1`, id, name, description)
441 if err != nil { 463 if err != nil {
@@ -463,9 +485,17 @@ func (m *Module) updateListSettings(w http.ResponseWriter, r *http.Request, p ht
463 list.Description = description 485 list.Description = description
464 486
465 m.viewListSettings(w, r, p) 487 m.viewListSettings(w, r, p)
488 el.EditList(user, eventlogging.EditListData{
489 ListID: id,
490 OldName: oldName,
491 OldDescription: oldDescription,
492 NewName: name,
493 NewDescription: description,
494 })
466} 495}
467 496
468func (m *Module) addGrilToList(w http.ResponseWriter, r *http.Request, p httprouter.Params) { 497func (m *Module) addGrilToList(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
498 el := m.g.EventLogger(r)
469 slistID := p.ByName("id") 499 slistID := p.ByName("id")
470 500
471 user, err := m.g.Charakterin.GetUserFromRequest(r) 501 user, err := m.g.Charakterin.GetUserFromRequest(r)
@@ -532,7 +562,10 @@ func (m *Module) addGrilToList(w http.ResponseWriter, r *http.Request, p httprou
532 data["Value"] = value 562 data["Value"] = value
533 list.Grils = append(list.Grils, lg) 563 list.Grils = append(list.Grils, lg)
534 m.g.Renderer.RenderPage("list_gril", w, data) 564 m.g.Renderer.RenderPage("list_gril", w, data)
535 return 565 el.AddGrilToList(user, eventlogging.AddGrilToListData{
566 ListID: list.ID,
567 GrilID: gril.ID,
568 })
536} 569}
537 570
538func (m *Module) displayCreateList(w http.ResponseWriter, r *http.Request, p httprouter.Params) { 571func (m *Module) displayCreateList(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
@@ -549,6 +582,7 @@ func (m *Module) displayCreateList(w http.ResponseWriter, r *http.Request, p htt
549} 582}
550 583
551func (m *Module) createList(w http.ResponseWriter, r *http.Request, p httprouter.Params) { 584func (m *Module) createList(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
585 el := m.g.EventLogger(r)
552 user, err := m.g.Charakterin.GetUserFromRequest(r) 586 user, err := m.g.Charakterin.GetUserFromRequest(r)
553 if err != nil { 587 if err != nil {
554 log.Println(err) 588 log.Println(err)
@@ -572,9 +606,15 @@ func (m *Module) createList(w http.ResponseWriter, r *http.Request, p httprouter
572 } 606 }
573 607
574 http.Redirect(w, r, fmt.Sprintf("/list/%d", id), 302) 608 http.Redirect(w, r, fmt.Sprintf("/list/%d", id), 302)
609 el.CreateList(user, eventlogging.CreateListData{
610 ListID: id,
611 Name: values.Get("name"),
612 Description: values.Get("description"),
613 })
575} 614}
576 615
577func (m *Module) updateGrilOrder(w http.ResponseWriter, r *http.Request, p httprouter.Params) { 616func (m *Module) updateGrilOrder(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
617 el := m.g.EventLogger(r)
578 slistID := p.ByName("id") 618 slistID := p.ByName("id")
579 619
580 user, err := m.g.Charakterin.GetUserFromRequest(r) 620 user, err := m.g.Charakterin.GetUserFromRequest(r)
@@ -607,6 +647,16 @@ func (m *Module) updateGrilOrder(w http.ResponseWriter, r *http.Request, p httpr
607 return 647 return
608 } 648 }
609 649
650 var oldOrder int
651 err = m.g.DB.QueryRow(
652 `SELECT "order" FROM grilist.lists_grils WHERE list_id = $1 AND gril_id = $2;`,
653 listID, grilID).Scan(&oldOrder)
654 if err != nil {
655 log.Println(err)
656 http.Error(w, "Internal Server Error", 500)
657 return
658 }
659
610 // rein in die DB damit 660 // rein in die DB damit
611 _, err = m.g.DB.Exec(`SELECT grilist.set_gril_order($1, $2, $3, $4)`, user.ID, listID, grilID, pos) 661 _, err = m.g.DB.Exec(`SELECT grilist.set_gril_order($1, $2, $3, $4)`, user.ID, listID, grilID, pos)
612 if err != nil { 662 if err != nil {
@@ -623,17 +673,30 @@ func (m *Module) updateGrilOrder(w http.ResponseWriter, r *http.Request, p httpr
623 673
624 w.WriteHeader(200) 674 w.WriteHeader(200)
625 w.Write([]byte("ok")) 675 w.Write([]byte("ok"))
626 return 676 el.ChangeGrilOrder(user, eventlogging.ChangeGrilOrderData{
677 ListID: listID,
678 GrilID: grilID,
679 OldOrder: oldOrder,
680 NewOrder: pos,
681 })
627} 682}
628 683
629func (m *Module) removeGrilFromList(w http.ResponseWriter, r *http.Request, p httprouter.Params) { 684func (m *Module) removeGrilFromList(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
685 el := m.g.EventLogger(r)
630 slistID := p.ByName("id") 686 slistID := p.ByName("id")
631 687
688 user, err := m.g.Charakterin.GetUserFromRequest(r)
689 if err != nil {
690 http.Error(w, "Unauthorized", 401)
691 return
692 }
693
632 listID, err := strconv.Atoi(slistID) 694 listID, err := strconv.Atoi(slistID)
633 if err != nil { 695 if err != nil {
634 http.Error(w, "invalid list ID (type mismatch)", 400) 696 http.Error(w, "invalid list ID (type mismatch)", 400)
635 return 697 return
636 } 698 }
699 //TODO: noch mal gucken ob der User Rechte hat
637 700
638 values, err := readBody(r) 701 values, err := readBody(r)
639 if err != nil { 702 if err != nil {
@@ -667,7 +730,10 @@ func (m *Module) removeGrilFromList(w http.ResponseWriter, r *http.Request, p ht
667 730
668 w.WriteHeader(200) 731 w.WriteHeader(200)
669 w.Write([]byte("ok")) 732 w.Write([]byte("ok"))
670 return 733 el.DeleteGrilFromList(user, eventlogging.DeleteGrilFromListData{
734 ListID: listID,
735 GrilID: grilID,
736 })
671} 737}
672 738
673func (m *Module) APIgetUserLists(w http.ResponseWriter, r *http.Request, p httprouter.Params) { 739func (m *Module) APIgetUserLists(w http.ResponseWriter, r *http.Request, p httprouter.Params) {