aboutsummaryrefslogtreecommitdiff
path: root/modules/lists
diff options
context:
space:
mode:
Diffstat (limited to 'modules/lists')
-rw-r--r--modules/lists/lists.go108
1 files changed, 100 insertions, 8 deletions
diff --git a/modules/lists/lists.go b/modules/lists/lists.go
index 343ba62..519bb36 100644
--- a/modules/lists/lists.go
+++ b/modules/lists/lists.go
@@ -2,12 +2,16 @@ package lists
2 2
3import ( 3import (
4 "database/sql" 4 "database/sql"
5 "errors"
5 "fagott.pw/charakterin" 6 "fagott.pw/charakterin"
6 "fagott.pw/grilist/frontend" 7 "fagott.pw/grilist/frontend"
7 "fagott.pw/grilist/grilist" 8 "fagott.pw/grilist/grilist"
8 "fagott.pw/grilist/modules/grils" 9 "fagott.pw/grilist/modules/grils"
9 "fmt" 10 "fmt"
11 "github.com/julienschmidt/httprouter"
10 "log" 12 "log"
13 "net/http"
14 "strconv"
11 "time" 15 "time"
12) 16)
13 17
@@ -23,8 +27,15 @@ type List struct {
23 Name string 27 Name string
24 Description string 28 Description string
25 Owner *charakterin.User 29 Owner *charakterin.User
26 ForkOf int 30 ForkOf *int
27 UpdatedAt time.Time 31 UpdatedAt *time.Time
32 Grils []*ListGril
33}
34
35// ListGril ist ein geranktes Gril
36type ListGril struct {
37 Gril *grils.Gril
38 Order int
28} 39}
29 40
30// Name gibt den Namen des Moduls zurück 41// Name gibt den Namen des Moduls zurück
@@ -46,6 +57,7 @@ func (m *Module) Init(g *grilist.Grilist) {
46 log.Fatal("lists: error with grils module") 57 log.Fatal("lists: error with grils module")
47 } 58 }
48 m.grils = grilsModule 59 m.grils = grilsModule
60 m.g.Router.GET("/list/:id", m.viewList)
49} 61}
50 62
51// Interface gibt das Modul zurück 63// Interface gibt das Modul zurück
@@ -53,13 +65,40 @@ func (m *Module) Interface() interface{} {
53 return m 65 return m
54} 66}
55 67
56// GetUserLists gibt die Listen eines Benutzers zurück. 68func (m *Module) getListGrils(list *List) error {
57func (m *Module) GetUserLists(u *charakterin.User) []*List { 69 rows, err := m.g.DB.Query(`SELECT gril_id, "order" FROM grilist.lists_grils WHERE list_id = $1`, list.ID)
70 if err != nil {
71 return err
72 }
73
74 defer rows.Close()
75 for rows.Next() {
76 var grilID int
77 lg := &ListGril{}
78
79 if err := rows.Scan(&grilID, &lg.Order); err != nil {
80 log.Println("error scanning row in getListGrils:", err)
81 continue
82 }
83
84 gril, err := m.grils.FromID(grilID)
85 if err != nil {
86 log.Println("error getting listGril:", err)
87 continue
88 }
89 lg.Gril = gril
90
91 list.Grils = append(list.Grils, lg)
92 }
93 return nil
94}
95
96func (m *Module) getLists(whereClause string, params ...interface{}) ([]*List, error) {
58 var lists []*List 97 var lists []*List
59 98
60 rows, err := m.g.DB.Query(`SELECT id, name, description, fork_of, updated_at FROM grilist.lists WHERE user_id = $1`, u.ID) 99 rows, err := m.g.DB.Query(fmt.Sprintf(`SELECT id, name, description, fork_of, updated_at FROM grilist.lists WHERE %s`, whereClause), params...)
61 if err != nil { 100 if err != nil {
62 return lists 101 return nil, err
63 } 102 }
64 103
65 defer rows.Close() 104 defer rows.Close()
@@ -67,16 +106,43 @@ func (m *Module) GetUserLists(u *charakterin.User) []*List {
67 list := &List{} 106 list := &List{}
68 107
69 if err := rows.Scan(&list.ID, &list.Name, &list.Description, &list.ForkOf, &list.UpdatedAt); err != nil { 108 if err := rows.Scan(&list.ID, &list.Name, &list.Description, &list.ForkOf, &list.UpdatedAt); err != nil {
70 log.Println("error scanning row", err) 109 log.Println("error scanning row in getLists:", err)
110 continue
111 }
112
113 if err := m.getListGrils(list); err != nil {
114 log.Println(err)
71 continue 115 continue
72 } 116 }
73 117
74 lists = append(lists, list) 118 lists = append(lists, list)
75 } 119 }
76 120
121 return lists, nil
122}
123
124// GetUserLists gibt die Listen eines Benutzers zurück.
125func (m *Module) GetUserLists(u *charakterin.User) []*List {
126 lists, err := m.getLists(`user_id = $1`, u.ID)
127 if err != nil {
128 log.Println(err)
129 }
77 return lists 130 return lists
78} 131}
79 132
133// FromID sucht nach der Liste mit der gegebenen ID und gibt sie, falls sie existiert, zurück.
134func (m *Module) FromID(id int) (*List, error) {
135 lists, err := m.getLists(`id = $1`, id)
136 if err != nil {
137 return nil, err
138 }
139
140 if len(lists) == 0 {
141 return nil, errors.New("no list found")
142 }
143 return lists[0], nil
144}
145
80func mkCard(title, description string, actions ...frontend.Action) frontend.Card { 146func mkCard(title, description string, actions ...frontend.Action) frontend.Card {
81 card := frontend.Card{ 147 card := frontend.Card{
82 Title: title, 148 Title: title,
@@ -102,7 +168,7 @@ func rowsToCard(rows *sql.Rows) []frontend.Card {
102 continue 168 continue
103 } 169 }
104 170
105 cards = append(cards, mkCard(title, description, frontend.Action{"anguckieren", fmt.Sprintf("/lists/%d", id)})) 171 cards = append(cards, mkCard(title, description, frontend.Action{"anguckieren", fmt.Sprintf("/list/%d", id)}))
106 } 172 }
107 return cards 173 return cards
108} 174}
@@ -140,6 +206,32 @@ func (m *Module) ProvideDashboardData(user *charakterin.User) []grilist.Dashboar
140 return categories 206 return categories
141} 207}
142 208
209func (m *Module) viewList(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
210 loggedIn := false
211 if user, _ := m.g.Charakterin.GetUserFromRequest(r); user != nil {
212 loggedIn = true
213 }
214 sid := p.ByName("id")
215
216 id, err := strconv.Atoi(sid)
217 if err != nil {
218 http.Redirect(w, r, "/", 302)
219 return
220 }
221
222 list, err := m.FromID(id)
223 if err != nil {
224 http.Redirect(w, r, "/", 302)
225 return
226 }
227
228 data := make(map[string]interface{})
229 data["loggedIn"] = loggedIn
230 data["list"] = list
231
232 m.g.Renderer.RenderPage("list", w, data)
233}
234
143// New erstellt eine neue Instanz des Modules 235// New erstellt eine neue Instanz des Modules
144func New() *Module { 236func New() *Module {
145 return &Module{} 237 return &Module{}