From f239fc2678f2f4e458cf10a4ab30d54acc5b92c2 Mon Sep 17 00:00:00 2001
From: jan <jan@ruken.pw>
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