package web import ( "github.com/bouncepaw/mycorrhiza/hyphae/categories" "github.com/bouncepaw/mycorrhiza/user" "github.com/bouncepaw/mycorrhiza/util" "github.com/bouncepaw/mycorrhiza/views" "github.com/gorilla/mux" "io" "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) { views.CategoryList(views.MetaFrom(w, rq)) } func handlerCategory(w http.ResponseWriter, rq *http.Request) { util.PrepareRq(rq) var ( catName = util.HyphaNameFromRq(rq, "category") ) views.CategoryPage(views.MetaFrom(w, rq), catName) } 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") ) if !user.FromRequest(rq).CanProceed("remove-from-category") { w.WriteHeader(http.StatusForbidden) _, _ = io.WriteString(w, "403 Forbidden") return } categories.RemoveHyphaFromCategory(hyphaName, catName) http.Redirect(w, rq, redirectTo, http.StatusSeeOther) } 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") ) if !user.FromRequest(rq).CanProceed("add-to-category") { w.WriteHeader(http.StatusForbidden) _, _ = io.WriteString(w, "403 Forbidden") return } categories.AddHyphaToCategory(hyphaName, catName) http.Redirect(w, rq, redirectTo, http.StatusSeeOther) }