aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorrtz12 <koenig@fagott.pw>2015-12-22 21:30:55 (UTC)
committerrtz12 <koenig@fagott.pw>2015-12-22 21:30:55 (UTC)
commit213a8d2e3ba4fcaff0b750bd493bf8f94f21dd31 (patch)
tree16e02d775af1052d40f4658bd8865daff9097180
parent9907271d4fddee0bafbba0d534a2c041156034a8 (diff)
parent9e6b06475dc6c878dee0c116fc48b9f8130d3ae1 (diff)
Merge branch 'master' of projekte.fagott.pw:grilist
-rw-r--r--dashboard.go18
-rw-r--r--grilist/grilist.go2
-rw-r--r--modules/grils/grils.go10
-rw-r--r--modules/lists/lists.go129
-rw-r--r--views/dashboard.html3
5 files changed, 117 insertions, 45 deletions
diff --git a/dashboard.go b/dashboard.go
index aa6405c..6e81d6b 100644
--- a/dashboard.go
+++ b/dashboard.go
@@ -1,9 +1,9 @@
1package main 1package main
2 2
3import ( 3import (
4 "net/http"
5 "fagott.pw/grilist/grilist" 4 "fagott.pw/grilist/grilist"
6 "github.com/julienschmidt/httprouter" 5 "github.com/julienschmidt/httprouter"
6 "net/http"
7) 7)
8 8
9func viewDashboard(w http.ResponseWriter, r *http.Request, _ httprouter.Params) { 9func viewDashboard(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
@@ -12,18 +12,18 @@ func viewDashboard(w http.ResponseWriter, r *http.Request, _ httprouter.Params)
12 http.Redirect(w, r, "/", 302) 12 http.Redirect(w, r, "/", 302)
13 return 13 return
14 } 14 }
15 15
16 var categories []grilist.DashboardCategory 16 var categories []grilist.DashboardCategory
17 17
18 for _, module := range app.Modules { 18 for _, module := range app.Modules {
19 category := module.ProvideDashboardData(user) 19 for _, category := range module.ProvideDashboardData(user) {
20 20 if len(category.Cards) > 0 {
21 if len(category.Cards) > 0 { 21 categories = append(categories, category)
22 categories = append(categories, category) 22 }
23 } 23 }
24 } 24 }
25 25
26 data := make(map[string]interface{}) 26 data := make(map[string]interface{})
27 data["categories"] = categories 27 data["categories"] = categories
28 app.Renderer.RenderPage("dashboard", w, data) 28 app.Renderer.RenderPage("dashboard", w, data)
29} \ No newline at end of file 29}
diff --git a/grilist/grilist.go b/grilist/grilist.go
index b166966..baff81c 100644
--- a/grilist/grilist.go
+++ b/grilist/grilist.go
@@ -25,7 +25,7 @@ type Module interface {
25 Init(*Grilist) 25 Init(*Grilist)
26 Interface() interface{} 26 Interface() interface{}
27 Name() string 27 Name() string
28 ProvideDashboardData(*charakterin.User) DashboardCategory 28 ProvideDashboardData(*charakterin.User) []DashboardCategory
29} 29}
30 30
31// DashboardCategory ist eine Kategorie mit Karten, die auf dem Dashboard angezeigt werden. 31// DashboardCategory ist eine Kategorie mit Karten, die auf dem Dashboard angezeigt werden.
diff --git a/modules/grils/grils.go b/modules/grils/grils.go
index 6ce7c5e..fa1f21c 100644
--- a/modules/grils/grils.go
+++ b/modules/grils/grils.go
@@ -6,7 +6,7 @@ import (
6) 6)
7 7
8type GrilsModule struct { 8type GrilsModule struct {
9 g *grilist.Grilist 9 g *grilist.Grilist
10 Test []int 10 Test []int
11} 11}
12 12
@@ -16,16 +16,16 @@ func (m *GrilsModule) Name() string {
16 16
17func (m *GrilsModule) Init(g *grilist.Grilist) { 17func (m *GrilsModule) Init(g *grilist.Grilist) {
18 m.g = g 18 m.g = g
19 m.Test = append(m.Test, len(m.Test) + 1) 19 m.Test = append(m.Test, len(m.Test)+1)
20} 20}
21 21
22func (m *GrilsModule) Interface() interface{} { 22func (m *GrilsModule) Interface() interface{} {
23 m.Test = append(m.Test, len(m.Test) + 1) 23 m.Test = append(m.Test, len(m.Test)+1)
24 return m 24 return m
25} 25}
26 26
27func (m *GrilsModule) ProvideDashboardData(user *charakterin.User) grilist.DashboardCategory { 27func (m *GrilsModule) ProvideDashboardData(user *charakterin.User) []grilist.DashboardCategory {
28 return grilist.DashboardCategory{} 28 return []grilist.DashboardCategory{}
29} 29}
30 30
31func New() *GrilsModule { 31func New() *GrilsModule {
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}
diff --git a/views/dashboard.html b/views/dashboard.html
index 67fb005..a000b57 100644
--- a/views/dashboard.html
+++ b/views/dashboard.html
@@ -7,6 +7,9 @@
7 </head> 7 </head>
8 <body> 8 <body>
9 <div class="container"> 9 <div class="container">
10 <div class="row right-align">
11 <a class="waves-effect waves-teal btn-flat large" style="top: 24px; font-size: 18px" href="/logout">Ausloggen</a>
12 </div>
10 <div class="row"> 13 <div class="row">
11 {{ range $category := .categories }} 14 {{ range $category := .categories }}
12 <h2>{{ $category.Title }}</h2> 15 <h2>{{ $category.Title }}</h2>