Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.
 
 

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