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.

228 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. requestBody, err = ioutil.ReadAll(r.Body)
  99. if err != nil {
  100. log.Printf("Error encountered reading POST body: %s\n", err.Error())
  101. JsonReturn(w, 500, "An error occured")
  102. return
  103. }
  104. postData, err = JsonSerialization.DeserializePost(requestBody, []string{
  105. "id",
  106. "links",
  107. "images",
  108. "videos",
  109. "audios",
  110. }, false)
  111. if err != nil {
  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. urlVars map[string]string
  136. id string
  137. ok bool
  138. err error
  139. )
  140. urlVars = mux.Vars(r)
  141. id, ok = urlVars["postID"]
  142. if !ok {
  143. log.Printf("Error encountered getting id\n")
  144. JsonReturn(w, 500, "An error occured")
  145. return
  146. }
  147. requestBody, err = ioutil.ReadAll(r.Body)
  148. if err != nil {
  149. log.Printf("Error encountered reading POST body: %s\n", err.Error())
  150. JsonReturn(w, 500, "An error occured")
  151. return
  152. }
  153. postData, err = JsonSerialization.DeserializePost(requestBody, []string{}, true)
  154. if err != nil {
  155. log.Printf("Invalid data provided to posts API: %s\n", err.Error())
  156. JsonReturn(w, 405, "Invalid data")
  157. return
  158. }
  159. postData, err = Database.UpdatePost(id, &postData)
  160. if err != nil {
  161. log.Printf("An error occured: %s\n", err.Error())
  162. JsonReturn(w, 500, "An error occured")
  163. return
  164. }
  165. returnJson, err = json.MarshalIndent(postData, "", " ")
  166. if err != nil {
  167. log.Printf("An error occured: %s\n", err.Error())
  168. JsonReturn(w, 500, "An error occured")
  169. return
  170. }
  171. // Return updated json
  172. w.WriteHeader(http.StatusOK)
  173. w.Write(returnJson)
  174. }
  175. func deletePost(w http.ResponseWriter, r *http.Request) {
  176. var (
  177. postData Models.Post
  178. err error
  179. )
  180. postData, err = getPostById(w, r)
  181. if err != nil {
  182. return
  183. }
  184. err = Database.DeletePost(&postData)
  185. if err != nil {
  186. log.Printf("An error occured: %s\n", err.Error())
  187. JsonReturn(w, 500, "An error occured")
  188. return
  189. }
  190. // Return updated json
  191. w.WriteHeader(http.StatusOK)
  192. }