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.

283 lines
5.9 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/Auth"
  11. "git.tovijaeschke.xyz/tovi/SuddenImpactRecords/Api/JsonSerialization"
  12. "git.tovijaeschke.xyz/tovi/SuddenImpactRecords/Database"
  13. "git.tovijaeschke.xyz/tovi/SuddenImpactRecords/Models"
  14. "git.tovijaeschke.xyz/tovi/SuddenImpactRecords/Util"
  15. )
  16. func getPosts(w http.ResponseWriter, r *http.Request) {
  17. var (
  18. posts []Models.Post
  19. returnJson []byte
  20. values url.Values
  21. page, pageSize int
  22. search string
  23. err error
  24. )
  25. _, err = Auth.CheckCookie(r)
  26. if err != nil {
  27. w.WriteHeader(http.StatusUnauthorized)
  28. return
  29. }
  30. values = r.URL.Query()
  31. page, err = strconv.Atoi(values.Get("page"))
  32. if err != nil {
  33. log.Println("Could not parse page url argument")
  34. Util.JsonReturn(w, 500, "An error occured")
  35. return
  36. }
  37. pageSize, err = strconv.Atoi(values.Get("pageSize"))
  38. if err != nil {
  39. log.Println("Could not parse pageSize url argument")
  40. Util.JsonReturn(w, 500, "An error occured")
  41. return
  42. }
  43. search = values.Get("search")
  44. posts, err = Database.GetPosts(page, pageSize, search)
  45. if err != nil {
  46. log.Printf("An error occured: %s\n", err.Error())
  47. Util.JsonReturn(w, 500, "An error occured")
  48. return
  49. }
  50. if len(posts) == 0 {
  51. Util.JsonReturn(w, 404, "No more data")
  52. return
  53. }
  54. returnJson, err = json.MarshalIndent(posts, "", " ")
  55. if err != nil {
  56. Util.JsonReturn(w, 500, "An error occured")
  57. return
  58. }
  59. // Return updated json
  60. w.WriteHeader(http.StatusOK)
  61. w.Write(returnJson)
  62. }
  63. func getPost(w http.ResponseWriter, r *http.Request) {
  64. var (
  65. postData Models.Post
  66. returnJson []byte
  67. err error
  68. )
  69. _, err = Auth.CheckCookie(r)
  70. if err != nil {
  71. w.WriteHeader(http.StatusUnauthorized)
  72. return
  73. }
  74. postData, err = Util.GetPostById(w, r)
  75. if err != nil {
  76. return
  77. }
  78. returnJson, err = json.MarshalIndent(postData, "", " ")
  79. if err != nil {
  80. Util.JsonReturn(w, 500, "An error occured")
  81. return
  82. }
  83. // Return updated json
  84. w.WriteHeader(http.StatusOK)
  85. w.Write(returnJson)
  86. }
  87. func createPost(w http.ResponseWriter, r *http.Request) {
  88. var (
  89. requestBody []byte
  90. postData Models.Post
  91. returnJson []byte
  92. err error
  93. )
  94. _, err = Auth.CheckCookie(r)
  95. if err != nil {
  96. w.WriteHeader(http.StatusUnauthorized)
  97. return
  98. }
  99. requestBody, err = ioutil.ReadAll(r.Body)
  100. if err != nil {
  101. log.Printf("Error encountered reading POST body: %s\n", err.Error())
  102. Util.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. Util.JsonReturn(w, 405, "Invalid data")
  115. return
  116. }
  117. err = Database.CreatePost(&postData)
  118. if err != nil {
  119. Util.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. Util.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. id string
  137. err error
  138. )
  139. _, err = Auth.CheckCookie(r)
  140. if err != nil {
  141. w.WriteHeader(http.StatusUnauthorized)
  142. return
  143. }
  144. id, err = Util.GetPostId(r)
  145. if err != nil {
  146. log.Printf("Error encountered getting id\n")
  147. Util.JsonReturn(w, 500, "An error occured")
  148. return
  149. }
  150. requestBody, err = ioutil.ReadAll(r.Body)
  151. if err != nil {
  152. log.Printf("Error encountered reading POST body: %s\n", err.Error())
  153. Util.JsonReturn(w, 500, "An error occured")
  154. return
  155. }
  156. postData, err = JsonSerialization.DeserializePost(requestBody, []string{}, true)
  157. if err != nil {
  158. log.Printf("Invalid data provided to posts API: %s\n", err.Error())
  159. Util.JsonReturn(w, 405, "Invalid data")
  160. return
  161. }
  162. postData, err = Database.UpdatePost(id, &postData)
  163. if err != nil {
  164. log.Printf("An error occured: %s\n", err.Error())
  165. Util.JsonReturn(w, 500, "An error occured")
  166. return
  167. }
  168. returnJson, err = json.MarshalIndent(postData, "", " ")
  169. if err != nil {
  170. log.Printf("An error occured: %s\n", err.Error())
  171. Util.JsonReturn(w, 500, "An error occured")
  172. return
  173. }
  174. // Return updated json
  175. w.WriteHeader(http.StatusOK)
  176. w.Write(returnJson)
  177. }
  178. func publishPost(w http.ResponseWriter, r *http.Request) {
  179. var (
  180. now time.Time = time.Now()
  181. postData Models.Post
  182. returnJson []byte
  183. err error
  184. )
  185. _, err = Auth.CheckCookie(r)
  186. if err != nil {
  187. w.WriteHeader(http.StatusUnauthorized)
  188. return
  189. }
  190. postData, err = Util.GetPostById(w, r)
  191. if err != nil {
  192. log.Printf("Error encountered getting id\n")
  193. Util.JsonReturn(w, 500, "An error occured")
  194. return
  195. }
  196. if postData.PublishedAt == nil {
  197. postData.PublishedAt = &now
  198. } else {
  199. postData.PublishedAt = nil
  200. }
  201. postData, err = Database.UpdatePost(postData.ID.String(), &postData)
  202. if err != nil {
  203. log.Printf("An error occured: %s\n", err.Error())
  204. Util.JsonReturn(w, 500, "An error occured")
  205. return
  206. }
  207. returnJson, err = json.MarshalIndent(postData, "", " ")
  208. if err != nil {
  209. log.Printf("An error occured: %s\n", err.Error())
  210. Util.JsonReturn(w, 500, "An error occured")
  211. return
  212. }
  213. // Return updated json
  214. w.WriteHeader(http.StatusOK)
  215. w.Write(returnJson)
  216. }
  217. func deletePost(w http.ResponseWriter, r *http.Request) {
  218. var (
  219. postData Models.Post
  220. err error
  221. )
  222. _, err = Auth.CheckCookie(r)
  223. if err != nil {
  224. w.WriteHeader(http.StatusUnauthorized)
  225. return
  226. }
  227. postData, err = Util.GetPostById(w, r)
  228. if err != nil {
  229. return
  230. }
  231. err = Database.DeletePost(&postData)
  232. if err != nil {
  233. log.Printf("An error occured: %s\n", err.Error())
  234. Util.JsonReturn(w, 500, "An error occured")
  235. return
  236. }
  237. // Return updated json
  238. w.WriteHeader(http.StatusOK)
  239. }