diff options
author | rtz12 <koenig@fagott.pw> | 2015-12-29 19:41:42 (UTC) |
---|---|---|
committer | rtz12 <koenig@fagott.pw> | 2015-12-29 19:41:42 (UTC) |
commit | c2498d562d3e30e4795056058fc3d8a5cec4ec11 (patch) | |
tree | f560375a0a6217edf7c9ac6e8753a09eb775b831 /modules | |
parent | 43e206d8627ccdb94ad729d9d48d66484faba117 (diff) |
Tagansicht hinzugefuegt
Diffstat (limited to 'modules')
-rw-r--r-- | modules/grils/grils.go | 16 | ||||
-rw-r--r-- | modules/tags/tags.go | 96 |
2 files changed, 112 insertions, 0 deletions
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) { | |||
156 | return gril, nil | 156 | return gril, nil |
157 | } | 157 | } |
158 | 158 | ||
159 | func (m *GrilsModule) FromIDs(ids []int) ([]*Gril, error) { | ||
160 | idList := "(" | ||
161 | first := true | ||
162 | for _, v := range ids { | ||
163 | if first { | ||
164 | first = false | ||
165 | } else { | ||
166 | idList += "," | ||
167 | } | ||
168 | idList += strconv.Itoa(v) | ||
169 | } | ||
170 | idList += ")" | ||
171 | grils, err := m.getGrils("id IN " + idList) | ||
172 | return grils, err | ||
173 | } | ||
174 | |||
159 | func (m *GrilsModule) viewGril(w http.ResponseWriter, r *http.Request, p httprouter.Params) { | 175 | func (m *GrilsModule) viewGril(w http.ResponseWriter, r *http.Request, p httprouter.Params) { |
160 | user, _ := m.g.Charakterin.GetUserFromRequest(r) | 176 | user, _ := m.g.Charakterin.GetUserFromRequest(r) |
161 | sid := p.ByName("id") | 177 | 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 @@ | |||
1 | package tags | ||
2 | |||
3 | import ( | ||
4 | "log" | ||
5 | "net/http" | ||
6 | |||
7 | "fagott.pw/charakterin" | ||
8 | "fagott.pw/grilist/frontend" | ||
9 | "fagott.pw/grilist/grilist" | ||
10 | "fagott.pw/grilist/modules/grils" | ||
11 | |||
12 | "github.com/julienschmidt/httprouter" | ||
13 | ) | ||
14 | |||
15 | type Module struct { | ||
16 | g *grilist.Grilist | ||
17 | grils *grils.GrilsModule | ||
18 | } | ||
19 | |||
20 | func New() *Module { | ||
21 | return &Module{} | ||
22 | } | ||
23 | |||
24 | func (m *Module) Name() string { | ||
25 | return "Tags" | ||
26 | } | ||
27 | |||
28 | func (m *Module) Init(g *grilist.Grilist) { | ||
29 | m.g = g | ||
30 | gm, ok := g.Modules["Grils"] | ||
31 | if !ok { | ||
32 | log.Fatal("tags: grils module not found") | ||
33 | } | ||
34 | m.grils = gm.(*grils.GrilsModule) | ||
35 | m.g.Router.GET("/tag/:tag", m.viewTag) | ||
36 | } | ||
37 | |||
38 | func (m *Module) Interface() interface{} { | ||
39 | return m | ||
40 | } | ||
41 | |||
42 | func (m *Module) ProvideDashboardData(user *charakterin.User) []grilist.DashboardCategory { | ||
43 | return make([]grilist.DashboardCategory, 0) | ||
44 | } | ||
45 | |||
46 | func (m *Module) viewTag(w http.ResponseWriter, r *http.Request, p httprouter.Params) { | ||
47 | user, _ := m.g.Charakterin.GetUserFromRequest(r) | ||
48 | tag := p.ByName("tag") | ||
49 | var tagID int | ||
50 | row := m.g.DB.QueryRow("SELECT id FROM grilist.tags WHERE name = $1;", tag) | ||
51 | if err := row.Scan(&tagID); err != nil { | ||
52 | log.Println(err) | ||
53 | http.Redirect(w, r, "/", 302) | ||
54 | return | ||
55 | } | ||
56 | idList := make([]int, 0) | ||
57 | rows, err := m.g.DB.Query( | ||
58 | "SELECT gril_id FROM grilist.grils_tags WHERE tag_id = $1", | ||
59 | tagID) | ||
60 | if err != nil { | ||
61 | log.Println(err) | ||
62 | http.Redirect(w, r, "/", 302) | ||
63 | return | ||
64 | } | ||
65 | defer rows.Close() | ||
66 | for rows.Next() { | ||
67 | var grilID int | ||
68 | rows.Scan(&grilID) | ||
69 | idList = append(idList, grilID) | ||
70 | } | ||
71 | grils, err := m.grils.FromIDs(idList) | ||
72 | if err != nil { | ||
73 | log.Println(err) | ||
74 | http.Redirect(w, r, "/", 302) | ||
75 | return | ||
76 | } | ||
77 | cards := make([]frontend.Card, 0) | ||
78 | for _, v := range grils { | ||
79 | cards = append(cards, frontend.Card{ | ||
80 | Title: v.RomajiName, | ||
81 | Description: v.KanjiName, | ||
82 | Size: "medium", | ||
83 | Actions: []frontend.Action{ | ||
84 | frontend.Action{ | ||
85 | Name: "anguckieren", | ||
86 | Link: "/gril/" + v.Slug(), | ||
87 | }, | ||
88 | }, | ||
89 | }) | ||
90 | } | ||
91 | data := m.g.Renderer.DefaultData() | ||
92 | data["user"] = user | ||
93 | data["tag"] = tag | ||
94 | data["cards"] = cards | ||
95 | m.g.Renderer.RenderPage("tag", w, data) | ||
96 | } | ||