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.
 
 

110 regels
4.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).openAPI(
  16. // summary: "Create User",
  17. // description: "Create a new user with the provided data",
  18. // body: .type(mdlUser.self),
  19. // response: .type(mdlUser.self)
  20. // )
  21. rts.post(use: create)
  22. rts.delete(":x", use: delete)
  23. }
  24. func login(req: Request) async throws -> Response{
  25. guard let obj = try? await mdlUser.ObjQuery(req: req).first() else {
  26. throw Abort(.unauthorized, reason: "Username or Password or Both are invalid.")
  27. }
  28. guard let response = try? await obj.encodeResponse(status: .ok, for: req) else {
  29. throw Abort(.noContent, reason: "Something went wrong decoding data. User has been authenticated.")
  30. }
  31. return response
  32. }
  33. func index(req: Request) async throws -> Response {
  34. guard let obj = try? await mdlUser.ObjQuery(req: req).all() else {
  35. throw Abort(.badGateway, reason: "Something went wrong with this api.")
  36. }
  37. guard let response = try? await obj.encodeResponse(status: .ok, for: req) else{
  38. throw Abort(.noContent, reason: "Something went wrong decoding data. Array could not be shown.")
  39. }
  40. return response
  41. }
  42. func read(req:Request) async throws -> Response{
  43. guard let obj = try? await mdlUser.ObjQuery(req: req).first() else{
  44. throw Abort(.notFound, reason: "User was not found.")
  45. }
  46. guard let response = try? await obj.encodeResponse(status: .ok, for: req) else {
  47. throw Abort(.noContent, reason: "Something went wrong decoding object.")
  48. }
  49. return response
  50. }
  51. func create(req: Request) async throws -> Response {
  52. guard let obj = try? req.content.decode(mdlUser.self) else {
  53. throw Abort(.notAcceptable, reason: "User json not formatted correctly or is missing parents.")
  54. }
  55. var exists = false
  56. if(obj.id != nil){
  57. obj._$idExists = true
  58. exists = true
  59. }
  60. guard let _ = try? await obj.save(on: req.db) else{
  61. throw Abort(.conflict, reason: "Something conflicts in the database. User cannot be saved.")
  62. }
  63. guard let response = try? await obj.encodeResponse(status: exists ? .accepted : .created, for: req) else{
  64. throw Abort(.noContent, reason: "Something went wrong decoding data. User has been saved.")
  65. }
  66. return response
  67. }
  68. func delete(req: Request) async throws -> Response {
  69. guard let obj = try? await mdlUser.ObjQuery(req: req).first() else {
  70. throw Abort(.notFound, reason: "User's ID was not supplied. User will not be deleted")
  71. }
  72. guard let _ = try? await obj.delete(on: req.db) else{
  73. throw Abort(.conflict, reason: "There was a conflict deleting User. Make sure it has not children.")
  74. }
  75. guard let response = try? await obj.encodeResponse(status: .ok, for: req) else{
  76. throw Abort(.noContent, reason: "Something went wrong decoding data. User has been deleted.")
  77. }
  78. return response
  79. }
  80. func createToken(req: Request) async throws -> Response{
  81. guard let obj = try? await mdlUser.ObjQuery(req: req).field(\.$id).field(\.$username).first() else{
  82. throw Abort(.notFound, reason: "User was not found.")
  83. }
  84. guard let exp = try? await mdlLicense.query(on: req.db).first() else {
  85. throw Abort(.notFound)
  86. }
  87. let token = Token(userid: obj.id!, token: [UInt8].random(count: 16).base64, source: .signup, expiresat: exp.expires)
  88. try await token.save(on: req.db)
  89. guard let response = try? await token.encodeResponse(status: .ok, for: req) else{
  90. throw Abort(.noContent, reason: "Something went wrong decoding data. Token has been deleted.")
  91. }
  92. return response
  93. }
  94. }