aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorjan <jan@ruken.pw>2016-10-10 11:03:30 (UTC)
committerjan <jan@ruken.pw>2016-10-10 11:03:30 (UTC)
commit5b069f567cc45475a7d2562150de6550d03634f5 (patch)
treeb38e5ed69425166c4e131087361fd8bb85bfe4bf
parent49f7ec4e634c22c07df461b45840e6bd0830a88b (diff)
listeneinstellungen funktionieren
wir sollten vielleicht nur in die Datenbank schreiben, wenn auch was geaendert ist.
-rw-r--r--modules/lists/lists.go79
-rw-r--r--views/pages/list_settings.html9
2 files changed, 84 insertions, 4 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
325func (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
324func (m *Module) addGrilToList(w http.ResponseWriter, r *http.Request, p httprouter.Params) { 403func (m *Module) addGrilToList(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
325 slistID := p.ByName("id") 404 slistID := p.ByName("id")
326 405
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 @@
10 <body> 10 <body>
11 {{ template "navbar" . }} 11 {{ template "navbar" . }}
12 <div class="container"> 12 <div class="container">
13 <h1>Listeneinstellungen</h1> 13 <h1 class="row valign-wrapper"><a href="/list/{{ .list.ID}}" class="col s1 btn purple lighten-4" style="width: auto"><i class="material-icons">arrow_back</i></a><span class="col s11"> Listeneinstellungen</span></h1>
14 <div class="row"> 14 <div class="row">
15 <div class="col s12 m6 offset-m3 l4 offset-l4"> 15 <div class="col s12 m6 offset-m3 l4 offset-l4">
16 {{ if .error }} 16 {{ if .error }}
@@ -23,14 +23,15 @@
23 </div> 23 </div>
24 </div> 24 </div>
25 {{ end }} 25 {{ end }}
26 <form method="POST" name="list" action="#"> 26 <form method="POST" name="list" action="">
27 <div class="input-field col s12"> 27 <div class="input-field col s12">
28 <i class="material-icons prefix">assignment_ind</i> 28 <i class="material-icons prefix">assignment_ind</i>
29 <input name="name" type="text" placeholder="Name" class="validate" value="{{ .list.Name }}"/> 29 <input id="name" name="name" type="text" placeholder="Name" class="{{ if .error_name }}invalid{{ end }}" value="{{ .list.Name }}"/>
30 <label for="name">Name</label>
30 </div> 31 </div>
31 <div class="input-field col s12"> 32 <div class="input-field col s12">
32 <i class="material-icons prefix">mode_edit</i> 33 <i class="material-icons prefix">mode_edit</i>
33 <textarea id="description" name="description" class="materialize-textarea">{{ .list.Description }}</textarea> 34 <textarea id="description" name="description" class="materialize-textarea {{ if .error_description }}invalid{{ end }}">{{ .list.Description }}</textarea>
34 <label for="description">Beschreibung</label> 35 <label for="description">Beschreibung</label>
35 </div> 36 </div>
36 <div class="col s12"> 37 <div class="col s12">