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.

246 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. "time"
  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 getPost(w http.ResponseWriter, r *http.Request) {
  58. var (
  59. postData Models.Post
  60. returnJson []byte
  61. err error
  62. )
  63. postData, err = Util.GetPostById(w, r)
  64. if err != nil {
  65. return
  66. }
  67. returnJson, err = json.MarshalIndent(postData, "", " ")
  68. if err != nil {
  69. Util.JsonReturn(w, 500, "An error occured")
  70. return
  71. }
  72. // Return updated json
  73. w.WriteHeader(http.StatusOK)
  74. w.Write(returnJson)
  75. }
  76. func createPost(w http.ResponseWriter, r *http.Request) {
  77. var (
  78. requestBody []byte
  79. postData Models.Post
  80. returnJson []byte
  81. err error
  82. )
  83. requestBody, err = ioutil.ReadAll(r.Body)
  84. if err != nil {
  85. log.Printf("Error encountered reading POST body: %s\n", err.Error())
  86. Util.JsonReturn(w, 500, "An error occured")
  87. return
  88. }
  89. postData, err = JsonSerialization.DeserializePost(requestBody, []string{
  90. "id",
  91. "links",
  92. "images",
  93. "videos",
  94. "audios",
  95. }, false)
  96. if err != nil {
  97. log.Printf("Invalid data provided to posts API: %s\n", err.Error())
  98. Util.JsonReturn(w, 405, "Invalid data")
  99. return
  100. }
  101. err = Database.CreatePost(&postData)
  102. if err != nil {
  103. Util.JsonReturn(w, 405, "Invalid data")
  104. }
  105. returnJson, err = json.MarshalIndent(postData, "", " ")
  106. if err != nil {
  107. log.Printf("An error occured: %s\n", err.Error())
  108. Util.JsonReturn(w, 500, "An error occured")
  109. return
  110. }
  111. // Return updated json
  112. w.WriteHeader(http.StatusOK)
  113. w.Write(returnJson)
  114. }
  115. func updatePost(w http.ResponseWriter, r *http.Request) {
  116. var (
  117. postData Models.Post
  118. requestBody []byte
  119. returnJson []byte
  120. id string
  121. err error
  122. )
  123. id, err = Util.GetPostId(r)
  124. if err != nil {
  125. log.Printf("Error encountered getting id\n")
  126. Util.JsonReturn(w, 500, "An error occured")
  127. return
  128. }
  129. requestBody, err = ioutil.ReadAll(r.Body)
  130. if err != nil {
  131. log.Printf("Error encountered reading POST body: %s\n", err.Error())
  132. Util.JsonReturn(w, 500, "An error occured")
  133. return
  134. }
  135. postData, err = JsonSerialization.DeserializePost(requestBody, []string{}, true)
  136. if err != nil {
  137. log.Printf("Invalid data provided to posts API: %s\n", err.Error())
  138. Util.JsonReturn(w, 405, "Invalid data")
  139. return
  140. }
  141. postData, err = Database.UpdatePost(id, &postData)
  142. if err != nil {
  143. log.Printf("An error occured: %s\n", err.Error())
  144. Util.JsonReturn(w, 500, "An error occured")
  145. return
  146. }
  147. returnJson, err = json.MarshalIndent(postData, "", " ")
  148. if err != nil {
  149. log.Printf("An error occured: %s\n", err.Error())
  150. Util.JsonReturn(w, 500, "An error occured")
  151. return
  152. }
  153. // Return updated json
  154. w.WriteHeader(http.StatusOK)
  155. w.Write(returnJson)
  156. }
  157. func publishPost(w http.ResponseWriter, r *http.Request) {
  158. var (
  159. now time.Time = time.Now()
  160. postData Models.Post
  161. returnJson []byte
  162. err error
  163. )
  164. postData, err = Util.GetPostById(w, r)
  165. if err != nil {
  166. log.Printf("Error encountered getting id\n")
  167. Util.JsonReturn(w, 500, "An error occured")
  168. return
  169. }
  170. if postData.PublishedAt == nil {
  171. postData.PublishedAt = &now
  172. } else {
  173. postData.PublishedAt = nil
  174. }
  175. postData, err = Database.UpdatePost(postData.ID.String(), &postData)
  176. if err != nil {
  177. log.Printf("An error occured: %s\n", err.Error())
  178. Util.JsonReturn(w, 500, "An error occured")
  179. return
  180. }
  181. returnJson, err = json.MarshalIndent(postData, "", " ")
  182. if err != nil {
  183. log.Printf("An error occured: %s\n", err.Error())
  184. Util.JsonReturn(w, 500, "An error occured")
  185. return
  186. }
  187. // Return updated json
  188. w.WriteHeader(http.StatusOK)
  189. w.Write(returnJson)
  190. }
  191. func deletePost(w http.ResponseWriter, r *http.Request) {
  192. var (
  193. postData Models.Post
  194. err error
  195. )
  196. postData, err = Util.GetPostById(w, r)
  197. if err != nil {
  198. return
  199. }
  200. err = Database.DeletePost(&postData)
  201. if err != nil {
  202. log.Printf("An error occured: %s\n", err.Error())
  203. Util.JsonReturn(w, 500, "An error occured")
  204. return
  205. }
  206. // Return updated json
  207. w.WriteHeader(http.StatusOK)
  208. }