Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.
 
 
 

84 рядки
2.9 KiB

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