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.

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