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.
 
 
 

82 lines
3.0 KiB

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