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"
|
|
|
|
|
|
|
|
"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) {
|
|
|
|
r.PathPrefix("/title-search/").HandlerFunc(handlerTitleSearch)
|
|
|
|
r.PathPrefix("/title-search-json/").HandlerFunc(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-07-12 18:00:53 +00:00
|
|
|
w.WriteHeader(http.StatusOK)
|
2021-07-12 13:25:57 +00:00
|
|
|
_, _ = io.WriteString(
|
|
|
|
w,
|
|
|
|
views.BaseHTML(
|
|
|
|
"Search: "+query,
|
2021-07-12 17:05:50 +00:00
|
|
|
views.TitleSearchHTML(query, shroom.YieldHyphaNamesContainingString),
|
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),
|
|
|
|
)
|
|
|
|
}
|