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.
 
 

215 line
7.3 KiB

  1. //
  2. // CustomerCtrl.swift
  3. //
  4. //
  5. // Created by Michiel Carman on 11/4/21.
  6. //
  7. import Foundation
  8. import Vapor
  9. import Fluent
  10. import FluentPostgresDriver
  11. final class CustomerCtrl{
  12. //Create Unit
  13. func create(req: Request) throws -> EventLoopFuture<mdlCustomer.Output>{
  14. let input = try req.content.decode(mdlCustomer.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<mdlCustomer.Output>{
  20. let input = try req.content.decode(mdlCustomer.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<mdlCustomer.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<mdlCustomer.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 mdlCustomer.find(id, on: req.db).unwrap(or: Abort(.notFound)).flatMap{ $0.delete(on: req.db)}.map{.ok}
  45. }
  46. }
  47. extension CustomerCtrl{
  48. //Query Settings
  49. func qbase(req: Request) throws -> QueryBuilder<mdlCustomer> {
  50. let query = mdlCustomer.query(on: req.db)
  51. .with(\.$contacts)
  52. .with(\.$templates)
  53. .with(\.$workorders)
  54. .with(\.$machines)
  55. if let id = req.parameters.get("id", as: Int.self){
  56. return query.filter(\.$id == id)
  57. } else{
  58. return query
  59. }
  60. }
  61. //create Output
  62. func createRead(_ output: mdlCustomer) -> mdlCustomer.Output {
  63. return mdlCustomer.Output(
  64. id: output.id!,
  65. number: output.number,
  66. name: output.name,
  67. address: output.address,
  68. address2: output.address2,
  69. city: output.city,
  70. state: output.state,
  71. zip: output.zip,
  72. phone1: output.phone1,
  73. phone2: output.phone2,
  74. fax: output.fax,
  75. isactive: output.isactive,
  76. createdate: output.createdate,
  77. createuserid: output.createuserid,
  78. updatedate: output.updatedate,
  79. updateuserid: output.updateuserid,
  80. contacts: output.contacts,
  81. machines: output.machines,
  82. templates: output.templates,
  83. workorders: output.workorders
  84. )
  85. }
  86. func mapOutput(_ output: Optional<mdlCustomer>.WrappedType, _ input: mdlCustomer.Input){
  87. output.number = input.number;
  88. output.name = input.name;
  89. output.address = input.address;
  90. output.address2 = input.address2;
  91. output.city = input.city;
  92. output.state = input.state;
  93. output.zip = input.zip;
  94. output.phone1 = input.phone1;
  95. output.phone2 = input.phone2;
  96. output.fax = input.fax;
  97. output.isactive = input.isactive;
  98. output.createdate = input.createdate;
  99. output.updatedate = input.updatedate;
  100. output.createuserid = input.createuserid;
  101. output.updateuserid = input.updateuserid;
  102. output.$contacts.value = input.contacts;
  103. output.$machines.value = input.machines;
  104. output.$templates.value = input.templates;
  105. output.$workorders.value = input.workorders
  106. }
  107. func createUnit(_ input: mdlCustomer.Input) -> mdlCustomer {
  108. return mdlCustomer(
  109. id: input.id ?? nil
  110. , number: input.number
  111. , name: input.name
  112. , address: input.address
  113. , address2: input.address2
  114. , city: input.city
  115. , state: input.state
  116. , zip: input.zip
  117. , phone1: input.phone1
  118. , phone2: input.phone2
  119. , fax: input.fax
  120. , isactive: input.isactive
  121. , createuserid: input.createuserid ?? -1
  122. , createdate: input.createdate
  123. , updateuserid: input.updateuserid ?? -1
  124. , updatedate: input.updatedate
  125. , contacts: input.contacts
  126. , machines: input.machines
  127. , templates: input.templates
  128. , workorders: input.workorders
  129. )
  130. }
  131. //update function
  132. func save(_ output: Optional<mdlCustomer>.WrappedType, req: Request) -> EventLoopFuture<mdlCustomer.Output> {
  133. return output
  134. .save(on: req.db)
  135. .map{mdlCustomer.Output(
  136. id: output.id!
  137. , number: output.number
  138. , name: output.name
  139. , address: output.address
  140. , address2: output.address2
  141. , city: output.city
  142. , state: output.state
  143. , zip: output.zip
  144. , phone1: output.phone1
  145. , phone2: output.phone2
  146. , fax: output.fax
  147. , isactive: output.isactive
  148. , createdate: output.createdate
  149. , createuserid: output.createuserid
  150. , updatedate: output.updatedate
  151. , updateuserid: output.updateuserid
  152. , contacts: output.contacts
  153. , machines: output.machines
  154. , templates: output.templates
  155. , workorders: output.workorders
  156. )
  157. }
  158. }
  159. //create function
  160. func save(_ output: mdlCustomer, _ req: Request, _ input: mdlCustomer.Input) -> EventLoopFuture<mdlCustomer.Output> {
  161. return output
  162. .save(on: req.db)
  163. .map{
  164. mdlCustomer.Output(
  165. id: output.id!
  166. , number: output.number
  167. , name: output.name
  168. , address: output.address
  169. , address2: output.address2
  170. , city: output.city
  171. , state: output.state
  172. , zip: output.zip
  173. , phone1: output.phone1
  174. , phone2: output.phone2
  175. , fax: output.fax
  176. , isactive: output.isactive
  177. , createdate: output.createdate
  178. , createuserid: output.createuserid
  179. , updatedate: output.updatedate
  180. , updateuserid: output.updateuserid
  181. , contacts: input.contacts
  182. , machines: input.machines
  183. , templates: input.templates
  184. , workorders: input.workorders
  185. )
  186. }
  187. }
  188. }
  189. extension CustomerCtrl: RouteCollection{
  190. func boot(routes: RoutesBuilder) throws{
  191. let apigroup = routes.grouped("customers")
  192. apigroup.get("", use: readAll)
  193. apigroup.get(":id", use: read)
  194. apigroup.post("", use: create)
  195. apigroup.post(":id", use: update)
  196. apigroup.delete(":id", use: delete)
  197. }
  198. }