aboutsummaryrefslogtreecommitdiff
path: root/modules
diff options
context:
space:
mode:
Diffstat (limited to 'modules')
-rw-r--r--modules/lists/lists.go75
1 files changed, 41 insertions, 34 deletions
diff --git a/modules/lists/lists.go b/modules/lists/lists.go
index 910162a..90de18c 100644
--- a/modules/lists/lists.go
+++ b/modules/lists/lists.go
@@ -5,9 +5,9 @@ import (
5 "encoding/json" 5 "encoding/json"
6 "errors" 6 "errors"
7 "fagott.pw/charakterin" 7 "fagott.pw/charakterin"
8 "fagott.pw/grilist/cache"
8 "fagott.pw/grilist/frontend" 9 "fagott.pw/grilist/frontend"
9 "fagott.pw/grilist/grilist" 10 "fagott.pw/grilist/grilist"
10 "fagott.pw/grilist/cache"
11 "fagott.pw/grilist/modules/grils" 11 "fagott.pw/grilist/modules/grils"
12 "fmt" 12 "fmt"
13 "github.com/julienschmidt/httprouter" 13 "github.com/julienschmidt/httprouter"
@@ -23,7 +23,7 @@ import (
23// Module und so. 23// Module und so.
24type Module struct { 24type Module struct {
25 g *grilist.Grilist 25 g *grilist.Grilist
26 c *cache.Cache 26 c *cache.Cache
27 grils *grils.GrilsModule 27 grils *grils.GrilsModule
28} 28}
29 29
@@ -78,8 +78,8 @@ func (m *Module) Init(g *grilist.Grilist) {
78 m.g.Router.POST("/new/list", m.createList) 78 m.g.Router.POST("/new/list", m.createList)
79 79
80 m.g.Router.GET("/api/lists/user", m.APIgetUserLists) 80 m.g.Router.GET("/api/lists/user", m.APIgetUserLists)
81 81
82 m.c = cache.New() 82 m.c = cache.New()
83} 83}
84 84
85func (m *Module) getListGrils(list *List) error { 85func (m *Module) getListGrils(list *List) error {
@@ -89,7 +89,7 @@ func (m *Module) getListGrils(list *List) error {
89 } 89 }
90 90
91 defer rows.Close() 91 defer rows.Close()
92 list.Grils = list.Grils[:0] 92 list.Grils = list.Grils[:0]
93 for rows.Next() { 93 for rows.Next() {
94 var grilID int 94 var grilID int
95 lg := &ListGril{} 95 lg := &ListGril{}
@@ -128,12 +128,12 @@ func (m *Module) getLists(whereClause string, params ...interface{}) ([]*List, e
128 log.Println("error scanning row in getLists:", err) 128 log.Println("error scanning row in getLists:", err)
129 continue 129 continue
130 } 130 }
131 131
132 if clist, ok := m.c.Get(list.ID); ok { 132 if clist, ok := m.c.Get(list.ID); ok {
133 // weiteres parsen abbrechen 133 // weiteres parsen abbrechen
134 lists = append(lists, clist.(*List)) 134 lists = append(lists, clist.(*List))
135 continue 135 continue
136 } 136 }
137 137
138 // Owner kriegn 138 // Owner kriegn
139 owner, err := m.g.Charakterin.GetUserByID(ownerID) 139 owner, err := m.g.Charakterin.GetUserByID(ownerID)
@@ -142,8 +142,8 @@ func (m *Module) getLists(whereClause string, params ...interface{}) ([]*List, e
142 continue 142 continue
143 } 143 }
144 list.Owner = owner 144 list.Owner = owner
145 145
146 m.c.Insert(list.ID, list) 146 m.c.Insert(list.ID, list)
147 lists = append(lists, list) 147 lists = append(lists, list)
148 } 148 }
149 149
@@ -169,16 +169,16 @@ func (m *Module) GetUserLists(u *charakterin.User, withGrils bool) []*List {
169 169
170// FromID sucht nach der Liste mit der gegebenen ID und gibt sie, falls sie existiert, zurück. 170// FromID sucht nach der Liste mit der gegebenen ID und gibt sie, falls sie existiert, zurück.
171func (m *Module) FromID(id int, withGrils bool) (*List, error) { 171func (m *Module) FromID(id int, withGrils bool) (*List, error) {
172 if lst, ok := m.c.Get(id); ok { 172 if lst, ok := m.c.Get(id); ok {
173 l := lst.(*List) 173 l := lst.(*List)
174 // Potenzieller Optimierungsbedarf: neue query, wenn die Liste leer ist. updateGrilOrder nutzt derzeit aber genau dieses verhalten. 174 // Potenzieller Optimierungsbedarf: neue query, wenn die Liste leer ist. updateGrilOrder nutzt derzeit aber genau dieses verhalten.
175 if withGrils && len(l.Grils) == 0 { 175 if withGrils && len(l.Grils) == 0 {
176 if err := m.getListGrils(l); err != nil { 176 if err := m.getListGrils(l); err != nil {
177 return nil, err 177 return nil, err
178 } 178 }
179 } 179 }
180 return l, nil 180 return l, nil
181 } 181 }
182 182
183 lists, err := m.getLists(`id = $1`, id) 183 lists, err := m.getLists(`id = $1`, id)
184 if err != nil { 184 if err != nil {
@@ -233,11 +233,11 @@ func (m *Module) ProvideDashboardData(user *charakterin.User) []grilist.Dashboar
233 log.Println(err) 233 log.Println(err)
234 return categories 234 return categories
235 } 235 }
236 236
237 for _, list := range lists { 237 for _, list := range lists {
238 if err := m.getListGrils(list); err != nil { 238 if err := m.getListGrils(list); err != nil {
239 log.Println(err) 239 log.Println(err)
240 } 240 }
241 } 241 }
242 242
243 categories = append(categories, grilist.DashboardCategory{ 243 categories = append(categories, grilist.DashboardCategory{
@@ -266,14 +266,14 @@ func (m *Module) viewList(w http.ResponseWriter, r *http.Request, p httprouter.P
266 266
267 id, err := strconv.Atoi(sid) 267 id, err := strconv.Atoi(sid)
268 if err != nil { 268 if err != nil {
269 log.Println("redir") 269 log.Println("redir")
270 http.Redirect(w, r, "/", 302) 270 http.Redirect(w, r, "/", 302)
271 return 271 return
272 } 272 }
273 273
274 list, err := m.FromID(id, true) 274 list, err := m.FromID(id, true)
275 if err != nil { 275 if err != nil {
276 log.Println("redir") 276 log.Println("redir")
277 http.Redirect(w, r, "/", 302) 277 http.Redirect(w, r, "/", 302)
278 return 278 return
279 } 279 }
@@ -343,12 +343,14 @@ func (m *Module) addGrilToList(w http.ResponseWriter, r *http.Request, p httprou
343 return 343 return
344 } 344 }
345 345
346 lg := &ListGril{gril, rank}
346 data := m.g.Renderer.DefaultData() 347 data := m.g.Renderer.DefaultData()
347 data["Index"] = len(list.Grils) 348 data["Index"] = len(list.Grils)
348 value := make(map[string]interface{}) 349 value := make(map[string]interface{})
349 value["IsListOwner"] = true 350 value["IsListOwner"] = true
350 value["Gril"] = ListGril{gril, rank} 351 value["Gril"] = lg
351 data["Value"] = value 352 data["Value"] = value
353 list.Grils = append(list.Grils, lg)
352 m.g.Renderer.RenderPage("list_gril", w, data) 354 m.g.Renderer.RenderPage("list_gril", w, data)
353 return 355 return
354} 356}
@@ -432,12 +434,12 @@ func (m *Module) updateGrilOrder(w http.ResponseWriter, r *http.Request, p httpr
432 http.Error(w, "could not update gril order", 500) 434 http.Error(w, "could not update gril order", 500)
433 return 435 return
434 } 436 }
435 437
436 // wenn die liste im cache ist, die Grils clearen, damit beim naechsten aufruf die Gril-Liste neu geholt wird. 438 // wenn die liste im cache ist, die Grils clearen, damit beim naechsten aufruf die Gril-Liste neu geholt wird.
437 if l, ok := m.c.Get(listID); ok { 439 if l, ok := m.c.Get(listID); ok {
438 ls := l.(*List) 440 ls := l.(*List)
439 ls.Grils = ls.Grils[:0] 441 ls.Grils = ls.Grils[:0]
440 } 442 }
441 443
442 w.WriteHeader(200) 444 w.WriteHeader(200)
443 w.Write([]byte("ok")) 445 w.Write([]byte("ok"))
@@ -478,6 +480,11 @@ func (m *Module) removeGrilFromList(w http.ResponseWriter, r *http.Request, p ht
478 return 480 return
479 } 481 }
480 482
483 if l, ok := m.c.Get(listID); ok {
484 ls := l.(*List)
485 ls.Grils = ls.Grils[:0]
486 }
487
481 w.WriteHeader(200) 488 w.WriteHeader(200)
482 w.Write([]byte("ok")) 489 w.Write([]byte("ok"))
483 return 490 return