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.
211 lines
4.8 KiB
211 lines
4.8 KiB
package controllers
|
|
|
|
import (
|
|
"fmt"
|
|
"net/http"
|
|
"strings"
|
|
"time"
|
|
|
|
"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.IndexList{}
|
|
vm.Headers, err = model.ListHeaders()
|
|
vm.Categories = model.PageCategories
|
|
vm.Setup(user, req.URL.Path, "Index")
|
|
|
|
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.IndexList{}
|
|
|
|
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, path, category.Plural)
|
|
|
|
if err != nil {
|
|
response.Text(w, 500, err.Error())
|
|
return true
|
|
}
|
|
|
|
view.Render(w, "index", 200, vm)
|
|
|
|
return true
|
|
}
|
|
}
|
|
|
|
func listTagged(tagType string) 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.IndexList{}
|
|
|
|
if tagName == "" {
|
|
return false
|
|
}
|
|
|
|
tag, err := model.FindTag("name", tagName, tagType)
|
|
if err != nil {
|
|
response.Text(w, 404, err.Error())
|
|
return true
|
|
}
|
|
|
|
vm.ActiveTag = *tag
|
|
vm.Headers, err = model.ListHeadersByTag("", tag)
|
|
|
|
vm.Categories = model.PageCategories
|
|
vm.Setup(user, tag.Path(), tag.Name)
|
|
|
|
if err != nil {
|
|
response.Text(w, 500, err.Error())
|
|
return true
|
|
}
|
|
|
|
view.Render(w, "index", 200, vm)
|
|
|
|
return true
|
|
}
|
|
}
|
|
|
|
func tagList(path string, w http.ResponseWriter, req *http.Request, user *auth.User) bool {
|
|
//var err error
|
|
if (req.Method != "GET" && req.Method != "POST") || len(req.URL.Path) > len(path) {
|
|
return false
|
|
}
|
|
|
|
tl := viewmodel.IndexTags{}
|
|
tl.Tags, _ = model.ListTags()
|
|
tl.Setup(user, path)
|
|
|
|
view.Render(w, "tags", 200, tl)
|
|
|
|
return true
|
|
}
|
|
|
|
func listAuthor(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
|
|
}
|
|
|
|
authorName := strings.Replace(req.URL.Path[len(path):], "_", " ", -1)
|
|
|
|
vm := viewmodel.IndexList{}
|
|
vm.ActiveAuthor = authorName
|
|
|
|
vm.Headers, err = model.ListHeadersByAuthor(authorName)
|
|
if err != nil {
|
|
response.Text(w, 500, err.Error())
|
|
return true
|
|
}
|
|
|
|
if len(vm.Headers) > 0 {
|
|
authorName = vm.Headers[0].Author
|
|
}
|
|
|
|
vm.Categories = model.PageCategories
|
|
vm.Setup(user, "/author/"+authorName, authorName)
|
|
|
|
view.Render(w, "index", 200, vm)
|
|
|
|
return true
|
|
}
|
|
|
|
func listDate(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
|
|
}
|
|
|
|
date, err := time.Parse("2006-01", req.URL.Path[len(path):])
|
|
if err != nil {
|
|
response.Text(w, 400, "Invalid date")
|
|
return true
|
|
}
|
|
|
|
vm := viewmodel.IndexList{}
|
|
vm.ActiveDate = date.Format("January 2006")
|
|
vm.ActiveDatePath = "/month/" + date.Format("2006-01")
|
|
|
|
vm.Headers, err = model.ListHeadersByMonth(date.Year(), int(date.Month()))
|
|
if err != nil {
|
|
response.Text(w, 500, err.Error())
|
|
return true
|
|
}
|
|
|
|
vm.Categories = model.PageCategories
|
|
vm.Setup(user, "/month/"+date.Format("2006-01"), vm.ActiveDate)
|
|
|
|
view.Render(w, "index", 200, vm)
|
|
|
|
return true
|
|
}
|
|
|
|
func init() {
|
|
ListController.Function("/", listIndex)
|
|
ListController.Function("/tags/", tagList)
|
|
ListController.Function("/author/", listAuthor)
|
|
ListController.Function("/month/", listDate)
|
|
|
|
for _, category := range model.PageCategories {
|
|
ListController.Function(category.Path, listFiltered(category))
|
|
}
|
|
|
|
for _, tagType := range model.TagTypes {
|
|
ListController.Function(fmt.Sprintf("/%s/", strings.ToLower(tagType)), listTagged(tagType))
|
|
}
|
|
}
|