1
0
mirror of https://github.com/osmarks/mycorrhiza.git synced 2024-12-12 13:30:26 +00:00
mycorrhiza/web/search.go

51 lines
1.1 KiB
Go
Raw Normal View History

package web
import (
"io"
"net/http"
"github.com/gorilla/mux"
2021-09-29 14:56:17 +00:00
"github.com/bouncepaw/mycorrhiza/l18n"
"github.com/bouncepaw/mycorrhiza/shroom"
"github.com/bouncepaw/mycorrhiza/user"
"github.com/bouncepaw/mycorrhiza/util"
"github.com/bouncepaw/mycorrhiza/views"
)
func initSearch(r *mux.Router) {
r.HandleFunc("/title-search/", handlerTitleSearch)
r.HandleFunc("/title-search-json/", handlerTitleSearchJSON)
}
func handlerTitleSearch(w http.ResponseWriter, rq *http.Request) {
util.PrepareRq(rq)
_ = rq.ParseForm()
var (
query = rq.FormValue("q")
u = user.FromRequest(rq)
2021-09-06 17:46:34 +00:00
lc = l18n.FromRequest(rq)
)
w.WriteHeader(http.StatusOK)
_, _ = io.WriteString(
w,
views.BaseHTML(
2021-09-06 17:46:34 +00:00
lc.Get("ui.title_search_title", &l18n.Replacements{"query": query}),
views.TitleSearchHTML(query, shroom.YieldHyphaNamesContainingString, lc),
lc,
u,
),
)
}
func handlerTitleSearchJSON(w http.ResponseWriter, rq *http.Request) {
util.PrepareRq(rq)
_ = rq.ParseForm()
2021-07-15 18:14:05 +00:00
query := rq.FormValue("q")
w.WriteHeader(http.StatusOK)
_, _ = io.WriteString(
w,
views.TitleSearchJSON(query, shroom.YieldHyphaNamesContainingString),
)
}