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.

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