From 50d50d37809fef22b08c0b260302a5e96ad35694 Mon Sep 17 00:00:00 2001 From: jan Date: Tue, 22 Dec 2015 17:28:21 +0100 Subject: kleines cleanup 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 ( "fagott.pw/grilist/frontend" "fagott.pw/grilist/grilist" "fagott.pw/grilist/modules/grils" + "fmt" "log" + "time" ) -type ListsModule struct { - g *grilist.Grilist +// Module und so. +type Module struct { + g *grilist.Grilist grils *grils.GrilsModule } +// List ist eine Liste an DINGEN. type List struct { - ID int - Name string + ID int + Name string Description string - Owner *charakterin.User + Owner *charakterin.User + ForkOf int + UpdatedAt time.Time } -func (m *ListsModule) Name() string { +// Name gibt den Namen des Moduls zurück +func (m *Module) Name() string { return "Lists" } -func (m *ListsModule) Init(g *grilist.Grilist) { +// Init initialisiert das Modul +func (m *Module) Init(g *grilist.Grilist) { m.g = g - + gm, ok := g.Modules["Grils"] if !ok { log.Fatal("lists: grils module not found") } - + grilsModule, ok := gm.Interface().(*grils.GrilsModule) if !ok { log.Fatal("lists: error with grils module") @@ -39,39 +47,74 @@ func (m *ListsModule) Init(g *grilist.Grilist) { m.grils = grilsModule } -func (m *ListsModule) Interface() interface{} { +// Interface gibt das Modul zurück +func (m *Module) Interface() interface{} { return m } -func (m *ListsModule) ProvideDashboardData(user *charakterin.User) grilist.DashboardCategory { +// GetUserLists gibt die Listen eines Benutzers zurück. +func (m *Module) GetUserLists(u *charakterin.User) []*List { + var lists []*List + + rows, err := m.g.DB.Query(`SELECT id, name, description, fork_of, updated_at FROM grilist.lists WHERE user_id = $1`, u.ID) + if err != nil { + return lists + } + + defer rows.Close() + for rows.Next() { + list := &List{} + + if err := rows.Scan(&list.ID, &list.Name, &list.Description, &list.ForkOf, &list.UpdatedAt); err != nil { + log.Println("error scanning row", err) + continue + } + + lists = append(lists, list) + } + + return lists +} + +func mkCard(title, description string, actions ...frontend.Action) frontend.Card { + card := frontend.Card{ + Title: title, + Description: description, + Size: "medium", + } + + for _, action := range actions { + card.Actions = append(card.Actions, action) + } + + return card +} + +func (m *Module) ProvideDashboardData(user *charakterin.User) grilist.DashboardCategory { data := grilist.DashboardCategory{ Title: "Meine Listen", } - + rows, err := m.g.DB.Query(`SELECT id, name, description FROM grilist.lists WHERE user_id = $1`, user.ID) if err != nil { return data } - + defer rows.Close() for rows.Next() { - card := frontend.Card{} - view := frontend.Action{ - Name: "anguckieren", - } var id int - if err := rows.Scan(&id, &card.Title, &card.Description); err != nil { - log.Println("error scanning row",err) + var title, description string + if err := rows.Scan(&id, &title, &description); err != nil { + log.Println("error scanning row", err) continue } - - card.Actions = []frontend.Action{view} - data.Cards = append(data.Cards, card) + + data.Cards = append(data.Cards, mkCard(title, description, frontend.Action{"anguckieren", fmt.Sprintf("/lists/%d", id)})) } - + return data } -func New() *ListsModule { - return &ListsModule{} +func New() *Module { + return &Module{} } -- cgit v0.10.1 From f239fc2678f2f4e458cf10a4ab30d54acc5b92c2 Mon Sep 17 00:00:00 2001 From: jan Date: Tue, 22 Dec 2015 19:01:08 +0100 Subject: mehr cleanup, neue Listen anzeigen auf dem Dashboard diff --git a/dashboard.go b/dashboard.go index aa6405c..6e81d6b 100644 --- a/dashboard.go +++ b/dashboard.go @@ -1,9 +1,9 @@ package main import ( - "net/http" "fagott.pw/grilist/grilist" "github.com/julienschmidt/httprouter" + "net/http" ) func viewDashboard(w http.ResponseWriter, r *http.Request, _ httprouter.Params) { @@ -12,18 +12,18 @@ func viewDashboard(w http.ResponseWriter, r *http.Request, _ httprouter.Params) http.Redirect(w, r, "/", 302) return } - + var categories []grilist.DashboardCategory - + for _, module := range app.Modules { - category := module.ProvideDashboardData(user) - - if len(category.Cards) > 0 { - categories = append(categories, category) + for _, category := range module.ProvideDashboardData(user) { + if len(category.Cards) > 0 { + categories = append(categories, category) + } } } - + data := make(map[string]interface{}) data["categories"] = categories app.Renderer.RenderPage("dashboard", w, data) -} \ No newline at end of file +} 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 { Init(*Grilist) Interface() interface{} Name() string - ProvideDashboardData(*charakterin.User) DashboardCategory + ProvideDashboardData(*charakterin.User) []DashboardCategory } // 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 ( ) type GrilsModule struct { - g *grilist.Grilist + g *grilist.Grilist Test []int } @@ -16,16 +16,16 @@ func (m *GrilsModule) Name() string { func (m *GrilsModule) Init(g *grilist.Grilist) { m.g = g - m.Test = append(m.Test, len(m.Test) + 1) + m.Test = append(m.Test, len(m.Test)+1) } func (m *GrilsModule) Interface() interface{} { - m.Test = append(m.Test, len(m.Test) + 1) + m.Test = append(m.Test, len(m.Test)+1) return m } -func (m *GrilsModule) ProvideDashboardData(user *charakterin.User) grilist.DashboardCategory { - return grilist.DashboardCategory{} +func (m *GrilsModule) ProvideDashboardData(user *charakterin.User) []grilist.DashboardCategory { + return []grilist.DashboardCategory{} } func New() *GrilsModule { diff --git a/modules/lists/lists.go b/modules/lists/lists.go index 24d774e..343ba62 100644 --- a/modules/lists/lists.go +++ b/modules/lists/lists.go @@ -1,6 +1,7 @@ package lists import ( + "database/sql" "fagott.pw/charakterin" "fagott.pw/grilist/frontend" "fagott.pw/grilist/grilist" @@ -90,17 +91,9 @@ func mkCard(title, description string, actions ...frontend.Action) frontend.Card return card } -func (m *Module) ProvideDashboardData(user *charakterin.User) grilist.DashboardCategory { - data := grilist.DashboardCategory{ - Title: "Meine Listen", - } - - rows, err := m.g.DB.Query(`SELECT id, name, description FROM grilist.lists WHERE user_id = $1`, user.ID) - if err != nil { - return data - } +func rowsToCard(rows *sql.Rows) []frontend.Card { + var cards []frontend.Card - defer rows.Close() for rows.Next() { var id int var title, description string @@ -109,12 +102,45 @@ func (m *Module) ProvideDashboardData(user *charakterin.User) grilist.DashboardC continue } - data.Cards = append(data.Cards, mkCard(title, description, frontend.Action{"anguckieren", fmt.Sprintf("/lists/%d", id)})) + cards = append(cards, mkCard(title, description, frontend.Action{"anguckieren", fmt.Sprintf("/lists/%d", id)})) } + return cards +} + +// ProvideDashboardData gibt Daten für das Dashboard bezogen auf den Benutzer zurück +func (m *Module) ProvideDashboardData(user *charakterin.User) []grilist.DashboardCategory { + var categories []grilist.DashboardCategory + + // Listen des Benutzers + rows, err := m.g.DB.Query(`SELECT id, name, description FROM grilist.lists WHERE user_id = $1`, user.ID) + if err != nil { + log.Println(err) + return []grilist.DashboardCategory{} + } + defer rows.Close() + + categories = append(categories, grilist.DashboardCategory{ + Title: "Meine Listen", + Cards: rowsToCard(rows), + }) + + // Neue Listen + rows, err = m.g.DB.Query(`SELECT id, name, description FROM grilist.lists ORDER BY id DESC LIMIT 5`) + if err != nil { + log.Println(err) + return categories + } + defer rows.Close() + + categories = append(categories, grilist.DashboardCategory{ + Title: "Neueste Listen", + Cards: rowsToCard(rows), + }) - return data + return categories } +// New erstellt eine neue Instanz des Modules func New() *Module { return &Module{} } -- cgit v0.10.1 From 9e6b06475dc6c878dee0c116fc48b9f8130d3ae1 Mon Sep 17 00:00:00 2001 From: jan Date: Tue, 22 Dec 2015 19:05:05 +0100 Subject: Jetzt auch wieder mit Logout 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 @@
+
+ Ausloggen +
{{ range $category := .categories }}

{{ $category.Title }}

-- cgit v0.10.1