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.

120 lines
2.4 KiB

  1. package Api
  2. import (
  3. "encoding/json"
  4. "log"
  5. "net/http"
  6. "net/url"
  7. "strconv"
  8. "git.tovijaeschke.xyz/tovi/SuddenImpactRecords/Database"
  9. "git.tovijaeschke.xyz/tovi/SuddenImpactRecords/Models"
  10. "git.tovijaeschke.xyz/tovi/SuddenImpactRecords/Util"
  11. )
  12. func getFrontPagePosts(w http.ResponseWriter, r *http.Request) {
  13. var (
  14. posts []Models.Post
  15. returnJson []byte
  16. err error
  17. )
  18. posts, err = Database.GetFrontPagePosts()
  19. if err != nil {
  20. log.Printf("An error occured: %s\n", err.Error())
  21. Util.JsonReturn(w, 500, "An error occured")
  22. return
  23. }
  24. returnJson, err = json.MarshalIndent(posts, "", " ")
  25. if err != nil {
  26. Util.JsonReturn(w, 500, "An error occured")
  27. return
  28. }
  29. // Return updated json
  30. w.WriteHeader(http.StatusOK)
  31. w.Write(returnJson)
  32. }
  33. func getPostsPublic(w http.ResponseWriter, r *http.Request) {
  34. var (
  35. posts []Models.Post
  36. returnJson []byte
  37. values url.Values
  38. page, pageSize int
  39. search string
  40. err error
  41. )
  42. values = r.URL.Query()
  43. page, err = strconv.Atoi(values.Get("page"))
  44. if err != nil {
  45. log.Println("Could not parse page url argument")
  46. Util.JsonReturn(w, 500, "An error occured")
  47. return
  48. }
  49. pageSize, err = strconv.Atoi(values.Get("pageSize"))
  50. if err != nil {
  51. log.Println("Could not parse pageSize url argument")
  52. Util.JsonReturn(w, 500, "An error occured")
  53. return
  54. }
  55. search = values.Get("search")
  56. posts, err = Database.GetPostsPublic(page, pageSize, search)
  57. if err != nil {
  58. log.Printf("An error occured: %s\n", err.Error())
  59. Util.JsonReturn(w, 500, "An error occured")
  60. return
  61. }
  62. if len(posts) == 0 {
  63. Util.JsonReturn(w, 404, "No more data")
  64. return
  65. }
  66. returnJson, err = json.MarshalIndent(posts, "", " ")
  67. if err != nil {
  68. Util.JsonReturn(w, 500, "An error occured")
  69. return
  70. }
  71. // Return updated json
  72. w.WriteHeader(http.StatusOK)
  73. w.Write(returnJson)
  74. }
  75. func getPostPublic(w http.ResponseWriter, r *http.Request) {
  76. var (
  77. postData Models.Post
  78. returnJson []byte
  79. id string
  80. err error
  81. )
  82. id, err = Util.GetPostId(r)
  83. if err != nil {
  84. Util.JsonReturn(w, 500, "An error occured")
  85. return
  86. }
  87. postData, err = Database.GetPostByIdPublic(id)
  88. if err != nil {
  89. Util.JsonReturn(w, 404, "Not found")
  90. return
  91. }
  92. returnJson, err = json.MarshalIndent(postData, "", " ")
  93. if err != nil {
  94. Util.JsonReturn(w, 500, "An error occured")
  95. return
  96. }
  97. // Return updated json
  98. w.WriteHeader(http.StatusOK)
  99. w.Write(returnJson)
  100. }