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. 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. users, err = Database.GetUsers(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(users) == 0 {
  45. Util.JsonReturn(w, 404, "No more data")
  46. return
  47. }
  48. returnJson, err = json.MarshalIndent(users, "", " ")
  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 getUser(w http.ResponseWriter, r *http.Request) {
  58. var (
  59. userData Models.User
  60. returnJson []byte
  61. err error
  62. )
  63. userData, err = Util.GetUserById(w, r)
  64. if err != nil {
  65. return
  66. }
  67. returnJson, err = json.MarshalIndent(userData, "", " ")
  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 createUser(w http.ResponseWriter, r *http.Request) {
  77. var (
  78. userData Models.User
  79. requestBody []byte
  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. userData, err = JsonSerialization.DeserializeUser(requestBody, []string{
  90. "id",
  91. "last_login",
  92. }, false)
  93. if err != nil {
  94. log.Printf("Invalid data provided to user API: %s\n", err.Error())
  95. Util.JsonReturn(w, 405, "Invalid data")
  96. return
  97. }
  98. if userData.FirstName == "" ||
  99. userData.LastName == "" ||
  100. userData.Email == "" ||
  101. userData.Password == "" ||
  102. userData.ConfirmPassword == "" {
  103. Util.JsonReturn(w, http.StatusUnprocessableEntity, "Invalid data")
  104. return
  105. }
  106. err = Database.CheckUniqueEmail(userData.Email)
  107. if err != nil || !Util.IsEmailValid(userData.Email) {
  108. Util.JsonReturn(w, 405, "invalid_email")
  109. return
  110. }
  111. if userData.Password != userData.ConfirmPassword {
  112. Util.JsonReturn(w, 405, "invalid_password")
  113. return
  114. }
  115. userData.Password, err = Auth.HashPassword(userData.Password)
  116. if err != nil {
  117. Util.JsonReturn(w, 500, "An error occured")
  118. return
  119. }
  120. err = Database.CreateUser(&userData)
  121. if err != nil {
  122. Util.JsonReturn(w, 500, "An error occured")
  123. return
  124. }
  125. returnJson, err = json.MarshalIndent(userData, "", " ")
  126. if err != nil {
  127. Util.JsonReturn(w, 500, "An error occured")
  128. return
  129. }
  130. // Return updated json
  131. w.WriteHeader(http.StatusOK)
  132. w.Write(returnJson)
  133. }
  134. func updateUser(w http.ResponseWriter, r *http.Request) {
  135. var (
  136. userData Models.User
  137. requestBody []byte
  138. returnJson []byte
  139. id string
  140. err error
  141. )
  142. id, err = Util.GetUserId(r)
  143. if err != nil {
  144. log.Printf("Error encountered reading POST body: %s\n", err.Error())
  145. Util.JsonReturn(w, 500, "An error occured")
  146. return
  147. }
  148. requestBody, err = ioutil.ReadAll(r.Body)
  149. if err != nil {
  150. log.Printf("Error encountered reading POST body: %s\n", err.Error())
  151. Util.JsonReturn(w, 500, "An error occured")
  152. return
  153. }
  154. userData, err = JsonSerialization.DeserializeUser(requestBody, []string{}, true)
  155. if err != nil {
  156. log.Printf("Invalid data provided to users API: %s\n", err.Error())
  157. Util.JsonReturn(w, 405, "Invalid data")
  158. return
  159. }
  160. err = Database.UpdateUser(id, &userData)
  161. if err != nil {
  162. log.Printf("An error occured: %s\n", err.Error())
  163. Util.JsonReturn(w, 500, "An error occured")
  164. return
  165. }
  166. returnJson, err = json.MarshalIndent(userData, "", " ")
  167. if err != nil {
  168. log.Printf("An error occured: %s\n", err.Error())
  169. Util.JsonReturn(w, 500, "An error occured")
  170. return
  171. }
  172. // Return updated json
  173. w.WriteHeader(http.StatusOK)
  174. w.Write(returnJson)
  175. }
  176. func deleteUser(w http.ResponseWriter, r *http.Request) {
  177. var (
  178. userData Models.User
  179. err error
  180. )
  181. userData, err = Util.GetUserById(w, r)
  182. if err != nil {
  183. w.WriteHeader(http.StatusNotFound)
  184. return
  185. }
  186. err = Database.DeleteUser(&userData)
  187. if err != nil {
  188. log.Printf("An error occured: %s\n", err.Error())
  189. Util.JsonReturn(w, 500, "An error occured")
  190. return
  191. }
  192. // Return updated json
  193. w.WriteHeader(http.StatusOK)
  194. }