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.

241 lines
5.1 KiB

  1. package Api
  2. import (
  3. "encoding/json"
  4. "io/ioutil"
  5. "log"
  6. "net/http"
  7. "net/url"
  8. "strconv"
  9. "git.tovijaeschke.xyz/tovi/SuddenImpactRecords/Api/Auth"
  10. "git.tovijaeschke.xyz/tovi/SuddenImpactRecords/Api/JsonSerialization"
  11. "git.tovijaeschke.xyz/tovi/SuddenImpactRecords/Database"
  12. "git.tovijaeschke.xyz/tovi/SuddenImpactRecords/Models"
  13. "git.tovijaeschke.xyz/tovi/SuddenImpactRecords/Util"
  14. )
  15. func getPosts(w http.ResponseWriter, r *http.Request) {
  16. var (
  17. posts []Models.Post
  18. returnJson []byte
  19. values url.Values
  20. page, pageSize int
  21. err error
  22. )
  23. values = r.URL.Query()
  24. page, err = strconv.Atoi(values.Get("page"))
  25. if err != nil {
  26. log.Println("Could not parse page url argument")
  27. Util.JsonReturn(w, 500, "An error occured")
  28. return
  29. }
  30. page, err = strconv.Atoi(values.Get("pageSize"))
  31. if err != nil {
  32. log.Println("Could not parse pageSize url argument")
  33. Util.JsonReturn(w, 500, "An error occured")
  34. return
  35. }
  36. posts, err = Database.GetPosts(page, pageSize)
  37. if err != nil {
  38. log.Printf("An error occured: %s\n", err.Error())
  39. Util.JsonReturn(w, 500, "An error occured")
  40. return
  41. }
  42. returnJson, err = json.MarshalIndent(posts, "", " ")
  43. if err != nil {
  44. Util.JsonReturn(w, 500, "An error occured")
  45. return
  46. }
  47. // Return updated json
  48. w.WriteHeader(http.StatusOK)
  49. w.Write(returnJson)
  50. }
  51. func getFrontPagePosts(w http.ResponseWriter, r *http.Request) {
  52. var (
  53. posts []Models.Post
  54. returnJson []byte
  55. err error
  56. )
  57. posts, err = Database.GetFrontPagePosts()
  58. if err != nil {
  59. log.Printf("An error occured: %s\n", err.Error())
  60. Util.JsonReturn(w, 500, "An error occured")
  61. return
  62. }
  63. returnJson, err = json.MarshalIndent(posts, "", " ")
  64. if err != nil {
  65. Util.JsonReturn(w, 500, "An error occured")
  66. return
  67. }
  68. // Return updated json
  69. w.WriteHeader(http.StatusOK)
  70. w.Write(returnJson)
  71. }
  72. func getPost(w http.ResponseWriter, r *http.Request) {
  73. var (
  74. postData Models.Post
  75. returnJson []byte
  76. err error
  77. )
  78. postData, err = Util.GetPostById(w, r)
  79. if err != nil {
  80. return
  81. }
  82. returnJson, err = json.MarshalIndent(postData, "", " ")
  83. if err != nil {
  84. Util.JsonReturn(w, 500, "An error occured")
  85. return
  86. }
  87. // Return updated json
  88. w.WriteHeader(http.StatusOK)
  89. w.Write(returnJson)
  90. }
  91. func createPost(w http.ResponseWriter, r *http.Request) {
  92. var (
  93. requestBody []byte
  94. postData Models.Post
  95. returnJson []byte
  96. err error
  97. )
  98. _, err = Auth.CheckCookie(r)
  99. if err != nil {
  100. w.WriteHeader(http.StatusUnauthorized)
  101. return
  102. }
  103. requestBody, err = ioutil.ReadAll(r.Body)
  104. if err != nil {
  105. log.Printf("Error encountered reading POST body: %s\n", err.Error())
  106. Util.JsonReturn(w, 500, "An error occured")
  107. return
  108. }
  109. postData, err = JsonSerialization.DeserializePost(requestBody, []string{
  110. "id",
  111. "links",
  112. "images",
  113. "videos",
  114. "audios",
  115. }, false)
  116. if err != nil {
  117. log.Printf("Invalid data provided to posts API: %s\n", err.Error())
  118. Util.JsonReturn(w, 405, "Invalid data")
  119. return
  120. }
  121. err = Database.CreatePost(&postData)
  122. if err != nil {
  123. Util.JsonReturn(w, 405, "Invalid data")
  124. }
  125. returnJson, err = json.MarshalIndent(postData, "", " ")
  126. if err != nil {
  127. log.Printf("An error occured: %s\n", err.Error())
  128. Util.JsonReturn(w, 500, "An error occured")
  129. return
  130. }
  131. // Return updated json
  132. w.WriteHeader(http.StatusOK)
  133. w.Write(returnJson)
  134. }
  135. func updatePost(w http.ResponseWriter, r *http.Request) {
  136. var (
  137. postData Models.Post
  138. requestBody []byte
  139. returnJson []byte
  140. id string
  141. err error
  142. )
  143. _, err = Auth.CheckCookie(r)
  144. if err != nil {
  145. w.WriteHeader(http.StatusUnauthorized)
  146. return
  147. }
  148. id, err = Util.GetPostId(r)
  149. if err != nil {
  150. log.Printf("Error encountered getting id\n")
  151. Util.JsonReturn(w, 500, "An error occured")
  152. return
  153. }
  154. requestBody, err = ioutil.ReadAll(r.Body)
  155. if err != nil {
  156. log.Printf("Error encountered reading POST body: %s\n", err.Error())
  157. Util.JsonReturn(w, 500, "An error occured")
  158. return
  159. }
  160. postData, err = JsonSerialization.DeserializePost(requestBody, []string{}, true)
  161. if err != nil {
  162. log.Printf("Invalid data provided to posts API: %s\n", err.Error())
  163. Util.JsonReturn(w, 405, "Invalid data")
  164. return
  165. }
  166. postData, err = Database.UpdatePost(id, &postData)
  167. if err != nil {
  168. log.Printf("An error occured: %s\n", err.Error())
  169. Util.JsonReturn(w, 500, "An error occured")
  170. return
  171. }
  172. returnJson, err = json.MarshalIndent(postData, "", " ")
  173. if err != nil {
  174. log.Printf("An error occured: %s\n", err.Error())
  175. Util.JsonReturn(w, 500, "An error occured")
  176. return
  177. }
  178. // Return updated json
  179. w.WriteHeader(http.StatusOK)
  180. w.Write(returnJson)
  181. }
  182. func deletePost(w http.ResponseWriter, r *http.Request) {
  183. var (
  184. postData Models.Post
  185. err error
  186. )
  187. _, err = Auth.CheckCookie(r)
  188. if err != nil {
  189. w.WriteHeader(http.StatusUnauthorized)
  190. return
  191. }
  192. postData, err = Util.GetPostById(w, r)
  193. if err != nil {
  194. return
  195. }
  196. err = Database.DeletePost(&postData)
  197. if err != nil {
  198. log.Printf("An error occured: %s\n", err.Error())
  199. Util.JsonReturn(w, 500, "An error occured")
  200. return
  201. }
  202. // Return updated json
  203. w.WriteHeader(http.StatusOK)
  204. }