Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

88 строки
3.2 KiB

  1. //
  2. // cntrlUser.swift
  3. //
  4. //
  5. // Created by Michiel Carman on 4/1/24.
  6. //
  7. import Fluent
  8. import Vapor
  9. import Crypto
  10. struct cntrlUser: RouteCollection {
  11. func boot(routes: RoutesBuilder) throws {
  12. let rts = routes.grouped("users")
  13. rts.get(use: index)
  14. rts.get("login", ":x", use: login)
  15. rts.get(":x", use: read)
  16. rts.post(use: create)
  17. rts.delete(":x", use: delete)
  18. }
  19. func login(req: Request) async throws -> Response{
  20. guard let obj = try? await mdlUser.ObjQuery(req: req).first() else {
  21. throw Abort(.unauthorized, reason: "Username or Password or Both are invalid.")
  22. }
  23. guard let response = try? await obj.encodeResponse(status: .ok, for: req) else {
  24. throw Abort(.noContent, reason: "Something went wrong decoding data. User has been authenticated.")
  25. }
  26. return response
  27. }
  28. func index(req: Request) async throws -> Response {
  29. guard let obj = try? await mdlUser.ObjQuery(req: req).all() else {
  30. throw Abort(.badGateway, reason: "Something went wrong with this api.")
  31. }
  32. guard let response = try? await obj.encodeResponse(status: .ok, for: req) else{
  33. throw Abort(.noContent, reason: "Something went wrong decoding data. Array could not be shown.")
  34. }
  35. return response
  36. }
  37. func read(req:Request) async throws -> Response{
  38. guard let obj = try? await mdlUser.ObjQuery(req: req).first() else{
  39. throw Abort(.notFound, reason: "User was not found.")
  40. }
  41. guard let response = try? await obj.encodeResponse(status: .ok, for: req) else {
  42. throw Abort(.noContent, reason: "Something went wrong decoding object.")
  43. }
  44. return response
  45. }
  46. func create(req: Request) async throws -> Response {
  47. guard let obj = try? req.content.decode(mdlUser.self) else {
  48. throw Abort(.notAcceptable, reason: "User json not formatted correctly or is missing parents.")
  49. }
  50. var exists = false
  51. if(obj.id != nil){
  52. obj._$idExists = true
  53. exists = true
  54. }
  55. guard let _ = try? await obj.save(on: req.db) else{
  56. throw Abort(.conflict, reason: "Something conflicts in the database. User cannot be saved.")
  57. }
  58. guard let response = try? await obj.encodeResponse(status: exists ? .accepted : .created, for: req) else{
  59. throw Abort(.noContent, reason: "Something went wrong decoding data. User has been saved.")
  60. }
  61. return response
  62. }
  63. func delete(req: Request) async throws -> Response {
  64. guard let obj = try? await mdlUser.ObjQuery(req: req).first() else {
  65. throw Abort(.notFound, reason: "User's ID was not supplied. User will not be deleted")
  66. }
  67. guard let _ = try? await obj.delete(on: req.db) else{
  68. throw Abort(.conflict, reason: "There was a conflict deleting User. Make sure it has not children.")
  69. }
  70. guard let response = try? await obj.encodeResponse(status: .ok, for: req) else{
  71. throw Abort(.noContent, reason: "Something went wrong decoding data. User has been deleted.")
  72. }
  73. return response
  74. }
  75. }