Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.
 
 

161 lignes
5.4 KiB

  1. //
  2. // ProductCtrl.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 ProductCtrl{
  12. //Create Unit
  13. func create(req: Request) throws -> EventLoopFuture<mdlProduct.Output>{
  14. let input = try req.content.decode(mdlProduct.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<mdlProduct.Output>{
  20. let input = try req.content.decode(mdlProduct.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<mdlProduct.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<mdlProduct.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 mdlProduct.find(id, on: req.db).unwrap(or: Abort(.notFound)).flatMap{ $0.delete(on: req.db)}.map{.ok}
  45. }
  46. }
  47. extension ProductCtrl{
  48. //Query Settings
  49. func qbase(req: Request) throws -> QueryBuilder<mdlProduct> {
  50. let query = mdlProduct.query(on: req.db).with(\.$models).with(\.$templates)
  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: mdlProduct) -> mdlProduct.Output {
  59. return mdlProduct.Output(
  60. id: output.id!,
  61. code: output.code,
  62. description: output.description,
  63. isactive: output.isactive,
  64. createdate: output.createdate,
  65. createuserid: output.createuserid,
  66. updatedate: output.updatedate,
  67. updateuserid: output.updateuserid,
  68. models: output.$models.value!,
  69. templates: output.$templates.value!
  70. )
  71. }
  72. func mapOutput(_ output: Optional<mdlProduct>.WrappedType, _ input: mdlProduct.Input){
  73. output.code = input.code;
  74. output.description = input.description;
  75. output.isactive = input.isactive;
  76. output.createdate = input.createdate;
  77. output.updatedate = input.updatedate;
  78. output.createuserid = input.createuserid;
  79. output.updateuserid = input.updateuserid;
  80. output.$models.value = input.models;
  81. output.$templates.value = input.templates;
  82. }
  83. func createUnit(_ input: mdlProduct.Input) -> mdlProduct {
  84. return mdlProduct(
  85. id: input.id ?? nil
  86. , code: input.code
  87. , description: input.description
  88. , isactive: input.isactive
  89. , createuserid: input.createuserid ?? -1
  90. , createdate: input.createdate
  91. , updateuserid: input.updateuserid ?? -1
  92. , updatedate: input.updatedate
  93. , models: input.models
  94. , templates: input.templates
  95. )
  96. }
  97. //update function
  98. func save(_ output: Optional<mdlProduct>.WrappedType, req: Request) -> EventLoopFuture<mdlProduct.Output> {
  99. return output
  100. .save(on: req.db)
  101. .map{mdlProduct.Output(
  102. id: output.id!
  103. , code: output.code
  104. , description: output.description
  105. , isactive: output.isactive
  106. , createdate: output.createdate
  107. , createuserid: output.createuserid
  108. , updatedate: output.updatedate
  109. , updateuserid: output.updateuserid
  110. , models: output.models
  111. , templates: output.templates
  112. )
  113. }
  114. }
  115. //create function
  116. func save(_ output: mdlProduct, _ req: Request, _ input: mdlProduct.Input) -> EventLoopFuture<mdlProduct.Output> {
  117. return output
  118. .save(on: req.db)
  119. .map{
  120. mdlProduct.Output(
  121. id: output.id!
  122. , code: output.code
  123. , description: output.description
  124. , isactive: output.isactive
  125. , createdate: output.createdate
  126. , createuserid: output.createuserid
  127. , updatedate: output.updatedate
  128. , updateuserid: output.updateuserid
  129. , models: input.models
  130. , templates: input.templates
  131. )
  132. }
  133. }
  134. }
  135. extension ProductCtrl: RouteCollection{
  136. func boot(routes: RoutesBuilder) throws{
  137. let apigroup = routes.grouped("products")
  138. apigroup.get("", use: readAll)
  139. apigroup.get(":id", use: read)
  140. apigroup.post("", use: create)
  141. apigroup.post(":id", use: update)
  142. apigroup.delete(":id", use: delete)
  143. }
  144. }