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.
 
 

105 lines
3.7 KiB

  1. //
  2. // File.swift
  3. //
  4. //
  5. // Created by Michiel Carman on 11/2/21.
  6. //
  7. import Foundation
  8. import Vapor
  9. import Fluent
  10. import FluentPostgresDriver
  11. final class GateCtrl{
  12. //Create Division
  13. func create(req: Request) throws -> EventLoopFuture<mdlGate.Output>{
  14. let input = try req.content.decode(mdlGate.Input.self)
  15. let output = buildObj(input)
  16. return output.save(on: req.db).map{
  17. self.buildObj(output)
  18. }
  19. }
  20. //Update Division
  21. func update(req: Request) throws -> EventLoopFuture<mdlGate.Output>{
  22. let input = try req.content.decode(mdlGate.Input.self)
  23. return try qbase(req: req).first().unwrap(or: Abort(.internalServerError)).flatMap{output in self.buildObj(output, input)
  24. return self.save(output, req)
  25. }
  26. }
  27. //Get all Divisions
  28. func readAll(req: Request) throws -> EventLoopFuture<Page<mdlGate.Output>>{
  29. return try qbase(req: req)
  30. .paginate(for: req)
  31. .map{
  32. page in page.map{
  33. self.buildObj($0)
  34. }
  35. }
  36. }
  37. //Get 1 Division
  38. func read(req: Request) throws -> EventLoopFuture<mdlGate.Output>{
  39. return try qbase(req: req).first().unwrap(or: Abort(.internalServerError)).map{self.buildObj($0)}
  40. }
  41. //Delete 1 Division
  42. func delete(req: Request) throws -> EventLoopFuture<HTTPStatus>{
  43. guard let id = req.parameters.get("id", as: Int.self) else{
  44. throw Abort(.badRequest)
  45. }
  46. return mdlGate.find(id, on: req.db).unwrap(or: Abort(.notFound)).flatMap{ $0.delete(on: req.db)}.map{.ok}
  47. }
  48. }
  49. extension GateCtrl{
  50. //Query Settings
  51. func qbase(req: Request) throws -> QueryBuilder<mdlGate> {
  52. let query = mdlGate.query(on: req.db).with(\.$location)
  53. if let id = req.parameters.get("id", as: Int.self){
  54. return query.filter(\.$id == id)
  55. } else{
  56. return query
  57. }
  58. }
  59. //create Input
  60. func buildObj(_ input: mdlGate.Input) -> mdlGate {
  61. return mdlGate(name: input.name, description: input.description, sortorder: input.sortorder, isactive: input.isactive, createuserid: input.createuserid, createdate: input.createdate, updateuserid: input.updateuserid, updatedate: input.updatedate, location: input.location)
  62. }
  63. //create Output
  64. func buildObj(_ output: mdlGate) -> mdlGate.Output {
  65. return mdlGate.Output(id: output.id!, name: output.name, description: output.description, sortorder: output.sortorder, isactive: output.isactive, createdate: output.createdate, createuserid: output.createuserid, updatedate: output.updatedate, updateuserid: output.updateuserid, location: output.location)
  66. }
  67. func buildObj(_ output: Optional<mdlGate>.WrappedType, _ input: mdlGate.Input){
  68. output.name = input.name;
  69. output.description = input.description;
  70. output.sortorder = input.sortorder;
  71. output.isactive = input.isactive;
  72. output.createdate = input.createdate;
  73. output.createuserid = input.createuserid;
  74. output.updatedate = input.updatedate;
  75. output.updateuserid = input.updateuserid;
  76. output.location = input.location;
  77. }
  78. func save(_ output: mdlGate, _ req: Request) -> EventLoopFuture<mdlGate.Output>{
  79. return output.save(on: req.db).map{self.buildObj(output)}
  80. }
  81. }
  82. extension GateCtrl: RouteCollection{
  83. func boot(routes: RoutesBuilder) throws{
  84. let apigroup = routes.grouped("gates")
  85. apigroup.get("", use: readAll)
  86. apigroup.get(":id", use: read)
  87. apigroup.post("", use: create)
  88. apigroup.post(":id", use: update)
  89. apigroup.delete(":id", use: delete)
  90. }
  91. }