diff options
Diffstat (limited to 'modules')
-rw-r--r-- | modules/grils/gril.go | 15 | ||||
-rw-r--r-- | modules/grils/grils.go | 29 | ||||
-rw-r--r-- | modules/tags/tags.go | 96 |
3 files changed, 134 insertions, 6 deletions
diff --git a/modules/grils/gril.go b/modules/grils/gril.go index fe316cd..89dd7c4 100644 --- a/modules/grils/gril.go +++ b/modules/grils/gril.go | |||
@@ -3,9 +3,12 @@ package grils | |||
3 | import ( | 3 | import ( |
4 | "database/sql" | 4 | "database/sql" |
5 | "fmt" | 5 | "fmt" |
6 | "github.com/lib/pq" | ||
7 | "os" | 6 | "os" |
7 | "strconv" | ||
8 | "strings" | ||
8 | "time" | 9 | "time" |
10 | |||
11 | "github.com/lib/pq" | ||
9 | ) | 12 | ) |
10 | 13 | ||
11 | type DataSource int | 14 | type DataSource int |
@@ -28,6 +31,16 @@ type Gril struct { | |||
28 | Lists []int | 31 | Lists []int |
29 | } | 32 | } |
30 | 33 | ||
34 | func (g *Gril) Slug() string { | ||
35 | if g.RomajiName == "" { | ||
36 | return strconv.Itoa(g.ID) | ||
37 | } | ||
38 | return fmt.Sprintf( | ||
39 | "%d/%s", | ||
40 | g.ID, | ||
41 | strings.Replace(g.RomajiName, " ", "", -1)) | ||
42 | } | ||
43 | |||
31 | func (g *Gril) ImagePath(prioritizeThumbnail bool) string { | 44 | func (g *Gril) ImagePath(prioritizeThumbnail bool) string { |
32 | var big string | 45 | var big string |
33 | exts := []string{"png", "jpg", "gif", "jpeg"} | 46 | exts := []string{"png", "jpg", "gif", "jpeg"} |
diff --git a/modules/grils/grils.go b/modules/grils/grils.go index a1c84c7..0df93a0 100644 --- a/modules/grils/grils.go +++ b/modules/grils/grils.go | |||
@@ -1,16 +1,18 @@ | |||
1 | package grils | 1 | package grils |
2 | 2 | ||
3 | import ( | 3 | import ( |
4 | "fagott.pw/charakterin" | ||
5 | "fagott.pw/grilist/frontend" | ||
6 | "fagott.pw/grilist/grilist" | ||
7 | "fmt" | 4 | "fmt" |
8 | "github.com/julienschmidt/httprouter" | ||
9 | "log" | 5 | "log" |
10 | "net/http" | 6 | "net/http" |
11 | "regexp" | 7 | "regexp" |
12 | "strconv" | 8 | "strconv" |
13 | "strings" | 9 | "strings" |
10 | |||
11 | "fagott.pw/charakterin" | ||
12 | "fagott.pw/grilist/frontend" | ||
13 | "fagott.pw/grilist/grilist" | ||
14 | |||
15 | "github.com/julienschmidt/httprouter" | ||
14 | ) | 16 | ) |
15 | 17 | ||
16 | var ( | 18 | var ( |
@@ -39,6 +41,7 @@ func (m *GrilsModule) Init(g *grilist.Grilist) { | |||
39 | findIdx() | 41 | findIdx() |
40 | m.g = g | 42 | m.g = g |
41 | m.g.Router.GET("/gril/:id", m.viewGril) | 43 | m.g.Router.GET("/gril/:id", m.viewGril) |
44 | m.g.Router.GET("/gril/:id/*rest", m.viewGril) | ||
42 | } | 45 | } |
43 | 46 | ||
44 | func (m *GrilsModule) getGrils(whereClause string, params ...interface{}) ([]*Gril, error) { | 47 | func (m *GrilsModule) getGrils(whereClause string, params ...interface{}) ([]*Gril, error) { |
@@ -124,7 +127,7 @@ func (m *GrilsModule) ProvideDashboardData(user *charakterin.User) []grilist.Das | |||
124 | Actions: []frontend.Action{ | 127 | Actions: []frontend.Action{ |
125 | frontend.Action{ | 128 | frontend.Action{ |
126 | Name: "anguckieren", | 129 | Name: "anguckieren", |
127 | Link: fmt.Sprintf("/gril/%d", gril.ID), | 130 | Link: "/gril/" + gril.Slug(), |
128 | }, | 131 | }, |
129 | }, | 132 | }, |
130 | }) | 133 | }) |
@@ -149,6 +152,22 @@ func (m *GrilsModule) FromID(id int) (*Gril, error) { | |||
149 | return gril, nil | 152 | return gril, nil |
150 | } | 153 | } |
151 | 154 | ||
155 | func (m *GrilsModule) FromIDs(ids []int) ([]*Gril, error) { | ||
156 | idList := "(" | ||
157 | first := true | ||
158 | for _, v := range ids { | ||
159 | if first { | ||
160 | first = false | ||
161 | } else { | ||
162 | idList += "," | ||
163 | } | ||
164 | idList += strconv.Itoa(v) | ||
165 | } | ||
166 | idList += ")" | ||
167 | grils, err := m.getGrils("id IN " + idList) | ||
168 | return grils, err | ||
169 | } | ||
170 | |||
152 | func (m *GrilsModule) viewGril(w http.ResponseWriter, r *http.Request, p httprouter.Params) { | 171 | func (m *GrilsModule) viewGril(w http.ResponseWriter, r *http.Request, p httprouter.Params) { |
153 | user, _ := m.g.Charakterin.GetUserFromRequest(r) | 172 | user, _ := m.g.Charakterin.GetUserFromRequest(r) |
154 | sid := p.ByName("id") | 173 | 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 | } | ||