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

190 строки
6.7 KiB

  1. //
  2. // ItemCtrl.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 ItemCtrl{
  12. //Create Unit
  13. func create(req: Request) throws -> EventLoopFuture<mdlItem.Output>{
  14. let input = try req.content.decode(mdlItem.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<mdlItem.Output>{
  20. let input = try req.content.decode(mdlItem.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<mdlItem.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<mdlItem.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 mdlItem.find(id, on: req.db).unwrap(or: Abort(.notFound)).flatMap{ $0.delete(on: req.db)}.map{.ok}
  45. }
  46. }
  47. extension ItemCtrl{
  48. //Query Settings
  49. func qbase(req: Request) throws -> QueryBuilder<mdlItem> {
  50. let query = mdlItem.query(on: req.db)
  51. .with(\.$category)
  52. .with(\.$code)
  53. .with(\.$inputtype)
  54. .with(\.$specs)
  55. .with(\.$responsetypes)
  56. .with(\.$templatecategoryitems)
  57. .with(\.$inspectioncategoryitems)
  58. if let id = req.parameters.get("id", as: Int.self){
  59. return query.filter(\.$id == id)
  60. } else{
  61. return query
  62. }
  63. }
  64. //create Output
  65. func createRead(_ output: mdlItem) -> mdlItem.Output {
  66. return mdlItem.Output(
  67. id: output.id!,
  68. name: output.name,
  69. description: output.description,
  70. isactive: output.isactive,
  71. createdate: output.createdate,
  72. createuserid: output.createuserid,
  73. updatedate: output.updatedate,
  74. updateuserid: output.updateuserid,
  75. category: output.$category.value!,
  76. code: output.$code.value!,
  77. inputtype: output.$inputtype.value!,
  78. responsetypes: output.$responsetypes.value!
  79. , specs: output.$specs.value!,
  80. templatecategoryitems: output.$templatecategoryitems.value!,
  81. inspectioncategoryitems: output.$inspectioncategoryitems.value!
  82. )
  83. }
  84. func mapOutput(_ output: Optional<mdlItem>.WrappedType, _ input: mdlItem.Input){
  85. output.name = input.name;
  86. output.description = input.description;
  87. output.isactive = input.isactive;
  88. output.createdate = input.createdate;
  89. output.updatedate = input.updatedate;
  90. output.createuserid = input.createuserid;
  91. output.updateuserid = input.updateuserid;
  92. output.$category.id = input.category.id!;
  93. output.$code.id = input.code.id!;
  94. output.$inputtype.id = input.inputtype.id!;
  95. output.$specs.value = input.specs;
  96. output.$templatecategoryitems.value = input.templatecategoryitems;
  97. output.$inspectioncategoryitems.value = input.inspectioncategoryitems;
  98. output.$responsetypes.value = input.responsetypes;
  99. }
  100. func createUnit(_ input: mdlItem.Input) -> mdlItem {
  101. return mdlItem(
  102. id: input.id ?? nil
  103. , name: input.name
  104. , description: input.description
  105. , isactive: input.isactive
  106. , createuserid: input.createuserid ?? -1
  107. , createdate: input.createdate
  108. , updateuserid: input.updateuserid ?? -1
  109. , updatedate: input.updatedate
  110. , responsetypes: input.responsetypes
  111. , specs: input.specs
  112. , templatecategoryitems: input.templatecategoryitems
  113. , inspectioncategoryitems: input.inspectioncategoryitems
  114. )
  115. }
  116. //update function
  117. func save(_ output: Optional<mdlItem>.WrappedType, req: Request) -> EventLoopFuture<mdlItem.Output> {
  118. return output
  119. .save(on: req.db)
  120. .map{mdlItem.Output(
  121. id: output.id!
  122. , name: output.name
  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. , category: output.category
  130. , code: output.code
  131. , inputtype: output.inputtype
  132. , responsetypes: output.responsetypes
  133. , specs: output.specs
  134. , templatecategoryitems: output.templatecategoryitems
  135. , inspectioncategoryitems: output.inspectioncategoryitems
  136. )
  137. }
  138. }
  139. //create function
  140. func save(_ output: mdlItem, _ req: Request, _ input: mdlItem.Input) -> EventLoopFuture<mdlItem.Output> {
  141. return output
  142. .save(on: req.db)
  143. .map{
  144. mdlItem.Output(
  145. id: output.id!
  146. , name: output.name
  147. , description: output.description
  148. , isactive: output.isactive
  149. , createdate: output.createdate
  150. , createuserid: output.createuserid
  151. , updatedate: output.updatedate
  152. , updateuserid: output.updateuserid
  153. , category: input.category
  154. , code: input.code
  155. , inputtype: input.inputtype
  156. , responsetypes: input.responsetypes
  157. , specs: input.specs
  158. , templatecategoryitems: input.templatecategoryitems
  159. , inspectioncategoryitems: input.inspectioncategoryitems
  160. )
  161. }
  162. }
  163. }
  164. extension ItemCtrl: RouteCollection{
  165. func boot(routes: RoutesBuilder) throws{
  166. let apigroup = routes.grouped("items")
  167. apigroup.get("", use: readAll)
  168. apigroup.get(":id", use: read)
  169. apigroup.post("", use: create)
  170. apigroup.post(":id", use: update)
  171. apigroup.delete(":id", use: delete)
  172. }
  173. }