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.

221 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/Auth"
  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 getUsers(w http.ResponseWriter, r *http.Request) {
  16. var (
  17. users []Models.User
  18. returnJson []byte
  19. values url.Values
  20. page, pageSize int
  21. err error
  22. )
  23. _, err = Auth.CheckCookie(r)
  24. if err != nil {
  25. w.WriteHeader(http.StatusUnauthorized)
  26. return
  27. }
  28. values = r.URL.Query()
  29. page, err = strconv.Atoi(values.Get("page"))
  30. if err != nil {
  31. log.Println("Could not parse page url argument")
  32. Util.JsonReturn(w, 500, "An error occured")
  33. return
  34. }
  35. page, err = strconv.Atoi(values.Get("pageSize"))
  36. if err != nil {
  37. log.Println("Could not parse pageSize url argument")
  38. Util.JsonReturn(w, 500, "An error occured")
  39. return
  40. }
  41. users, err = Database.GetUsers(page, pageSize)
  42. if err != nil {
  43. log.Printf("An error occured: %s\n", err.Error())
  44. Util.JsonReturn(w, 500, "An error occured")
  45. return
  46. }
  47. returnJson, err = json.MarshalIndent(users, "", " ")
  48. if err != nil {
  49. Util.JsonReturn(w, 500, "An error occured")
  50. return
  51. }
  52. // Return updated json
  53. w.WriteHeader(http.StatusOK)
  54. w.Write(returnJson)
  55. }
  56. func getUser(w http.ResponseWriter, r *http.Request) {
  57. var (
  58. userData Models.User
  59. returnJson []byte
  60. err error
  61. )
  62. _, err = Auth.CheckCookie(r)
  63. if err != nil {
  64. w.WriteHeader(http.StatusUnauthorized)
  65. return
  66. }
  67. userData, err = Util.GetUserById(w, r)
  68. if err != nil {
  69. return
  70. }
  71. returnJson, err = json.MarshalIndent(userData, "", " ")
  72. if err != nil {
  73. Util.JsonReturn(w, 500, "An error occured")
  74. return
  75. }
  76. // Return updated json
  77. w.WriteHeader(http.StatusOK)
  78. w.Write(returnJson)
  79. }
  80. func createUser(w http.ResponseWriter, r *http.Request) {
  81. var (
  82. userData Models.User
  83. requestBody []byte
  84. err error
  85. )
  86. requestBody, err = ioutil.ReadAll(r.Body)
  87. if err != nil {
  88. log.Printf("Error encountered reading POST body: %s\n", err.Error())
  89. Util.JsonReturn(w, 500, "An error occured")
  90. return
  91. }
  92. userData, err = JsonSerialization.DeserializeUser(requestBody, []string{
  93. "id",
  94. "last_login",
  95. }, false)
  96. if err != nil {
  97. log.Printf("Invalid data provided to user API: %s\n", err.Error())
  98. Util.JsonReturn(w, 405, "Invalid data")
  99. return
  100. }
  101. err = Database.CheckUniqueEmail(userData.Email)
  102. if err != nil {
  103. Util.JsonReturn(w, 405, "invalid_email")
  104. return
  105. }
  106. if userData.Password != userData.ConfirmPassword {
  107. Util.JsonReturn(w, 405, "invalid_password")
  108. return
  109. }
  110. userData.Password, err = Auth.HashPassword(userData.Password)
  111. if err != nil {
  112. Util.JsonReturn(w, 500, "An error occured")
  113. return
  114. }
  115. err = Database.CreateUser(&userData)
  116. if err != nil {
  117. Util.JsonReturn(w, 500, "An error occured")
  118. return
  119. }
  120. // Return updated json
  121. w.WriteHeader(http.StatusOK)
  122. }
  123. func updateUser(w http.ResponseWriter, r *http.Request) {
  124. var (
  125. currentUserData Models.User
  126. userData Models.User
  127. requestBody []byte
  128. returnJson []byte
  129. err error
  130. )
  131. currentUserData, err = Auth.CheckCookieCurrentUser(w, r)
  132. if err != nil {
  133. w.WriteHeader(http.StatusUnauthorized)
  134. return
  135. }
  136. requestBody, err = ioutil.ReadAll(r.Body)
  137. if err != nil {
  138. log.Printf("Error encountered reading POST body: %s\n", err.Error())
  139. Util.JsonReturn(w, 500, "An error occured")
  140. return
  141. }
  142. userData, err = JsonSerialization.DeserializeUser(requestBody, []string{}, true)
  143. if err != nil {
  144. log.Printf("Invalid data provided to users API: %s\n", err.Error())
  145. Util.JsonReturn(w, 405, "Invalid data")
  146. return
  147. }
  148. err = Database.UpdateUser(currentUserData.ID.String(), &userData)
  149. if err != nil {
  150. log.Printf("An error occured: %s\n", err.Error())
  151. Util.JsonReturn(w, 500, "An error occured")
  152. return
  153. }
  154. returnJson, err = json.MarshalIndent(userData, "", " ")
  155. if err != nil {
  156. log.Printf("An error occured: %s\n", err.Error())
  157. Util.JsonReturn(w, 500, "An error occured")
  158. return
  159. }
  160. // Return updated json
  161. w.WriteHeader(http.StatusOK)
  162. w.Write(returnJson)
  163. }
  164. func deleteUser(w http.ResponseWriter, r *http.Request) {
  165. var (
  166. userData Models.User
  167. err error
  168. )
  169. _, err = Auth.CheckCookie(r)
  170. if err != nil {
  171. w.WriteHeader(http.StatusUnauthorized)
  172. return
  173. }
  174. userData, err = Util.GetUserById(w, r)
  175. if err != nil {
  176. w.WriteHeader(http.StatusNotFound)
  177. return
  178. }
  179. err = Database.DeleteUser(&userData)
  180. if err != nil {
  181. log.Printf("An error occured: %s\n", err.Error())
  182. Util.JsonReturn(w, 500, "An error occured")
  183. return
  184. }
  185. // Return updated json
  186. w.WriteHeader(http.StatusOK)
  187. }