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