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.

203 lines
4.2 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. )
  14. func getUsers(w http.ResponseWriter, r *http.Request) {
  15. var (
  16. users []Models.User
  17. returnJson []byte
  18. values url.Values
  19. page, pageSize int
  20. err error
  21. )
  22. values = r.URL.Query()
  23. page, err = strconv.Atoi(values.Get("page"))
  24. if err != nil {
  25. log.Println("Could not parse page url argument")
  26. JsonReturn(w, 500, "An error occured")
  27. return
  28. }
  29. page, err = strconv.Atoi(values.Get("pageSize"))
  30. if err != nil {
  31. log.Println("Could not parse pageSize url argument")
  32. JsonReturn(w, 500, "An error occured")
  33. return
  34. }
  35. users, err = Database.GetUsers(page, pageSize)
  36. if err != nil {
  37. log.Printf("An error occured: %s\n", err.Error())
  38. JsonReturn(w, 500, "An error occured")
  39. return
  40. }
  41. returnJson, err = json.MarshalIndent(users, "", " ")
  42. if err != nil {
  43. JsonReturn(w, 500, "An error occured")
  44. return
  45. }
  46. // Return updated json
  47. w.WriteHeader(http.StatusOK)
  48. w.Write(returnJson)
  49. }
  50. func getUser(w http.ResponseWriter, r *http.Request) {
  51. var (
  52. userData Models.User
  53. returnJson []byte
  54. err error
  55. )
  56. userData, err = getUserById(w, r)
  57. if err != nil {
  58. return
  59. }
  60. returnJson, err = json.MarshalIndent(userData, "", " ")
  61. if err != nil {
  62. JsonReturn(w, 500, "An error occured")
  63. return
  64. }
  65. // Return updated json
  66. w.WriteHeader(http.StatusOK)
  67. w.Write(returnJson)
  68. }
  69. func createUser(w http.ResponseWriter, r *http.Request) {
  70. var (
  71. userData Models.User
  72. requestBody []byte
  73. err error
  74. )
  75. requestBody, err = ioutil.ReadAll(r.Body)
  76. if err != nil {
  77. log.Printf("Error encountered reading POST body: %s\n", err.Error())
  78. JsonReturn(w, 500, "An error occured")
  79. return
  80. }
  81. userData, err = JsonSerialization.DeserializeUser(requestBody, []string{
  82. "id",
  83. "last_login",
  84. }, false)
  85. if err != nil {
  86. log.Printf("Invalid data provided to user API: %s\n", err.Error())
  87. JsonReturn(w, 405, "Invalid data")
  88. return
  89. }
  90. err = Database.CheckUniqueEmail(userData.Email)
  91. if err != nil {
  92. JsonReturn(w, 405, "invalid_email")
  93. return
  94. }
  95. if userData.Password != userData.ConfirmPassword {
  96. JsonReturn(w, 405, "invalid_password")
  97. return
  98. }
  99. userData.Password, err = Auth.HashPassword(userData.Password)
  100. if err != nil {
  101. JsonReturn(w, 500, "An error occured")
  102. return
  103. }
  104. err = Database.CreateUser(&userData)
  105. if err != nil {
  106. JsonReturn(w, 500, "An error occured")
  107. return
  108. }
  109. // Return updated json
  110. w.WriteHeader(http.StatusOK)
  111. }
  112. func updateUser(w http.ResponseWriter, r *http.Request) {
  113. var (
  114. userData Models.User
  115. requestBody []byte
  116. returnJson []byte
  117. id string
  118. err error
  119. )
  120. id, err = getUserId(r)
  121. if err != nil {
  122. log.Printf("Error encountered getting id\n")
  123. JsonReturn(w, 500, "An error occured")
  124. return
  125. }
  126. requestBody, err = ioutil.ReadAll(r.Body)
  127. if err != nil {
  128. log.Printf("Error encountered reading POST body: %s\n", err.Error())
  129. JsonReturn(w, 500, "An error occured")
  130. return
  131. }
  132. userData, err = JsonSerialization.DeserializeUser(requestBody, []string{}, true)
  133. if err != nil {
  134. log.Printf("Invalid data provided to users API: %s\n", err.Error())
  135. JsonReturn(w, 405, "Invalid data")
  136. return
  137. }
  138. err = Database.UpdateUser(id, &userData)
  139. if err != nil {
  140. log.Printf("An error occured: %s\n", err.Error())
  141. JsonReturn(w, 500, "An error occured")
  142. return
  143. }
  144. returnJson, err = json.MarshalIndent(userData, "", " ")
  145. if err != nil {
  146. log.Printf("An error occured: %s\n", err.Error())
  147. JsonReturn(w, 500, "An error occured")
  148. return
  149. }
  150. // Return updated json
  151. w.WriteHeader(http.StatusOK)
  152. w.Write(returnJson)
  153. }
  154. func deleteUser(w http.ResponseWriter, r *http.Request) {
  155. var (
  156. userData Models.User
  157. err error
  158. )
  159. userData, err = getUserById(w, r)
  160. if err != nil {
  161. return
  162. }
  163. err = Database.DeleteUser(&userData)
  164. if err != nil {
  165. log.Printf("An error occured: %s\n", err.Error())
  166. JsonReturn(w, 500, "An error occured")
  167. return
  168. }
  169. // Return updated json
  170. w.WriteHeader(http.StatusOK)
  171. }