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

82 строки
2.9 KiB

  1. //
  2. // cntrlDepartment.swift
  3. //
  4. //
  5. // Created by Michiel Carman on 4/11/2024.
  6. //
  7. import Fluent
  8. import Vapor
  9. import Crypto
  10. struct cntrlDepartment: RouteCollection {
  11. func boot(routes: RoutesBuilder) throws {
  12. let rts = routes.grouped("departments")
  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 mdlDepartment.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 mdlDepartment.ObjQuery(req: req).first() else{
  32. throw Abort(.notFound, reason: "Department 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(mdlDepartment.self) else {
  42. throw Abort(.notAcceptable, reason: "Department 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. Department 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. Department has been saved.")
  54. }
  55. return response
  56. }
  57. @Sendable
  58. func delete(req: Request) async throws -> Response {
  59. guard let obj = try? await mdlDepartment.ObjQuery(req: req).first() else {
  60. throw Abort(.notFound, reason: "Department's ID was not supplied. Department 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 Department. 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. Department has been deleted.")
  67. }
  68. return response
  69. }
  70. }