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

3 лет назад
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. //
  2. // MachineCtrl.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 MachineCtrl{
  12. //Create Unit
  13. func create(req: Request) throws -> EventLoopFuture<mdlMachine.Output>{
  14. let input = try req.content.decode(mdlMachine.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<mdlMachine.Output>{
  20. let input = try req.content.decode(mdlMachine.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<mdlMachine.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<mdlMachine.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 mdlMachine.find(id, on: req.db).unwrap(or: Abort(.notFound)).flatMap{ $0.delete(on: req.db)}.map{.ok}
  45. }
  46. }
  47. extension MachineCtrl{
  48. //Query Settings
  49. func qbase(req: Request) throws -> QueryBuilder<mdlMachine> {
  50. let query = mdlMachine.query(on: req.db)
  51. .with(\.$machinetype)
  52. .with(\.$parent)
  53. .with(\.$model)
  54. .with(\.$machinenumbers)
  55. .with(\.$machinemeters)
  56. .with(\.$customers)
  57. .with(\.$children)
  58. .with(\.$templates)
  59. .with(\.$workorders)
  60. if let id = req.parameters.get("id", as: Int.self){
  61. return query.filter(\.$id == id)
  62. } else{
  63. return query
  64. }
  65. }
  66. //create Output
  67. func createRead(_ output: mdlMachine) -> mdlMachine.Output {
  68. return mdlMachine.Output(
  69. id: output.id!,
  70. manufactureryear: output.manufactureryear,
  71. description: output.description,
  72. currentlocation: output.currentlocation,
  73. lastservicedate: output.lastservicedate,
  74. isactive: output.isactive,
  75. voltage: output.voltage,
  76. kw: output.kw,
  77. phase: output.phase,
  78. inservicedate: output.inservicedate,
  79. createdate: output.createdate,
  80. createuserid: output.createuserid,
  81. updatedate: output.updatedate,
  82. updateuserid: output.updateuserid,
  83. machinetype: output.$machinetype.value!,
  84. parent: output.$parent.value!,
  85. model: output.$model.value!,
  86. machinenumbers: output.$machinenumbers.value!,
  87. machinemeters: output.$machinemeters.value!,
  88. customers: output.$customers.value!,
  89. children: output.$children.value!,
  90. templates: output.$templates.value!,
  91. workorders: output.$workorders.value!
  92. )
  93. }
  94. func mapOutput(_ output: Optional<mdlMachine>.WrappedType, _ input: mdlMachine.Input){
  95. output.manufactureryear = input.manufactureryear;
  96. output.description = input.description;
  97. output.currentlocation = input.currentlocation;
  98. output.lastservicedate = input.lastservicedate;
  99. output.isactive = input.isactive;
  100. output.voltage = input.voltage;
  101. output.kw = input.kw;
  102. output.phase = input.phase;
  103. output.inservicedate = input.inservicedate;
  104. output.createdate = input.createdate;
  105. output.updatedate = input.updatedate;
  106. output.createuserid = input.createuserid;
  107. output.updateuserid = input.updateuserid;
  108. output.$machinetype.id = input.machinetype.id!;
  109. output.$parent.id = input.parent.id!;
  110. output.$model.id = input.model.id!;
  111. output.$machinenumbers.value = input.machinenumbers;
  112. output.$machinemeters.value = input.machinemeters;
  113. output.$customers.value = input.customers;
  114. output.$children.value = input.children;
  115. output.$templates.value = input.templates;
  116. output.$workorders.value = input.workorders
  117. }
  118. func createUnit(_ input: mdlMachine.Input) -> mdlMachine {
  119. return mdlMachine(
  120. id: input.id ?? nil
  121. , manufactureryear: input.manufactureryear
  122. , description: input.description
  123. , currentlocation: input.currentlocation
  124. , lastservicedate: input.lastservicedate
  125. , isactive: input.isactive
  126. , voltage: input.voltage
  127. , kw: input.kw
  128. , phase: input.phase
  129. , inservicedate: input.inservicedate
  130. , createuserid: input.createuserid ?? -1
  131. , createdate: input.createdate
  132. , updateuserid: input.updateuserid ?? -1
  133. , updatedate: input.updatedate
  134. , machinetype: input.machinetype
  135. , parent: input.parent
  136. , model: input.model
  137. , machinenumbers: input.machinenumbers
  138. , machinemeters: input.machinemeters
  139. , customers: input.customers
  140. , children: input.children
  141. , templates: input.templates
  142. , workorders: input.workorders
  143. )
  144. }
  145. //update function
  146. func save(_ output: Optional<mdlMachine>.WrappedType, req: Request) -> EventLoopFuture<mdlMachine.Output> {
  147. return output
  148. .save(on: req.db)
  149. .map{mdlMachine.Output(
  150. id: output.id!
  151. , manufactureryear: output.manufactureryear
  152. , description: output.description
  153. , currentlocation: output.currentlocation
  154. , lastservicedate: output.lastservicedate
  155. , isactive: output.isactive
  156. , voltage: output.voltage
  157. , kw: output.kw
  158. , phase: output.phase
  159. , inservicedate: output.inservicedate
  160. , createdate: output.createdate
  161. , createuserid: output.createuserid
  162. , updatedate: output.updatedate
  163. , updateuserid: output.updateuserid
  164. , machinetype: output.machinetype
  165. , parent: output.parent
  166. , model: output.model
  167. , machinenumbers: output.machinenumbers
  168. , machinemeters: output.machinemeters
  169. , customers: output.customers
  170. , children: output.children
  171. , templates: output.templates
  172. , workorders: output.workorders
  173. )
  174. }
  175. }
  176. //create function
  177. func save(_ output: mdlMachine, _ req: Request, _ input: mdlMachine.Input) -> EventLoopFuture<mdlMachine.Output> {
  178. return output
  179. .save(on: req.db)
  180. .map{
  181. mdlMachine.Output(
  182. id: output.id!
  183. , manufactureryear: output.manufactureryear
  184. , description: output.description
  185. , currentlocation: output.currentlocation
  186. , lastservicedate: output.lastservicedate
  187. , isactive: output.isactive
  188. , voltage: output.voltage
  189. , kw: output.kw
  190. , phase: output.phase
  191. , inservicedate: output.inservicedate
  192. , createdate: output.createdate
  193. , createuserid: output.createuserid
  194. , updatedate: output.updatedate
  195. , updateuserid: output.updateuserid
  196. , machinetype: input.machinetype
  197. , parent: input.parent
  198. , model: input.model
  199. , machinenumbers: input.machinenumbers
  200. , machinemeters: input.machinemeters
  201. , customers: input.customers
  202. , children: input.children
  203. , templates: input.templates
  204. , workorders: input.workorders
  205. )
  206. }
  207. }
  208. }
  209. extension MachineCtrl: RouteCollection{
  210. func boot(routes: RoutesBuilder) throws{
  211. let apigroup = routes.grouped("machines")
  212. apigroup.get("", use: readAll)
  213. apigroup.get(":id", use: read)
  214. apigroup.post("", use: create)
  215. apigroup.post(":id", use: update)
  216. apigroup.delete(":id", use: delete)
  217. }
  218. }