From 5b069f567cc45475a7d2562150de6550d03634f5 Mon Sep 17 00:00:00 2001 From: jan Date: Mon, 10 Oct 2016 13:03:30 +0200 Subject: listeneinstellungen funktionieren wir sollten vielleicht nur in die Datenbank schreiben, wenn auch was geaendert ist. diff --git a/modules/lists/lists.go b/modules/lists/lists.go index 73a3fe7..2f37fb8 100644 --- a/modules/lists/lists.go +++ b/modules/lists/lists.go @@ -76,6 +76,7 @@ func (m *Module) Init(g *grilist.Grilist) { m.g.Router.POST("/list/:id", m.addGrilToList) m.g.Router.DELETE("/list/:id/order", m.removeGrilFromList) m.g.Router.GET("/list/:id/settings", m.viewListSettings) + m.g.Router.POST("/list/:id/settings", m.updateListSettings) m.g.Router.GET("/new/list", m.displayCreateList) m.g.Router.POST("/new/list", m.createList) @@ -321,6 +322,84 @@ func (m *Module) viewListSettings(w http.ResponseWriter, r *http.Request, p http m.g.Renderer.RenderPage("list_settings", w, data) } +func (m *Module) updateListSettings(w http.ResponseWriter, r *http.Request, p httprouter.Params) { + user, err := m.g.Charakterin.GetUserFromRequest(r) + if err != nil { + log.Println("invalid updateListSettings user") + http.Redirect(w, r, fmt.Sprintf("/list/%s", p.ByName("id")), 403) + return + } + + id, err := strconv.Atoi(p.ByName("id")) + if err != nil { + log.Println("invalid updateListSettings ID") + http.Redirect(w, r, "/", 400) + return + } + + list, err := m.FromID(id, false) + if err != nil { + log.Println("invalid updateListSettings ID (could not get list)") + http.Redirect(w, r, fmt.Sprintf("/list/%d", id), 500) + return + } + + // checken, ob der user auch list owner ist + if user.ID != list.Owner.ID { + log.Println("invalid updateListSettings request: user not list owner") + http.Redirect(w, r, fmt.Sprintf("/list/%d", id), 403) + return + } + + renderWithError := func(message string, nameError, descriptionError bool) { + data := m.g.Renderer.DefaultData() + data["user"] = user + data["list"] = list + data["error"] = message + data["error_name"] = nameError + data["error_description"] = descriptionError + + m.g.Renderer.RenderPage("list_settings", w, data) + } + + values, err := readBody(r) + if err != nil { + log.Println("invalid POST data in updateListSettings") + renderWithError("invalid post data", false, false) + return + } + + name := values.Get("name") + if len(name) < 3 { + renderWithError("Name zu kurz", true, false) + return + } else if len(name) > 32 { + renderWithError("Name zu lang", true, false) + return + } + + description := values.Get("description") + if len(description) < 3 { + renderWithError("Beschreibung zu kurz", false, true) + return + } else if len(description) > 100 { + renderWithError("Beschreibung zu lang", false, true) + return + } + + list.Name = name + list.Description = description + + _, err = m.g.DB.Query(`UPDATE grilist.lists SET name = $2, description = $3 WHERE id = $1`, id, name, description) + if err != nil { + log.Println("could not update list:", err) + renderWithError("interner fehler", false, false) + return + } + + m.viewListSettings(w, r, p) +} + func (m *Module) addGrilToList(w http.ResponseWriter, r *http.Request, p httprouter.Params) { slistID := p.ByName("id") diff --git a/views/pages/list_settings.html b/views/pages/list_settings.html index 0ce3860..5612093 100644 --- a/views/pages/list_settings.html +++ b/views/pages/list_settings.html @@ -10,7 +10,7 @@ {{ template "navbar" . }}
-

Listeneinstellungen

+

arrow_back Listeneinstellungen

{{ if .error }} @@ -23,14 +23,15 @@
{{ end }} -
+
assignment_ind - + +
mode_edit - +
-- cgit v0.10.1