From a31b9dbddf94dba7be23fcb67b169e1fe5ffc81b Mon Sep 17 00:00:00 2001 From: jan Date: Fri, 25 Dec 2015 21:37:02 +0100 Subject: simple list & gril view und parsing diff --git a/assets/css/gril.css b/assets/css/gril.css new file mode 100644 index 0000000..409ef51 --- /dev/null +++ b/assets/css/gril.css @@ -0,0 +1,4 @@ +h1 > small { + padding-left: 8px; + font-size: 50%; +} \ No newline at end of file diff --git a/modules/grils/grils.go b/modules/grils/grils.go index 41783a7..047385e 100644 --- a/modules/grils/grils.go +++ b/modules/grils/grils.go @@ -3,12 +3,14 @@ package grils import ( "fagott.pw/charakterin" "fagott.pw/grilist/grilist" + "github.com/julienschmidt/httprouter" + "net/http" + "strconv" "time" ) type GrilsModule struct { - g *grilist.Grilist - Test []int + g *grilist.Grilist } func (m *GrilsModule) Name() string { @@ -17,11 +19,10 @@ 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.g.Router.GET("/gril/:id", m.viewGril) } func (m *GrilsModule) Interface() interface{} { - m.Test = append(m.Test, len(m.Test)+1) return m } @@ -29,7 +30,7 @@ func (m *GrilsModule) ProvideDashboardData(user *charakterin.User) []grilist.Das return []grilist.DashboardCategory{} } -func (m *GrilsModule) FromID(id int) *Gril { +func (m *GrilsModule) FromID(id int) (*Gril, error) { return &Gril{ ID: id, KanjiName: "藤林 杏", @@ -38,7 +39,33 @@ func (m *GrilsModule) FromID(id int) *Gril { Age: 17, Birthday: time.Now(), Tags: []string{"tsundere", "hair intakes", "hair ribbon", "school uniform", "school crest"}, + }, nil +} + +func (m *GrilsModule) viewGril(w http.ResponseWriter, r *http.Request, p httprouter.Params) { + loggedIn := false + if user, _ := m.g.Charakterin.GetUserFromRequest(r); user != nil { + loggedIn = true + } + sid := p.ByName("id") + + id, err := strconv.Atoi(sid) + if err != nil { + http.Redirect(w, r, "/", 302) + return } + + gril, err := m.FromID(id) + if err != nil { + http.Redirect(w, r, "/", 302) + return + } + + data := make(map[string]interface{}) + data["loggedIn"] = loggedIn + data["gril"] = gril + + m.g.Renderer.RenderPage("gril", w, data) } func New() *GrilsModule { diff --git a/modules/lists/lists.go b/modules/lists/lists.go index 343ba62..519bb36 100644 --- a/modules/lists/lists.go +++ b/modules/lists/lists.go @@ -2,12 +2,16 @@ package lists import ( "database/sql" + "errors" "fagott.pw/charakterin" "fagott.pw/grilist/frontend" "fagott.pw/grilist/grilist" "fagott.pw/grilist/modules/grils" "fmt" + "github.com/julienschmidt/httprouter" "log" + "net/http" + "strconv" "time" ) @@ -23,8 +27,15 @@ type List struct { Name string Description string Owner *charakterin.User - ForkOf int - UpdatedAt time.Time + ForkOf *int + UpdatedAt *time.Time + Grils []*ListGril +} + +// ListGril ist ein geranktes Gril +type ListGril struct { + Gril *grils.Gril + Order int } // Name gibt den Namen des Moduls zurück @@ -46,6 +57,7 @@ func (m *Module) Init(g *grilist.Grilist) { log.Fatal("lists: error with grils module") } m.grils = grilsModule + m.g.Router.GET("/list/:id", m.viewList) } // Interface gibt das Modul zurück @@ -53,13 +65,40 @@ func (m *Module) Interface() interface{} { return m } -// GetUserLists gibt die Listen eines Benutzers zurück. -func (m *Module) GetUserLists(u *charakterin.User) []*List { +func (m *Module) getListGrils(list *List) error { + rows, err := m.g.DB.Query(`SELECT gril_id, "order" FROM grilist.lists_grils WHERE list_id = $1`, list.ID) + if err != nil { + return err + } + + defer rows.Close() + for rows.Next() { + var grilID int + lg := &ListGril{} + + if err := rows.Scan(&grilID, &lg.Order); err != nil { + log.Println("error scanning row in getListGrils:", err) + continue + } + + gril, err := m.grils.FromID(grilID) + if err != nil { + log.Println("error getting listGril:", err) + continue + } + lg.Gril = gril + + list.Grils = append(list.Grils, lg) + } + return nil +} + +func (m *Module) getLists(whereClause string, params ...interface{}) ([]*List, error) { 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) + rows, err := m.g.DB.Query(fmt.Sprintf(`SELECT id, name, description, fork_of, updated_at FROM grilist.lists WHERE %s`, whereClause), params...) if err != nil { - return lists + return nil, err } defer rows.Close() @@ -67,16 +106,43 @@ func (m *Module) GetUserLists(u *charakterin.User) []*List { list := &List{} if err := rows.Scan(&list.ID, &list.Name, &list.Description, &list.ForkOf, &list.UpdatedAt); err != nil { - log.Println("error scanning row", err) + log.Println("error scanning row in getLists:", err) + continue + } + + if err := m.getListGrils(list); err != nil { + log.Println(err) continue } lists = append(lists, list) } + return lists, nil +} + +// GetUserLists gibt die Listen eines Benutzers zurück. +func (m *Module) GetUserLists(u *charakterin.User) []*List { + lists, err := m.getLists(`user_id = $1`, u.ID) + if err != nil { + log.Println(err) + } return lists } +// FromID sucht nach der Liste mit der gegebenen ID und gibt sie, falls sie existiert, zurück. +func (m *Module) FromID(id int) (*List, error) { + lists, err := m.getLists(`id = $1`, id) + if err != nil { + return nil, err + } + + if len(lists) == 0 { + return nil, errors.New("no list found") + } + return lists[0], nil +} + func mkCard(title, description string, actions ...frontend.Action) frontend.Card { card := frontend.Card{ Title: title, @@ -102,7 +168,7 @@ func rowsToCard(rows *sql.Rows) []frontend.Card { continue } - cards = append(cards, mkCard(title, description, frontend.Action{"anguckieren", fmt.Sprintf("/lists/%d", id)})) + cards = append(cards, mkCard(title, description, frontend.Action{"anguckieren", fmt.Sprintf("/list/%d", id)})) } return cards } @@ -140,6 +206,32 @@ func (m *Module) ProvideDashboardData(user *charakterin.User) []grilist.Dashboar return categories } +func (m *Module) viewList(w http.ResponseWriter, r *http.Request, p httprouter.Params) { + loggedIn := false + if user, _ := m.g.Charakterin.GetUserFromRequest(r); user != nil { + loggedIn = true + } + sid := p.ByName("id") + + id, err := strconv.Atoi(sid) + if err != nil { + http.Redirect(w, r, "/", 302) + return + } + + list, err := m.FromID(id) + if err != nil { + http.Redirect(w, r, "/", 302) + return + } + + data := make(map[string]interface{}) + data["loggedIn"] = loggedIn + data["list"] = list + + m.g.Renderer.RenderPage("list", w, data) +} + // New erstellt eine neue Instanz des Modules func New() *Module { return &Module{} diff --git a/views/gril.html b/views/gril.html new file mode 100644 index 0000000..d62fc75 --- /dev/null +++ b/views/gril.html @@ -0,0 +1,21 @@ +{{ define "gril" }} +{{ $gril := .gril }} + + + {{ template "materialize" }} + grilist + + + + +
+ {{ if .loggedIn }} +
+ Ausloggen +
+ {{ end }} +

{{ $gril.RomajiName }}{{ $gril.KanjiName }}

+
+ + +{{ end }} \ No newline at end of file diff --git a/views/list.html b/views/list.html new file mode 100644 index 0000000..63cfd9c --- /dev/null +++ b/views/list.html @@ -0,0 +1,26 @@ +{{ define "list" }} +{{ $list := .list }} + + + {{ template "materialize" }} + grilist + + + + +
+ {{ if .loggedIn }} +
+ Ausloggen +
+ {{ end }} +

{{ $list.Name }}von xyz


+ +
+ + +{{ end }} \ No newline at end of file -- cgit v0.10.1