25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.

XCustomerMachineCtrl.swift 5.2 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. //
  2. // XCustomerMachineCtrl.swift
  3. //
  4. //
  5. // Created by Michiel Carman on 11/9/21.
  6. //
  7. import Foundation
  8. import Vapor
  9. import Fluent
  10. import FluentPostgresDriver
  11. final class XCustomerMachineCtrl{
  12. //Create Unit
  13. func create(req: Request) throws -> EventLoopFuture<mdlXCustomerMachine.Output>{
  14. let input = try req.content.decode(mdlXCustomerMachine.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<mdlXCustomerMachine.Output>{
  20. let input = try req.content.decode(mdlXCustomerMachine.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<mdlXCustomerMachine.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<mdlXCustomerMachine.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 mdlXCustomerMachine.find(id, on: req.db).unwrap(or: Abort(.notFound)).flatMap{ $0.delete(on: req.db)}.map{.ok}
  45. }
  46. }
  47. extension XCustomerMachineCtrl{
  48. //Query Settings
  49. func qbase(req: Request) throws -> QueryBuilder<mdlXCustomerMachine> {
  50. let query = mdlXCustomerMachine.query(on: req.db).with(\.$customer).with(\.$machine)
  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: mdlXCustomerMachine) -> mdlXCustomerMachine.Output {
  59. return mdlXCustomerMachine.Output(
  60. id: output.id!,
  61. customer: output.$customer.value!,
  62. machine: output.$machine.value!,
  63. createdate: output.createdate,
  64. isactive: output.isactive,
  65. createuserid: output.createuserid,
  66. updatedate: output.updatedate,
  67. updateuserid: output.updateuserid
  68. )
  69. }
  70. func mapOutput(_ output: Optional<mdlXCustomerMachine>.WrappedType, _ input: mdlXCustomerMachine.Input){
  71. output.$customer.id = input.customer.id!;
  72. output.$machine.id = input.machine.id!
  73. output.createdate = input.createdate;
  74. output.isactive = input.isactive;
  75. output.createuserid = input.createuserid;
  76. output.updatedate = input.updatedate;
  77. output.updateuserid = input.updateuserid
  78. }
  79. func createUnit(_ input: mdlXCustomerMachine.Input) -> mdlXCustomerMachine {
  80. return mdlXCustomerMachine(
  81. id: input.id ?? nil
  82. , customer: input.customer
  83. , machine: input.machine
  84. , createuserid: input.createuserid ?? -1
  85. , isactive: input.isactive
  86. , createdate: input.createdate
  87. , updatedate: input.updatedate
  88. , updateuserid: input.updateuserid ?? -1
  89. )
  90. }
  91. //update function
  92. func save(_ output: Optional<mdlXCustomerMachine>.WrappedType, req: Request) -> EventLoopFuture<mdlXCustomerMachine.Output> {
  93. return output
  94. .save(on: req.db)
  95. .map{mdlXCustomerMachine.Output(
  96. id: output.id!
  97. , customer: output.customer
  98. , machine: output.machine
  99. , createdate: output.createdate
  100. , isactive: output.isactive
  101. , createuserid: output.createuserid
  102. , updatedate: output.updatedate
  103. , updateuserid: output.updateuserid
  104. )
  105. }
  106. }
  107. //create function
  108. func save(_ output: mdlXCustomerMachine, _ req: Request, _ input: mdlXCustomerMachine.Input) -> EventLoopFuture<mdlXCustomerMachine.Output> {
  109. return output
  110. .save(on: req.db)
  111. .map{
  112. mdlXCustomerMachine.Output(
  113. id: output.id!
  114. , customer: output.customer
  115. , machine: output.machine
  116. , createdate: output.createdate
  117. , isactive: output.isactive
  118. , createuserid: output.createuserid
  119. , updatedate: output.updatedate
  120. , updateuserid: output.updateuserid
  121. )
  122. }
  123. }
  124. }
  125. extension XCustomerMachineCtrl: RouteCollection{
  126. func boot(routes: RoutesBuilder) throws{
  127. let apigroup = routes.grouped("xcustomermachines")
  128. apigroup.get("", use: readAll)
  129. apigroup.get(":id", use: read)
  130. apigroup.post("", use: create)
  131. apigroup.post(":id", use: update)
  132. apigroup.delete(":id", use: delete)
  133. }
  134. }