aboutsummaryrefslogtreecommitdiff
path: root/modules/lists
diff options
context:
space:
mode:
Diffstat (limited to 'modules/lists')
-rw-r--r--modules/lists/lists.go129
1 files changed, 99 insertions, 30 deletions
diff --git a/modules/lists/lists.go b/modules/lists/lists.go
index 4e0036a..343ba62 100644
--- a/modules/lists/lists.go
+++ b/modules/lists/lists.go
@@ -1,37 +1,46 @@
1package lists 1package lists
2 2
3import ( 3import (
4 "database/sql"
4 "fagott.pw/charakterin" 5 "fagott.pw/charakterin"
5 "fagott.pw/grilist/frontend" 6 "fagott.pw/grilist/frontend"
6 "fagott.pw/grilist/grilist" 7 "fagott.pw/grilist/grilist"
7 "fagott.pw/grilist/modules/grils" 8 "fagott.pw/grilist/modules/grils"
9 "fmt"
8 "log" 10 "log"
11 "time"
9) 12)
10 13
11type ListsModule struct { 14// Module und so.
12 g *grilist.Grilist 15type Module struct {
16 g *grilist.Grilist
13 grils *grils.GrilsModule 17 grils *grils.GrilsModule
14} 18}
15 19
20// List ist eine Liste an DINGEN.
16type List struct { 21type List struct {
17 ID int 22 ID int
18 Name string 23 Name string
19 Description string 24 Description string
20 Owner *charakterin.User 25 Owner *charakterin.User
26 ForkOf int
27 UpdatedAt time.Time
21} 28}
22 29
23func (m *ListsModule) Name() string { 30// Name gibt den Namen des Moduls zurück
31func (m *Module) Name() string {
24 return "Lists" 32 return "Lists"
25} 33}
26 34
27func (m *ListsModule) Init(g *grilist.Grilist) { 35// Init initialisiert das Modul
36func (m *Module) Init(g *grilist.Grilist) {
28 m.g = g 37 m.g = g
29 38
30 gm, ok := g.Modules["Grils"] 39 gm, ok := g.Modules["Grils"]
31 if !ok { 40 if !ok {
32 log.Fatal("lists: grils module not found") 41 log.Fatal("lists: grils module not found")
33 } 42 }
34 43
35 grilsModule, ok := gm.Interface().(*grils.GrilsModule) 44 grilsModule, ok := gm.Interface().(*grils.GrilsModule)
36 if !ok { 45 if !ok {
37 log.Fatal("lists: error with grils module") 46 log.Fatal("lists: error with grils module")
@@ -39,39 +48,99 @@ func (m *ListsModule) Init(g *grilist.Grilist) {
39 m.grils = grilsModule 48 m.grils = grilsModule
40} 49}
41 50
42func (m *ListsModule) Interface() interface{} { 51// Interface gibt das Modul zurück
52func (m *Module) Interface() interface{} {
43 return m 53 return m
44} 54}
45 55
46func (m *ListsModule) ProvideDashboardData(user *charakterin.User) grilist.DashboardCategory { 56// GetUserLists gibt die Listen eines Benutzers zurück.
47 data := grilist.DashboardCategory{ 57func (m *Module) GetUserLists(u *charakterin.User) []*List {
48 Title: "Meine Listen", 58 var lists []*List
49 } 59
50 60 rows, err := m.g.DB.Query(`SELECT id, name, description, fork_of, updated_at FROM grilist.lists WHERE user_id = $1`, u.ID)
51 rows, err := m.g.DB.Query(`SELECT id, name, description FROM grilist.lists WHERE user_id = $1`, user.ID)
52 if err != nil { 61 if err != nil {
53 return data 62 return lists
54 } 63 }
55 64
56 defer rows.Close() 65 defer rows.Close()
57 for rows.Next() { 66 for rows.Next() {
58 card := frontend.Card{} 67 list := &List{}
59 view := frontend.Action{ 68
60 Name: "anguckieren", 69 if err := rows.Scan(&list.ID, &list.Name, &list.Description, &list.ForkOf, &list.UpdatedAt); err != nil {
70 log.Println("error scanning row", err)
71 continue
61 } 72 }
73
74 lists = append(lists, list)
75 }
76
77 return lists
78}
79
80func mkCard(title, description string, actions ...frontend.Action) frontend.Card {
81 card := frontend.Card{
82 Title: title,
83 Description: description,
84 Size: "medium",
85 }
86
87 for _, action := range actions {
88 card.Actions = append(card.Actions, action)
89 }
90
91 return card
92}
93
94func rowsToCard(rows *sql.Rows) []frontend.Card {
95 var cards []frontend.Card
96
97 for rows.Next() {
62 var id int 98 var id int
63 if err := rows.Scan(&id, &card.Title, &card.Description); err != nil { 99 var title, description string
64 log.Println("error scanning row",err) 100 if err := rows.Scan(&id, &title, &description); err != nil {
101 log.Println("error scanning row", err)
65 continue 102 continue
66 } 103 }
67 104
68 card.Actions = []frontend.Action{view} 105 cards = append(cards, mkCard(title, description, frontend.Action{"anguckieren", fmt.Sprintf("/lists/%d", id)}))
69 data.Cards = append(data.Cards, card) 106 }
107 return cards
108}
109
110// ProvideDashboardData gibt Daten für das Dashboard bezogen auf den Benutzer zurück
111func (m *Module) ProvideDashboardData(user *charakterin.User) []grilist.DashboardCategory {
112 var categories []grilist.DashboardCategory
113
114 // Listen des Benutzers
115 rows, err := m.g.DB.Query(`SELECT id, name, description FROM grilist.lists WHERE user_id = $1`, user.ID)
116 if err != nil {
117 log.Println(err)
118 return []grilist.DashboardCategory{}
70 } 119 }
71 120 defer rows.Close()
72 return data 121
122 categories = append(categories, grilist.DashboardCategory{
123 Title: "Meine Listen",
124 Cards: rowsToCard(rows),
125 })
126
127 // Neue Listen
128 rows, err = m.g.DB.Query(`SELECT id, name, description FROM grilist.lists ORDER BY id DESC LIMIT 5`)
129 if err != nil {
130 log.Println(err)
131 return categories
132 }
133 defer rows.Close()
134
135 categories = append(categories, grilist.DashboardCategory{
136 Title: "Neueste Listen",
137 Cards: rowsToCard(rows),
138 })
139
140 return categories
73} 141}
74 142
75func New() *ListsModule { 143// New erstellt eine neue Instanz des Modules
76 return &ListsModule{} 144func New() *Module {
145 return &Module{}
77} 146}