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.

257 lines
5.3 KiB

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