diff options
author | jan <jan@ruken.pw> | 2016-10-10 11:03:30 (UTC) |
---|---|---|
committer | jan <jan@ruken.pw> | 2016-10-10 11:03:30 (UTC) |
commit | 5b069f567cc45475a7d2562150de6550d03634f5 (patch) | |
tree | b38e5ed69425166c4e131087361fd8bb85bfe4bf /modules | |
parent | 49f7ec4e634c22c07df461b45840e6bd0830a88b (diff) |
listeneinstellungen funktionieren
wir sollten vielleicht nur in die Datenbank schreiben, wenn auch was geaendert ist.
Diffstat (limited to 'modules')
-rw-r--r-- | modules/lists/lists.go | 79 |
1 files changed, 79 insertions, 0 deletions
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) { | |||
76 | m.g.Router.POST("/list/:id", m.addGrilToList) | 76 | m.g.Router.POST("/list/:id", m.addGrilToList) |
77 | m.g.Router.DELETE("/list/:id/order", m.removeGrilFromList) | 77 | m.g.Router.DELETE("/list/:id/order", m.removeGrilFromList) |
78 | m.g.Router.GET("/list/:id/settings", m.viewListSettings) | 78 | m.g.Router.GET("/list/:id/settings", m.viewListSettings) |
79 | m.g.Router.POST("/list/:id/settings", m.updateListSettings) | ||
79 | m.g.Router.GET("/new/list", m.displayCreateList) | 80 | m.g.Router.GET("/new/list", m.displayCreateList) |
80 | m.g.Router.POST("/new/list", m.createList) | 81 | m.g.Router.POST("/new/list", m.createList) |
81 | 82 | ||
@@ -321,6 +322,84 @@ func (m *Module) viewListSettings(w http.ResponseWriter, r *http.Request, p http | |||
321 | m.g.Renderer.RenderPage("list_settings", w, data) | 322 | m.g.Renderer.RenderPage("list_settings", w, data) |
322 | } | 323 | } |
323 | 324 | ||
325 | func (m *Module) updateListSettings(w http.ResponseWriter, r *http.Request, p httprouter.Params) { | ||
326 | user, err := m.g.Charakterin.GetUserFromRequest(r) | ||
327 | if err != nil { | ||
328 | log.Println("invalid updateListSettings user") | ||
329 | http.Redirect(w, r, fmt.Sprintf("/list/%s", p.ByName("id")), 403) | ||
330 | return | ||
331 | } | ||
332 | |||
333 | id, err := strconv.Atoi(p.ByName("id")) | ||
334 | if err != nil { | ||
335 | log.Println("invalid updateListSettings ID") | ||
336 | http.Redirect(w, r, "/", 400) | ||
337 | return | ||
338 | } | ||
339 | |||
340 | list, err := m.FromID(id, false) | ||
341 | if err != nil { | ||
342 | log.Println("invalid updateListSettings ID (could not get list)") | ||
343 | http.Redirect(w, r, fmt.Sprintf("/list/%d", id), 500) | ||
344 | return | ||
345 | } | ||
346 | |||
347 | // checken, ob der user auch list owner ist | ||
348 | if user.ID != list.Owner.ID { | ||
349 | log.Println("invalid updateListSettings request: user not list owner") | ||
350 | http.Redirect(w, r, fmt.Sprintf("/list/%d", id), 403) | ||
351 | return | ||
352 | } | ||
353 | |||
354 | renderWithError := func(message string, nameError, descriptionError bool) { | ||
355 | data := m.g.Renderer.DefaultData() | ||
356 | data["user"] = user | ||
357 | data["list"] = list | ||
358 | data["error"] = message | ||
359 | data["error_name"] = nameError | ||
360 | data["error_description"] = descriptionError | ||
361 | |||
362 | m.g.Renderer.RenderPage("list_settings", w, data) | ||
363 | } | ||
364 | |||
365 | values, err := readBody(r) | ||
366 | if err != nil { | ||
367 | log.Println("invalid POST data in updateListSettings") | ||
368 | renderWithError("invalid post data", false, false) | ||
369 | return | ||
370 | } | ||
371 | |||
372 | name := values.Get("name") | ||
373 | if len(name) < 3 { | ||
374 | renderWithError("Name zu kurz", true, false) | ||
375 | return | ||
376 | } else if len(name) > 32 { | ||
377 | renderWithError("Name zu lang", true, false) | ||
378 | return | ||
379 | } | ||
380 | |||
381 | description := values.Get("description") | ||
382 | if len(description) < 3 { | ||
383 | renderWithError("Beschreibung zu kurz", false, true) | ||
384 | return | ||
385 | } else if len(description) > 100 { | ||
386 | renderWithError("Beschreibung zu lang", false, true) | ||
387 | return | ||
388 | } | ||
389 | |||
390 | list.Name = name | ||
391 | list.Description = description | ||
392 | |||
393 | _, err = m.g.DB.Query(`UPDATE grilist.lists SET name = $2, description = $3 WHERE id = $1`, id, name, description) | ||
394 | if err != nil { | ||
395 | log.Println("could not update list:", err) | ||
396 | renderWithError("interner fehler", false, false) | ||
397 | return | ||
398 | } | ||
399 | |||
400 | m.viewListSettings(w, r, p) | ||
401 | } | ||
402 | |||
324 | func (m *Module) addGrilToList(w http.ResponseWriter, r *http.Request, p httprouter.Params) { | 403 | func (m *Module) addGrilToList(w http.ResponseWriter, r *http.Request, p httprouter.Params) { |
325 | slistID := p.ByName("id") | 404 | slistID := p.ByName("id") |
326 | 405 | ||