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.

131 lines
2.9 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 listTagged(tagType string) wrouter.FunctionHandlerFunc {
  62. return func(path string, w http.ResponseWriter, req *http.Request, user *auth.User) bool {
  63. var err error
  64. if req.Method != "GET" || strings.LastIndex(req.URL.Path, "/") >= len(path) {
  65. return false
  66. }
  67. tagName := strings.Replace(req.URL.Path[len(path):], "_", " ", -1)
  68. vm := viewmodel.PageList{}
  69. if tagName == "" {
  70. return false
  71. }
  72. tag, err := model.FindTag("name", tagName, tagType)
  73. if err != nil {
  74. response.Text(w, 404, err.Error())
  75. return true
  76. }
  77. vm.ActiveTag = *tag
  78. vm.Headers, err = model.ListHeadersByTag("", tag)
  79. vm.Categories = model.PageCategories
  80. vm.Setup(user)
  81. if err != nil {
  82. response.Text(w, 500, err.Error())
  83. return true
  84. }
  85. view.Render(w, "index", 200, vm)
  86. return true
  87. }
  88. }
  89. func init() {
  90. ListController.Function("/", listIndex)
  91. for _, category := range model.PageCategories {
  92. ListController.Function(fmt.Sprintf("/%s/", strings.ToLower(category.Plural)), listFiltered(category))
  93. }
  94. for _, tagType := range model.TagTypes {
  95. ListController.Function(fmt.Sprintf("/%s/", strings.ToLower(tagType)), listTagged(tagType))
  96. }
  97. }