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

7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
  1. package controllers
  2. import (
  3. "fmt"
  4. "net/http"
  5. "strings"
  6. "git.aiterp.net/gisle/wrouter/response"
  7. "git.aiterp.net/AiteRP/aitestory/model"
  8. "git.aiterp.net/AiteRP/aitestory/view"
  9. "git.aiterp.net/AiteRP/aitestory/viewmodel"
  10. "git.aiterp.net/gisle/wrouter"
  11. "git.aiterp.net/gisle/wrouter/auth"
  12. )
  13. // ListController serves the front page's list, with and without filters
  14. var ListController = wrouter.Router{}
  15. func listIndex(path string, w http.ResponseWriter, req *http.Request, user *auth.User) bool {
  16. var err error
  17. if req.Method != "GET" || len(req.URL.Path) > len(path) {
  18. return false
  19. }
  20. vm := viewmodel.PageList{}
  21. vm.Headers, err = model.ListHeaders()
  22. vm.Categories = model.PageCategories
  23. vm.Setup(user)
  24. if err != nil {
  25. response.Text(w, 500, err.Error())
  26. return true
  27. }
  28. view.Render(w, "index", 200, vm)
  29. return true
  30. }
  31. func listFiltered(category model.PageCategory) wrouter.FunctionHandlerFunc {
  32. return func(path string, w http.ResponseWriter, req *http.Request, user *auth.User) bool {
  33. var err error
  34. if req.Method != "GET" || strings.LastIndex(req.URL.Path, "/") >= len(path) {
  35. return false
  36. }
  37. tagName := strings.Replace(req.URL.Path[len(path):], "_", " ", -1)
  38. vm := viewmodel.PageList{}
  39. if tagName != "" {
  40. tag, err := model.FindTag("name", tagName)
  41. if err != nil {
  42. response.Text(w, 404, err.Error())
  43. return true
  44. }
  45. vm.ActiveTag = *tag
  46. vm.Headers, err = model.ListHeadersByTag(category.Key, tag)
  47. } else {
  48. vm.Headers, err = model.ListHeadersByCategory(category.Key)
  49. }
  50. vm.Categories = model.PageCategories
  51. vm.ActiveCategory = category
  52. vm.Setup(user)
  53. if err != nil {
  54. response.Text(w, 500, err.Error())
  55. return true
  56. }
  57. view.Render(w, "index", 200, vm)
  58. return true
  59. }
  60. }
  61. func init() {
  62. ListController.Function("/", listIndex)
  63. ListController.Function("/", listFiltered(model.PageCategory{Key: ""}))
  64. for _, category := range model.PageCategories {
  65. ListController.Function(fmt.Sprintf("/%s/", strings.ToLower(category.Plural)), listFiltered(category))
  66. }
  67. }