2021-07-12 13:25:57 +00:00
|
|
|
package web
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io"
|
|
|
|
"net/http"
|
|
|
|
|
2021-07-15 17:46:35 +00:00
|
|
|
"github.com/gorilla/mux"
|
|
|
|
|
2021-09-29 14:56:17 +00:00
|
|
|
"github.com/bouncepaw/mycorrhiza/l18n"
|
2021-07-15 17:46:35 +00:00
|
|
|
"github.com/bouncepaw/mycorrhiza/shroom"
|
|
|
|
"github.com/bouncepaw/mycorrhiza/user"
|
2021-07-12 13:25:57 +00:00
|
|
|
"github.com/bouncepaw/mycorrhiza/util"
|
2021-07-15 17:46:35 +00:00
|
|
|
"github.com/bouncepaw/mycorrhiza/views"
|
2021-07-12 13:25:57 +00:00
|
|
|
)
|
|
|
|
|
2021-07-15 17:46:35 +00:00
|
|
|
func initSearch(r *mux.Router) {
|
2021-07-17 14:15:39 +00:00
|
|
|
r.HandleFunc("/title-search/", handlerTitleSearch)
|
|
|
|
r.HandleFunc("/title-search-json/", handlerTitleSearchJSON)
|
2021-07-12 13:25:57 +00:00
|
|
|
}
|
|
|
|
|
2021-07-12 17:05:50 +00:00
|
|
|
func handlerTitleSearch(w http.ResponseWriter, rq *http.Request) {
|
2021-07-12 13:25:57 +00:00
|
|
|
util.PrepareRq(rq)
|
2021-07-12 17:01:12 +00:00
|
|
|
_ = rq.ParseForm()
|
2021-07-12 13:25:57 +00:00
|
|
|
var (
|
2021-07-12 17:01:12 +00:00
|
|
|
query = rq.FormValue("q")
|
2021-07-12 13:25:57 +00:00
|
|
|
u = user.FromRequest(rq)
|
2021-09-06 17:46:34 +00:00
|
|
|
lc = l18n.FromRequest(rq)
|
2021-07-12 13:25:57 +00:00
|
|
|
)
|
2021-07-12 18:00:53 +00:00
|
|
|
w.WriteHeader(http.StatusOK)
|
2021-07-12 13:25:57 +00:00
|
|
|
_, _ = 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,
|
2021-07-12 13:25:57 +00:00
|
|
|
u,
|
|
|
|
),
|
|
|
|
)
|
|
|
|
}
|
2021-07-12 17:55:36 +00:00
|
|
|
|
|
|
|
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")
|
2021-07-12 18:00:53 +00:00
|
|
|
w.WriteHeader(http.StatusOK)
|
2021-07-12 17:55:36 +00:00
|
|
|
_, _ = io.WriteString(
|
|
|
|
w,
|
|
|
|
views.TitleSearchJSON(query, shroom.YieldHyphaNamesContainingString),
|
|
|
|
)
|
|
|
|
}
|