2022-04-01 20:52:56 +00:00
|
|
|
package categories
|
2022-03-20 08:21:59 +00:00
|
|
|
|
|
|
|
import (
|
2022-03-26 15:31:13 +00:00
|
|
|
"github.com/bouncepaw/mycorrhiza/user"
|
2022-03-20 08:21:59 +00:00
|
|
|
"github.com/bouncepaw/mycorrhiza/util"
|
2022-03-29 20:59:36 +00:00
|
|
|
"github.com/bouncepaw/mycorrhiza/viewutil"
|
2022-03-20 08:21:59 +00:00
|
|
|
"github.com/gorilla/mux"
|
2022-03-26 15:31:13 +00:00
|
|
|
"io"
|
2022-03-29 16:41:12 +00:00
|
|
|
"log"
|
2022-03-20 08:21:59 +00:00
|
|
|
"net/http"
|
2022-03-29 16:41:12 +00:00
|
|
|
"strings"
|
2022-03-20 08:21:59 +00:00
|
|
|
)
|
|
|
|
|
2022-04-01 22:01:54 +00:00
|
|
|
// InitHandlers initializes HTTP handlers for the given router. Call somewhere in package web.
|
|
|
|
func InitHandlers(r *mux.Router) {
|
2022-03-20 08:21:59 +00:00
|
|
|
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")
|
2022-03-20 18:24:54 +00:00
|
|
|
r.PathPrefix("/category").HandlerFunc(handlerListCategory).Methods("GET")
|
2022-04-01 20:52:56 +00:00
|
|
|
prepareViews()
|
2022-03-20 18:24:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func handlerListCategory(w http.ResponseWriter, rq *http.Request) {
|
2022-03-29 16:41:12 +00:00
|
|
|
log.Println("Viewing list of categories")
|
2022-04-01 20:52:56 +00:00
|
|
|
categoryList(viewutil.MetaFrom(w, rq))
|
2022-03-20 08:21:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func handlerCategory(w http.ResponseWriter, rq *http.Request) {
|
|
|
|
util.PrepareRq(rq)
|
2022-03-29 16:41:12 +00:00
|
|
|
catName := util.CanonicalName(strings.TrimPrefix(strings.TrimPrefix(rq.URL.Path, "/category"), "/"))
|
|
|
|
if catName == "" {
|
|
|
|
handlerListCategory(w, rq)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
log.Println("Viewing category", catName)
|
2022-04-01 20:52:56 +00:00
|
|
|
categoryPage(viewutil.MetaFrom(w, rq), catName)
|
2022-03-20 08:21:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func handlerRemoveFromCategory(w http.ResponseWriter, rq *http.Request) {
|
2022-03-20 09:28:51 +00:00
|
|
|
util.PrepareRq(rq)
|
|
|
|
var (
|
2022-03-20 11:50:18 +00:00
|
|
|
hyphaName = util.CanonicalName(rq.PostFormValue("hypha"))
|
|
|
|
catName = util.CanonicalName(rq.PostFormValue("cat"))
|
2022-03-20 09:28:51 +00:00
|
|
|
redirectTo = rq.PostFormValue("redirect-to")
|
|
|
|
)
|
2022-03-26 15:31:13 +00:00
|
|
|
if !user.FromRequest(rq).CanProceed("remove-from-category") {
|
|
|
|
w.WriteHeader(http.StatusForbidden)
|
|
|
|
_, _ = io.WriteString(w, "403 Forbidden")
|
|
|
|
return
|
|
|
|
}
|
2022-03-29 16:46:29 +00:00
|
|
|
if hyphaName == "" || catName == "" {
|
|
|
|
http.Redirect(w, rq, redirectTo, http.StatusSeeOther)
|
|
|
|
return
|
|
|
|
}
|
2022-04-01 21:45:58 +00:00
|
|
|
log.Println(user.FromRequest(rq).Name, "removed", hyphaName, "from", catName)
|
2022-04-01 21:43:11 +00:00
|
|
|
removeHyphaFromCategory(hyphaName, catName)
|
2022-03-20 09:28:51 +00:00
|
|
|
http.Redirect(w, rq, redirectTo, http.StatusSeeOther)
|
2022-03-20 08:21:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func handlerAddToCategory(w http.ResponseWriter, rq *http.Request) {
|
2022-03-20 09:28:51 +00:00
|
|
|
util.PrepareRq(rq)
|
|
|
|
var (
|
2022-03-20 11:50:18 +00:00
|
|
|
hyphaName = util.CanonicalName(rq.PostFormValue("hypha"))
|
|
|
|
catName = util.CanonicalName(rq.PostFormValue("cat"))
|
2022-03-20 09:28:51 +00:00
|
|
|
redirectTo = rq.PostFormValue("redirect-to")
|
|
|
|
)
|
2022-03-26 15:31:13 +00:00
|
|
|
if !user.FromRequest(rq).CanProceed("add-to-category") {
|
|
|
|
w.WriteHeader(http.StatusForbidden)
|
|
|
|
_, _ = io.WriteString(w, "403 Forbidden")
|
|
|
|
return
|
|
|
|
}
|
2022-03-29 16:46:29 +00:00
|
|
|
if hyphaName == "" || catName == "" {
|
|
|
|
http.Redirect(w, rq, redirectTo, http.StatusSeeOther)
|
|
|
|
return
|
|
|
|
}
|
2022-04-01 21:45:58 +00:00
|
|
|
log.Println(user.FromRequest(rq).Name, "added", hyphaName, "to", catName)
|
2022-04-01 21:43:11 +00:00
|
|
|
addHyphaToCategory(hyphaName, catName)
|
2022-03-20 09:28:51 +00:00
|
|
|
http.Redirect(w, rq, redirectTo, http.StatusSeeOther)
|
2022-03-20 08:21:59 +00:00
|
|
|
}
|