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.

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