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.

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