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.

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