From c2498d562d3e30e4795056058fc3d8a5cec4ec11 Mon Sep 17 00:00:00 2001 From: rtz12 Date: Tue, 29 Dec 2015 20:41:42 +0100 Subject: Tagansicht hinzugefuegt diff --git a/assets/css/gril.css b/assets/css/gril.css index 63c45ed..b82e2c7 100644 --- a/assets/css/gril.css +++ b/assets/css/gril.css @@ -7,6 +7,15 @@ margin-bottom: 1rem; } +.gril-tag { + transition: background-color .2s, color .2s; +} + +.gril-tag:hover { + background-color: #BA68C8; + color: #FFF; +} + h1 > small { padding-left: 8px; font-size: 50%; diff --git a/main.go b/main.go index 6dec94c..04db8d6 100644 --- a/main.go +++ b/main.go @@ -11,6 +11,7 @@ import ( "fagott.pw/grilist/grilist" "fagott.pw/grilist/modules/grils" "fagott.pw/grilist/modules/lists" + "fagott.pw/grilist/modules/tags" "github.com/julienschmidt/httprouter" _ "github.com/lib/pq" @@ -73,6 +74,7 @@ func main() { // Module laden loadModule(grils.New()) loadModule(lists.New()) + loadModule(tags.New()) log.Fatal(http.ListenAndServe(":8080", nil)) } diff --git a/modules/grils/grils.go b/modules/grils/grils.go index 7ea5259..46d0fb3 100644 --- a/modules/grils/grils.go +++ b/modules/grils/grils.go @@ -156,6 +156,22 @@ func (m *GrilsModule) FromID(id int) (*Gril, error) { return gril, nil } +func (m *GrilsModule) FromIDs(ids []int) ([]*Gril, error) { + idList := "(" + first := true + for _, v := range ids { + if first { + first = false + } else { + idList += "," + } + idList += strconv.Itoa(v) + } + idList += ")" + grils, err := m.getGrils("id IN " + idList) + return grils, err +} + func (m *GrilsModule) viewGril(w http.ResponseWriter, r *http.Request, p httprouter.Params) { user, _ := m.g.Charakterin.GetUserFromRequest(r) sid := p.ByName("id") diff --git a/modules/tags/tags.go b/modules/tags/tags.go new file mode 100644 index 0000000..9412e24 --- /dev/null +++ b/modules/tags/tags.go @@ -0,0 +1,96 @@ +package tags + +import ( + "log" + "net/http" + + "fagott.pw/charakterin" + "fagott.pw/grilist/frontend" + "fagott.pw/grilist/grilist" + "fagott.pw/grilist/modules/grils" + + "github.com/julienschmidt/httprouter" +) + +type Module struct { + g *grilist.Grilist + grils *grils.GrilsModule +} + +func New() *Module { + return &Module{} +} + +func (m *Module) Name() string { + return "Tags" +} + +func (m *Module) Init(g *grilist.Grilist) { + m.g = g + gm, ok := g.Modules["Grils"] + if !ok { + log.Fatal("tags: grils module not found") + } + m.grils = gm.(*grils.GrilsModule) + m.g.Router.GET("/tag/:tag", m.viewTag) +} + +func (m *Module) Interface() interface{} { + return m +} + +func (m *Module) ProvideDashboardData(user *charakterin.User) []grilist.DashboardCategory { + return make([]grilist.DashboardCategory, 0) +} + +func (m *Module) viewTag(w http.ResponseWriter, r *http.Request, p httprouter.Params) { + user, _ := m.g.Charakterin.GetUserFromRequest(r) + tag := p.ByName("tag") + var tagID int + row := m.g.DB.QueryRow("SELECT id FROM grilist.tags WHERE name = $1;", tag) + if err := row.Scan(&tagID); err != nil { + log.Println(err) + http.Redirect(w, r, "/", 302) + return + } + idList := make([]int, 0) + rows, err := m.g.DB.Query( + "SELECT gril_id FROM grilist.grils_tags WHERE tag_id = $1", + tagID) + if err != nil { + log.Println(err) + http.Redirect(w, r, "/", 302) + return + } + defer rows.Close() + for rows.Next() { + var grilID int + rows.Scan(&grilID) + idList = append(idList, grilID) + } + grils, err := m.grils.FromIDs(idList) + if err != nil { + log.Println(err) + http.Redirect(w, r, "/", 302) + return + } + cards := make([]frontend.Card, 0) + for _, v := range grils { + cards = append(cards, frontend.Card{ + Title: v.RomajiName, + Description: v.KanjiName, + Size: "medium", + Actions: []frontend.Action{ + frontend.Action{ + Name: "anguckieren", + Link: "/gril/" + v.Slug(), + }, + }, + }) + } + data := m.g.Renderer.DefaultData() + data["user"] = user + data["tag"] = tag + data["cards"] = cards + m.g.Renderer.RenderPage("tag", w, data) +} diff --git a/views/card.html b/views/card.html new file mode 100644 index 0000000..be28272 --- /dev/null +++ b/views/card.html @@ -0,0 +1,20 @@ +{{ define "card" }} +{{ $card := . }} +
+
+
+ {{ $card.Title | html }} +

{{ $card.Description | html }}

+
+
+ {{ range $action := $card.Actions }} + {{ if $action.Disabled }} + {{ $action.Name | html }} + {{ else }} + {{ $action.Name | html }} + {{ end }} + {{ end }} +
+
+
+{{ end }} diff --git a/views/dashboard.html b/views/dashboard.html index 0551285..56be42b 100644 --- a/views/dashboard.html +++ b/views/dashboard.html @@ -13,23 +13,7 @@

{{ $category.Title }}

{{ range $card := $category.Cards }} -
-
-
- {{ $card.Title | html }} -

{{ $card.Description | html }}

-
-
- {{ range $action := $card.Actions }} - {{ if $action.Disabled }} - {{ $action.Name | html }} - {{ else }} - {{ $action.Name | html }} - {{ end }} - {{ end }} -
-
-
+ {{ template "card" $card }} {{ end }}
{{ end }} @@ -37,4 +21,4 @@ -{{ end }} \ No newline at end of file +{{ end }} diff --git a/views/gril.html b/views/gril.html index 9768778..803cdde 100644 --- a/views/gril.html +++ b/views/gril.html @@ -18,9 +18,13 @@

{{ end }} {{ range $tag := $gril.Tags }} -
- {{ $tag }} -
+ +
+ {{ $tag }} +
+
{{ end }}

diff --git a/views/tag.html b/views/tag.html new file mode 100644 index 0000000..788083f --- /dev/null +++ b/views/tag.html @@ -0,0 +1,22 @@ +{{ define "tag" }} + + + {{ template "materialize" }} + grilist + + + + {{ template "navbar" . }} +
+
+

Grils mit dem Tag "{{ .tag }}":

+
+ {{ range .cards }} + {{ template "card" . }} + {{ end }} +
+
+
+ + +{{ end }} -- cgit v0.10.1