1
0
mirror of https://github.com/osmarks/mycorrhiza.git synced 2024-10-30 11:46:16 +00:00
mycorrhiza/web/categories.go

51 lines
1.6 KiB
Go
Raw Normal View History

2022-03-20 08:21:59 +00:00
package web
import (
"github.com/bouncepaw/mycorrhiza/hyphae/categories"
2022-03-20 08:21:59 +00:00
"github.com/bouncepaw/mycorrhiza/util"
"github.com/bouncepaw/mycorrhiza/views"
"github.com/gorilla/mux"
"net/http"
)
func initCategories(r *mux.Router) {
r.PathPrefix("/add-to-category").HandlerFunc(handlerAddToCategory).Methods("POST")
r.PathPrefix("/remove-from-category").HandlerFunc(handlerRemoveFromCategory).Methods("POST")
r.PathPrefix("/category/").HandlerFunc(handlerCategory).Methods("GET")
r.PathPrefix("/category").HandlerFunc(handlerListCategory).Methods("GET")
}
func handlerListCategory(w http.ResponseWriter, rq *http.Request) {
2022-03-20 21:24:40 +00:00
views.CategoryList(views.MetaFrom(w, rq))
2022-03-20 08:21:59 +00:00
}
func handlerCategory(w http.ResponseWriter, rq *http.Request) {
util.PrepareRq(rq)
var (
catName = util.HyphaNameFromRq(rq, "category")
)
2022-03-20 21:24:40 +00:00
views.CategoryPage(views.MetaFrom(w, rq), catName)
2022-03-20 08:21:59 +00:00
}
func handlerRemoveFromCategory(w http.ResponseWriter, rq *http.Request) {
util.PrepareRq(rq)
var (
hyphaName = util.CanonicalName(rq.PostFormValue("hypha"))
catName = util.CanonicalName(rq.PostFormValue("cat"))
redirectTo = rq.PostFormValue("redirect-to")
)
categories.RemoveHyphaFromCategory(hyphaName, catName)
http.Redirect(w, rq, redirectTo, http.StatusSeeOther)
2022-03-20 08:21:59 +00:00
}
func handlerAddToCategory(w http.ResponseWriter, rq *http.Request) {
util.PrepareRq(rq)
var (
hyphaName = util.CanonicalName(rq.PostFormValue("hypha"))
catName = util.CanonicalName(rq.PostFormValue("cat"))
redirectTo = rq.PostFormValue("redirect-to")
)
categories.AddHyphaToCategory(hyphaName, catName)
http.Redirect(w, rq, redirectTo, http.StatusSeeOther)
2022-03-20 08:21:59 +00:00
}