aboutsummaryrefslogtreecommitdiff
path: root/modules/lists
diff options
context:
space:
mode:
authorJan C <jan@ruken.pw>2016-02-20 15:29:33 (UTC)
committerJan C <jan@ruken.pw>2016-02-20 15:29:33 (UTC)
commit3e34dfa6ebbc0d70c4e704fd7f57dca25ecb8c63 (patch)
tree351d02b87d5f6c285b537a1e178b1528f6e5d3f4 /modules/lists
parent0d76691705dbbaa8b3972e09ed8df1e590b38da5 (diff)
möglichkeit grils direkt über die gril view zu einer liste hinzuzufügen
Diffstat (limited to 'modules/lists')
-rw-r--r--modules/lists/lists.go67
1 files changed, 53 insertions, 14 deletions
diff --git a/modules/lists/lists.go b/modules/lists/lists.go
index e1b806d..a42c51c 100644
--- a/modules/lists/lists.go
+++ b/modules/lists/lists.go
@@ -2,6 +2,7 @@ package lists
2 2
3import ( 3import (
4 "database/sql" 4 "database/sql"
5 "encoding/json"
5 "errors" 6 "errors"
6 "fagott.pw/charakterin" 7 "fagott.pw/charakterin"
7 "fagott.pw/grilist/frontend" 8 "fagott.pw/grilist/frontend"
@@ -72,6 +73,8 @@ func (m *Module) Init(g *grilist.Grilist) {
72 m.g.Router.DELETE("/list/:id/order", m.removeGrilFromList) 73 m.g.Router.DELETE("/list/:id/order", m.removeGrilFromList)
73 m.g.Router.GET("/new/list", m.displayCreateList) 74 m.g.Router.GET("/new/list", m.displayCreateList)
74 m.g.Router.POST("/new/list", m.createList) 75 m.g.Router.POST("/new/list", m.createList)
76
77 m.g.Router.GET("/api/lists/user", m.APIgetUserLists)
75} 78}
76 79
77func (m *Module) getListGrils(list *List) error { 80func (m *Module) getListGrils(list *List) error {
@@ -120,11 +123,6 @@ func (m *Module) getLists(whereClause string, params ...interface{}) ([]*List, e
120 continue 123 continue
121 } 124 }
122 125
123 if err := m.getListGrils(list); err != nil {
124 log.Println(err)
125 continue
126 }
127
128 // Owner kriegn 126 // Owner kriegn
129 owner, err := m.g.Charakterin.GetUserByID(ownerID) 127 owner, err := m.g.Charakterin.GetUserByID(ownerID)
130 if err != nil { 128 if err != nil {
@@ -140,20 +138,36 @@ func (m *Module) getLists(whereClause string, params ...interface{}) ([]*List, e
140} 138}
141 139
142// GetUserLists gibt die Listen eines Benutzers zurück. 140// GetUserLists gibt die Listen eines Benutzers zurück.
143func (m *Module) GetUserLists(u *charakterin.User) []*List { 141func (m *Module) GetUserLists(u *charakterin.User, withGrils bool) []*List {
144 lists, err := m.getLists(`user_id = $1`, u.ID) 142 lists, err := m.getLists(`user_id = $1`, u.ID)
145 if err != nil { 143 if err != nil {
146 log.Println(err) 144 log.Println(err)
147 } 145 }
146
147 if withGrils {
148 for _, list := range lists {
149 if err := m.getListGrils(list); err != nil {
150 log.Println(err)
151 }
152 }
153 }
148 return lists 154 return lists
149} 155}
150 156
151// FromID sucht nach der Liste mit der gegebenen ID und gibt sie, falls sie existiert, zurück. 157// FromID sucht nach der Liste mit der gegebenen ID und gibt sie, falls sie existiert, zurück.
152func (m *Module) FromID(id int) (*List, error) { 158func (m *Module) FromID(id int, withGrils bool) (*List, error) {
153 lists, err := m.getLists(`id = $1`, id) 159 lists, err := m.getLists(`id = $1`, id)
154 if err != nil { 160 if err != nil {
155 return nil, err 161 return nil, err
156 } 162 }
163
164 if withGrils {
165 for _, list := range lists {
166 if err := m.getListGrils(list); err != nil {
167 return nil, err
168 }
169 }
170 }
157 171
158 if len(lists) == 0 { 172 if len(lists) == 0 {
159 return nil, errors.New("no list found") 173 return nil, errors.New("no list found")
@@ -199,6 +213,11 @@ func (m *Module) ProvideDashboardData(user *charakterin.User) []grilist.Dashboar
199 log.Println(err) 213 log.Println(err)
200 return categories 214 return categories
201 } 215 }
216 for _, list := range lists {
217 if err := m.getListGrils(list); err != nil {
218 log.Println(err)
219 }
220 }
202 221
203 categories = append(categories, grilist.DashboardCategory{ 222 categories = append(categories, grilist.DashboardCategory{
204 Title: "Neueste Listen", 223 Title: "Neueste Listen",
@@ -210,11 +229,7 @@ func (m *Module) ProvideDashboardData(user *charakterin.User) []grilist.Dashboar
210 } 229 }
211 230
212 // Listen des Benutzers 231 // Listen des Benutzers
213 lists, err = m.getLists(`user_id = $1`, user.ID) 232 lists = m.GetUserLists(user, true)
214 if err != nil {
215 log.Println(err)
216 return categories
217 }
218 233
219 categories = append(categories, grilist.DashboardCategory{ 234 categories = append(categories, grilist.DashboardCategory{
220 Title: "Meine Listen", 235 Title: "Meine Listen",
@@ -234,7 +249,7 @@ func (m *Module) viewList(w http.ResponseWriter, r *http.Request, p httprouter.P
234 return 249 return
235 } 250 }
236 251
237 list, err := m.FromID(id) 252 list, err := m.FromID(id, true)
238 if err != nil { 253 if err != nil {
239 http.Redirect(w, r, "/", 302) 254 http.Redirect(w, r, "/", 302)
240 return 255 return
@@ -274,7 +289,7 @@ func (m *Module) addGrilToList(w http.ResponseWriter, r *http.Request, p httprou
274 return 289 return
275 } 290 }
276 291
277 list, err := m.FromID(listID) 292 list, err := m.FromID(listID, true)
278 if err != nil { 293 if err != nil {
279 http.Error(w, "invalid list", 404) 294 http.Error(w, "invalid list", 404)
280 return 295 return
@@ -439,6 +454,30 @@ func (m *Module) removeGrilFromList(w http.ResponseWriter, r *http.Request, p ht
439 return 454 return
440} 455}
441 456
457func (m *Module) APIgetUserLists(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
458 user, err := m.g.Charakterin.GetUserFromRequest(r)
459 if err != nil {
460 http.Error(w, "403", http.StatusForbidden)
461 return
462 }
463
464 lists := m.GetUserLists(user, false)
465
466 for _, list := range lists {
467 // owner wegen SICHERHEIT rausfiltern (nodumppassword2k16)
468 list.Owner = nil
469 }
470
471 data, err := json.Marshal(&lists)
472 if err != nil {
473 log.Println(err)
474 http.Error(w, "500", http.StatusInternalServerError)
475 return
476 }
477 w.WriteHeader(200)
478 w.Write(data)
479}
480
442// New erstellt eine neue Instanz des Modules 481// New erstellt eine neue Instanz des Modules
443func New() *Module { 482func New() *Module {
444 return &Module{} 483 return &Module{}