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