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

171 рядки
5.9 KiB

  1. //
  2. // ContractCtrl.swift
  3. //
  4. //
  5. // Created by Michiel Carman on 11/5/21.
  6. //
  7. import Foundation
  8. import Vapor
  9. import Fluent
  10. import FluentPostgresDriver
  11. final class ContactCtrl{
  12. //Create Unit
  13. func create(req: Request) throws -> EventLoopFuture<mdlContact.Output>{
  14. let input = try req.content.decode(mdlContact.Input.self)
  15. let output = self.createUnit(input)
  16. return self.save(output, req, input)
  17. }
  18. //Update Unit
  19. func update(req: Request) throws -> EventLoopFuture<mdlContact.Output>{
  20. let input = try req.content.decode(mdlContact.Input.self)
  21. return try qbase(req: req).first().unwrap(or: Abort(.notFound)).flatMap{output in self.mapOutput(output, input)
  22. return self.save(output, req: req)
  23. }
  24. }
  25. //Get all Divisions
  26. func readAll(req: Request) throws -> EventLoopFuture<Page<mdlContact.Output>>{
  27. return try qbase(req: req)
  28. .paginate(for: req)
  29. .map{
  30. page in page.map{
  31. self.createRead($0)
  32. }
  33. }
  34. }
  35. //Get 1 Division
  36. func read(req: Request) throws -> EventLoopFuture<mdlContact.Output>{
  37. return try qbase(req: req).first().unwrap(or: Abort(.internalServerError)).map{self.createRead($0)}
  38. }
  39. //Delete 1 Division
  40. func delete(req: Request) throws -> EventLoopFuture<HTTPStatus>{
  41. guard let id = req.parameters.get("id", as: Int.self) else{
  42. throw Abort(.badRequest)
  43. }
  44. return mdlContact.find(id, on: req.db).unwrap(or: Abort(.notFound)).flatMap{ $0.delete(on: req.db)}.map{.ok}
  45. }
  46. }
  47. extension ContactCtrl{
  48. //Query Settings
  49. func qbase(req: Request) throws -> QueryBuilder<mdlContact> {
  50. let query = mdlContact.query(on: req.db).with(\.$customer).with(\.$location).with(\.$contacttypes).with(\.$files)
  51. if let id = req.parameters.get("id", as: Int.self){
  52. return query.filter(\.$id == id)
  53. } else{
  54. return query
  55. }
  56. }
  57. //create Output
  58. func createRead(_ output: mdlContact) -> mdlContact.Output {
  59. return mdlContact.Output(
  60. id: output.id!,
  61. firstname: output.firstname,
  62. lastname: output.lastname,
  63. isactive: output.isactive,
  64. createdate: output.createdate,
  65. createuserid: output.createuserid,
  66. updatedate: output.updatedate,
  67. updateuserid: output.updateuserid,
  68. customer: output.$customer.value!,
  69. location: output.$location.value!,
  70. contacttypes: output.$contacttypes.value!,
  71. files: output.$files.value!
  72. )
  73. }
  74. func mapOutput(_ output: Optional<mdlContact>.WrappedType, _ input: mdlContact.Input){
  75. output.firstname = input.firstname;
  76. output.lastname = input.lastname;
  77. output.isactive = input.isactive;
  78. output.createdate = input.createdate;
  79. output.updatedate = input.updatedate;
  80. output.createuserid = input.createuserid;
  81. output.updateuserid = input.updateuserid;
  82. output.$customer.id = input.customer.id!;
  83. output.$location.id = input.location.id!;
  84. output.$contacttypes.value = input.contacttypes;
  85. output.$files.value = input.files
  86. }
  87. func createUnit(_ input: mdlContact.Input) -> mdlContact {
  88. return mdlContact(
  89. id: input.id ?? nil
  90. , firstname: input.firstname
  91. , lastname: input.lastname
  92. , isactive: input.isactive
  93. , createuserid: input.createuserid ?? -1
  94. , createdate: input.createdate
  95. , updateuserid: input.updateuserid ?? -1
  96. , updatedate: input.updatedate
  97. , customer: input.customer
  98. , location: input.location
  99. , contacttypes: input.contacttypes
  100. , files: input.files
  101. )
  102. }
  103. //update function
  104. func save(_ output: Optional<mdlContact>.WrappedType, req: Request) -> EventLoopFuture<mdlContact.Output> {
  105. return output
  106. .save(on: req.db)
  107. .map{mdlContact.Output(
  108. id: output.id!
  109. , firstname: output.firstname
  110. , lastname: output.lastname
  111. , isactive: output.isactive
  112. , createdate: output.createdate
  113. , createuserid: output.createuserid
  114. , updatedate: output.updatedate
  115. , updateuserid: output.updateuserid
  116. , customer: output.customer
  117. , location: output.location
  118. , contacttypes: output.contacttypes
  119. , files: output.files
  120. )
  121. }
  122. }
  123. //create function
  124. func save(_ output: mdlContact, _ req: Request, _ input: mdlContact.Input) -> EventLoopFuture<mdlContact.Output> {
  125. return output
  126. .save(on: req.db)
  127. .map{
  128. mdlContact.Output(
  129. id: output.id!
  130. , firstname: output.firstname
  131. , lastname: output.lastname
  132. , isactive: output.isactive
  133. , createdate: output.createdate
  134. , createuserid: output.createuserid
  135. , updatedate: output.updatedate
  136. , updateuserid: output.updateuserid
  137. , customer: input.customer
  138. , location: input.location
  139. , contacttypes: input.contacttypes
  140. , files: input.files
  141. )
  142. }
  143. }
  144. }
  145. extension ContactCtrl: RouteCollection{
  146. func boot(routes: RoutesBuilder) throws{
  147. let apigroup = routes.grouped("contacts")
  148. apigroup.get("", use: readAll)
  149. apigroup.get(":id", use: read)
  150. apigroup.post("", use: create)
  151. apigroup.post(":id", use: update)
  152. apigroup.delete(":id", use: delete)
  153. }
  154. }