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.

230 lines
5.0 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. if userData.FirstName == "" ||
  102. userData.LastName == "" ||
  103. userData.Email == "" ||
  104. userData.Password == "" ||
  105. userData.ConfirmPassword == "" {
  106. Util.JsonReturn(w, http.StatusUnprocessableEntity, "Invalid data")
  107. return
  108. }
  109. err = Database.CheckUniqueEmail(userData.Email)
  110. if err != nil || !Util.IsEmailValid(userData.Email) {
  111. Util.JsonReturn(w, 405, "invalid_email")
  112. return
  113. }
  114. if userData.Password != userData.ConfirmPassword {
  115. Util.JsonReturn(w, 405, "invalid_password")
  116. return
  117. }
  118. userData.Password, err = Auth.HashPassword(userData.Password)
  119. if err != nil {
  120. Util.JsonReturn(w, 500, "An error occured")
  121. return
  122. }
  123. err = Database.CreateUser(&userData)
  124. if err != nil {
  125. Util.JsonReturn(w, 500, "An error occured")
  126. return
  127. }
  128. // Return updated json
  129. w.WriteHeader(http.StatusOK)
  130. }
  131. func updateUser(w http.ResponseWriter, r *http.Request) {
  132. var (
  133. currentUserData Models.User
  134. userData Models.User
  135. requestBody []byte
  136. returnJson []byte
  137. err error
  138. )
  139. currentUserData, err = Auth.CheckCookieCurrentUser(w, r)
  140. if err != nil {
  141. w.WriteHeader(http.StatusUnauthorized)
  142. return
  143. }
  144. requestBody, err = ioutil.ReadAll(r.Body)
  145. if err != nil {
  146. log.Printf("Error encountered reading POST body: %s\n", err.Error())
  147. Util.JsonReturn(w, 500, "An error occured")
  148. return
  149. }
  150. userData, err = JsonSerialization.DeserializeUser(requestBody, []string{}, true)
  151. if err != nil {
  152. log.Printf("Invalid data provided to users API: %s\n", err.Error())
  153. Util.JsonReturn(w, 405, "Invalid data")
  154. return
  155. }
  156. err = Database.UpdateUser(currentUserData.ID.String(), &userData)
  157. if err != nil {
  158. log.Printf("An error occured: %s\n", err.Error())
  159. Util.JsonReturn(w, 500, "An error occured")
  160. return
  161. }
  162. returnJson, err = json.MarshalIndent(userData, "", " ")
  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. // Return updated json
  169. w.WriteHeader(http.StatusOK)
  170. w.Write(returnJson)
  171. }
  172. func deleteUser(w http.ResponseWriter, r *http.Request) {
  173. var (
  174. userData Models.User
  175. err error
  176. )
  177. _, err = Auth.CheckCookie(r)
  178. if err != nil {
  179. w.WriteHeader(http.StatusUnauthorized)
  180. return
  181. }
  182. userData, err = Util.GetUserById(w, r)
  183. if err != nil {
  184. w.WriteHeader(http.StatusNotFound)
  185. return
  186. }
  187. err = Database.DeleteUser(&userData)
  188. if err != nil {
  189. log.Printf("An error occured: %s\n", err.Error())
  190. Util.JsonReturn(w, 500, "An error occured")
  191. return
  192. }
  193. // Return updated json
  194. w.WriteHeader(http.StatusOK)
  195. }