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.

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