The backend for the AiteStory website
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

89 lines
2.0 KiB

package controllers
import (
"fmt"
"net/http"
"strings"
"git.aiterp.net/gisle/wrouter/response"
"git.aiterp.net/AiteRP/aitestory/model"
"git.aiterp.net/AiteRP/aitestory/view"
"git.aiterp.net/AiteRP/aitestory/viewmodel"
"git.aiterp.net/gisle/wrouter"
"git.aiterp.net/gisle/wrouter/auth"
)
// ListController serves the front page's list, with and without filters
var ListController = wrouter.Router{}
func listIndex(path string, w http.ResponseWriter, req *http.Request, user *auth.User) bool {
var err error
if req.Method != "GET" || len(req.URL.Path) > len(path) {
return false
}
vm := viewmodel.PageList{}
vm.Headers, err = model.ListHeaders()
vm.Categories = model.PageCategories
vm.Setup(user)
if err != nil {
response.Text(w, 500, err.Error())
return true
}
view.Render(w, "index", 200, vm)
return true
}
func listFiltered(category model.PageCategory) wrouter.FunctionHandlerFunc {
return func(path string, w http.ResponseWriter, req *http.Request, user *auth.User) bool {
var err error
if req.Method != "GET" || strings.LastIndex(req.URL.Path, "/") >= len(path) {
return false
}
tagName := strings.Replace(req.URL.Path[len(path):], "_", " ", -1)
vm := viewmodel.PageList{}
if tagName != "" {
tag, err := model.FindTag("name", tagName)
if err != nil {
response.Text(w, 404, err.Error())
return true
}
vm.ActiveTag = *tag
vm.Headers, err = model.ListHeadersByTag(category.Key, tag)
} else {
vm.Headers, err = model.ListHeadersByCategory(category.Key)
}
vm.Categories = model.PageCategories
vm.ActiveCategory = category
vm.Setup(user)
if err != nil {
response.Text(w, 500, err.Error())
return true
}
view.Render(w, "index", 200, vm)
return true
}
}
func init() {
ListController.Function("/", listIndex)
ListController.Function("/", listFiltered(model.PageCategory{Key: ""}))
for _, category := range model.PageCategories {
ListController.Function(fmt.Sprintf("/%s/", strings.ToLower(category.Plural)), listFiltered(category))
}
}