package search import ( "log" "net/http" "fagott.pw/charakterin" "fagott.pw/grilist/grilist" "fagott.pw/grilist/modules/grils" "github.com/julienschmidt/httprouter" ) type Module struct { g *grilist.Grilist grils *grils.GrilsModule } type GrilResult struct { ID int ImagePath string Name string } func New() *Module { return &Module{} } func (m *Module) Name() string { return "Search" } func (m *Module) Init(g *grilist.Grilist) { m.g = g gm, ok := g.Modules["Grils"] if !ok { log.Fatal("search: grils module not found") } m.grils = gm.(*grils.GrilsModule) m.g.Router.GET("/search/gril_instant/*name", m.instantSearchGril) } func (m *Module) Interface() interface{} { return m } func (m *Module) ProvideDashboardData(user *charakterin.User) []grilist.DashboardCategory { return make([]grilist.DashboardCategory, 0) } func (m *Module) instantSearchGril(w http.ResponseWriter, r *http.Request, p httprouter.Params) { name := p.ByName("name")[1:] if len(name) < 2 { http.Error(w, "Bad Request", 400) return } rows, err := m.g.DB.Query(`SELECT id, name FROM grilist.search_grils($1)`, name) if err != nil { log.Println("error in instant gril search:", err) return } defer rows.Close() var results []GrilResult for rows.Next() { result := GrilResult{} if err := rows.Scan(&result.ID, &result.Name); err != nil { log.Println("error scanning in instant gril search", err) continue } result.ImagePath = grils.ImagePath(result.ID, true) results = append(results, result) } data := make(map[string]interface{}) data["results"] = results m.g.Renderer.RenderPage("instant_search_results", w, data) }