From 4254929eea29a42896fd57185d6f52fdc3efdca4 Mon Sep 17 00:00:00 2001 From: jan Date: Mon, 21 Dec 2015 23:29:10 +0100 Subject: dashboard. FUCKING DASHBOARD! diff --git a/dashboard.go b/dashboard.go index 221e923..aa6405c 100644 --- a/dashboard.go +++ b/dashboard.go @@ -1,3 +1,29 @@ package main -import () +import ( + "net/http" + "fagott.pw/grilist/grilist" + "github.com/julienschmidt/httprouter" +) + +func viewDashboard(w http.ResponseWriter, r *http.Request, _ httprouter.Params) { + user, err := app.Charakterin.GetUserFromRequest(r) + if err != nil { + 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) + } + } + + 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 c672dec..b166966 100644 --- a/grilist/grilist.go +++ b/grilist/grilist.go @@ -23,6 +23,7 @@ type Grilist struct { // Module ist ein Modul für Grilist. type Module interface { Init(*Grilist) + Interface() interface{} Name() string ProvideDashboardData(*charakterin.User) DashboardCategory } diff --git a/main.go b/main.go index e106a80..70763d4 100644 --- a/main.go +++ b/main.go @@ -10,6 +10,7 @@ import ( "fagott.pw/grilist/frontend" "fagott.pw/grilist/grilist" "fagott.pw/grilist/modules/lists" + "fagott.pw/grilist/modules/grils" "github.com/julienschmidt/httprouter" _ "github.com/lib/pq" @@ -18,16 +19,13 @@ import ( var app *grilist.Grilist func index(w http.ResponseWriter, r *http.Request, _ httprouter.Params) { - user, err := app.Charakterin.GetUserFromRequest(r) + _, err := app.Charakterin.GetUserFromRequest(r) if err != nil { http.Redirect(w, r, "/login", 302) return } - data := make(map[string]interface{}) - - data["username"] = user.GetName() - app.Renderer.RenderPage("index", w, data) + http.Redirect(w, r, "/dashboard", 302) } func loadModule(mod grilist.Module) error { @@ -35,6 +33,7 @@ func loadModule(mod grilist.Module) error { return fmt.Errorf("module with name %s already exists", mod.Name()) } app.Modules[mod.Name()] = mod + mod.Init(app) return nil } @@ -71,12 +70,14 @@ func main() { router.HandlerFunc("GET", "/register", login.DisplayRegistration) router.HandlerFunc("POST", "/register", login.Register) router.GET("/", index) + router.GET("/dashboard", viewDashboard) fs := http.FileServer(http.Dir("assets")) http.Handle("/assets/", http.StripPrefix("/assets/", fs)) http.Handle("/", router) // Module laden + loadModule(grils.New()) loadModule(lists.New()) log.Fatal(http.ListenAndServe(":8080", nil)) diff --git a/modules/grils/grils.go b/modules/grils/grils.go new file mode 100644 index 0000000..6ce7c5e --- /dev/null +++ b/modules/grils/grils.go @@ -0,0 +1,33 @@ +package grils + +import ( + "fagott.pw/charakterin" + "fagott.pw/grilist/grilist" +) + +type GrilsModule struct { + g *grilist.Grilist + Test []int +} + +func (m *GrilsModule) Name() string { + return "Grils" +} + +func (m *GrilsModule) Init(g *grilist.Grilist) { + m.g = g + m.Test = append(m.Test, len(m.Test) + 1) +} + +func (m *GrilsModule) Interface() interface{} { + m.Test = append(m.Test, len(m.Test) + 1) + return m +} + +func (m *GrilsModule) ProvideDashboardData(user *charakterin.User) grilist.DashboardCategory { + return grilist.DashboardCategory{} +} + +func New() *GrilsModule { + return &GrilsModule{} +} diff --git a/modules/lists/lists.go b/modules/lists/lists.go index f8f13bb..4e0036a 100644 --- a/modules/lists/lists.go +++ b/modules/lists/lists.go @@ -2,23 +2,74 @@ package lists import ( "fagott.pw/charakterin" + "fagott.pw/grilist/frontend" "fagott.pw/grilist/grilist" + "fagott.pw/grilist/modules/grils" + "log" ) type ListsModule struct { g *grilist.Grilist + grils *grils.GrilsModule } -func (d *ListsModule) Name() string { - return "ListsModule" +type List struct { + ID int + Name string + Description string + Owner *charakterin.User } -func (d *ListsModule) Init(g *grilist.Grilist) { - d.g = g +func (m *ListsModule) Name() string { + return "Lists" } -func (d *ListsModule) ProvideDashboardData(user *charakterin.User) grilist.DashboardCategory { - return grilist.DashboardCategory{} +func (m *ListsModule) 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") + } + m.grils = grilsModule +} + +func (m *ListsModule) Interface() interface{} { + return m +} + +func (m *ListsModule) 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) + continue + } + + card.Actions = []frontend.Action{view} + data.Cards = append(data.Cards, card) + } + + return data } func New() *ListsModule { diff --git a/views/dashboard.html b/views/dashboard.html new file mode 100644 index 0000000..67fb005 --- /dev/null +++ b/views/dashboard.html @@ -0,0 +1,35 @@ +{{ define "dashboard" }} + + + {{ template "materialize" }} + grilist + + + +
+
+ {{ range $category := .categories }} +

{{ $category.Title }}

+ {{ range $card := $category.Cards }} +
+
+
+
+ {{ $card.Title }} +

{{ $card.Description }}

+
+
+ {{ range $action := $card.Actions }} + {{ $action.Name }} + {{ end }} +
+
+
+
+ {{ end }} + {{ end }} +
+
+ + +{{ end }} \ No newline at end of file -- cgit v0.10.1