aboutsummaryrefslogtreecommitdiff
path: root/modules/lists/lists.go
diff options
context:
space:
mode:
Diffstat (limited to 'modules/lists/lists.go')
-rw-r--r--modules/lists/lists.go44
1 files changed, 42 insertions, 2 deletions
diff --git a/modules/lists/lists.go b/modules/lists/lists.go
index 8957c5f..85e6ce5 100644
--- a/modules/lists/lists.go
+++ b/modules/lists/lists.go
@@ -15,6 +15,7 @@ import (
15 "net/http" 15 "net/http"
16 "net/url" 16 "net/url"
17 "strconv" 17 "strconv"
18 "sort"
18) 19)
19 20
20// Module und so. 21// Module und so.
@@ -40,6 +41,12 @@ type ListGril struct {
40 Order int 41 Order int
41} 42}
42 43
44// ListGrils ist die Sort-Interface Implementation für Grils einer Liste.
45type ListGrils []*ListGril
46func (l ListGrils) Len() int { return len(l) }
47func (l ListGrils) Swap(i, j int) { l[i], l[j] = l[j], l[i] }
48func (l ListGrils) Less(i, j int) bool { return l[i].Order < l[j].Order }
49
43// Name gibt den Namen des Moduls zurück 50// Name gibt den Namen des Moduls zurück
44func (m *Module) Name() string { 51func (m *Module) Name() string {
45 return "Lists" 52 return "Lists"
@@ -90,6 +97,7 @@ func (m *Module) getListGrils(list *List) error {
90 97
91 list.Grils = append(list.Grils, lg) 98 list.Grils = append(list.Grils, lg)
92 } 99 }
100 sort.Sort(ListGrils(list.Grils))
93 return nil 101 return nil
94} 102}
95 103
@@ -258,8 +266,40 @@ func (m *Module) addGrilToList(w http.ResponseWriter, r *http.Request, p httprou
258 return 266 return
259 } 267 }
260 268
261 log.Println("implement: add gril", grilID, "to list", listID) 269 list, err := m.FromID(listID)
262 http.Error(w, "not implemented", 500) 270 if err != nil {
271 http.Error(w, "invalid list", 404)
272 return
273 }
274
275 rank := 0
276 if len(list.Grils) > 0 {
277 rank = list.Grils[len(list.Grils) - 1].Order + 1
278 }
279
280 // rein in die DB damit
281 _, err = m.g.DB.Query(`INSERT INTO grilist.lists_grils(list_id, gril_id, "order") VALUES($1, $2, $3)`, listID, grilID, rank)
282 if err != nil {
283 log.Println("error inserting gril into list:", err)
284 http.Error(w, "could not insert gril", 500)
285 return
286 }
287
288 gril, err := m.grils.FromID(grilID)
289 if err != nil {
290 log.Println("inserted gril into list but couldnt get gril afterwards:", err)
291 http.Error(w, "error after insert", 500)
292 return
293 }
294
295 data := m.g.Renderer.DefaultData()
296 data["Index"] = len(list.Grils)
297 value := make(map[string]interface{})
298 value["IsListOwner"] = true
299 value["Gril"] = ListGril{ gril, rank }
300 data["Value"] = value
301 m.g.Renderer.RenderPage("list_gril", w, data)
302 return
263} 303}
264 304
265func (m *Module) displayCreateList(w http.ResponseWriter, r *http.Request, p httprouter.Params) { 305func (m *Module) displayCreateList(w http.ResponseWriter, r *http.Request, p httprouter.Params) {