aboutsummaryrefslogtreecommitdiff
path: root/modules/search
diff options
context:
space:
mode:
Diffstat (limited to 'modules/search')
-rw-r--r--modules/search/search.go81
1 files changed, 81 insertions, 0 deletions
diff --git a/modules/search/search.go b/modules/search/search.go
new file mode 100644
index 0000000..da6d126
--- /dev/null
+++ b/modules/search/search.go
@@ -0,0 +1,81 @@
1package search
2
3import (
4 "log"
5 "net/http"
6
7 "fagott.pw/charakterin"
8 "fagott.pw/grilist/grilist"
9 "fagott.pw/grilist/modules/grils"
10
11 "github.com/julienschmidt/httprouter"
12)
13
14type Module struct {
15 g *grilist.Grilist
16 grils *grils.GrilsModule
17}
18
19func New() *Module {
20 return &Module{}
21}
22
23func (m *Module) Name() string {
24 return "Search"
25}
26
27func (m *Module) Init(g *grilist.Grilist) {
28 m.g = g
29 gm, ok := g.Modules["Grils"]
30 if !ok {
31 log.Fatal("search: grils module not found")
32 }
33 m.grils = gm.(*grils.GrilsModule)
34
35 m.g.Router.GET("/search/gril_instant/*name", m.instantSearchGril)
36}
37
38func (m *Module) Interface() interface{} {
39 return m
40}
41
42func (m *Module) ProvideDashboardData(user *charakterin.User) []grilist.DashboardCategory {
43 return make([]grilist.DashboardCategory, 0)
44}
45
46func (m *Module) instantSearchGril(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
47 name := p.ByName("name")[1:]
48 if len(name) < 2 {
49 http.Error(w, "Bad Request", 400)
50 return
51 }
52
53 rows, err := m.g.DB.Query(`SELECT id FROM grilist.search_grils($1)`, name)
54 if err != nil {
55 log.Println("error in instant gril search:", err)
56 return
57 }
58
59 defer rows.Close()
60
61 var ids []int
62 for rows.Next() {
63 var id int
64 if err := rows.Scan(&id); err != nil {
65 log.Println("error scanning in instant gril search", err)
66 continue
67 }
68
69 ids = append(ids, id)
70 }
71
72 grils, err := m.grils.FromIDs(ids)
73 if err != nil {
74 log.Println("error acquiring instant grils:", err)
75 return
76 }
77
78 data := make(map[string]interface{})
79 data["results"] = grils
80 m.g.Renderer.RenderPage("instant_search_results", w, data)
81}