Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

InspectionLevelCtrl.swift 6.0 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. //
  2. // InspectionLevelCtrl.swift
  3. //
  4. //
  5. // Created by Michiel Carman on 11/10/21.
  6. //
  7. import Foundation
  8. import Vapor
  9. import Fluent
  10. import FluentPostgresDriver
  11. final class InspectionLevelCtrl{
  12. //Create Unit
  13. func create(req: Request) throws -> EventLoopFuture<mdlInspectionLevel.Output>{
  14. let input = try req.content.decode(mdlInspectionLevel.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<mdlInspectionLevel.Output>{
  20. let input = try req.content.decode(mdlInspectionLevel.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<mdlInspectionLevel.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<mdlInspectionLevel.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 mdlInspectionLevel.find(id, on: req.db).unwrap(or: Abort(.notFound)).flatMap{ $0.delete(on: req.db)}.map{.ok}
  45. }
  46. }
  47. extension InspectionLevelCtrl{
  48. //Query Settings
  49. func qbase(req: Request) throws -> QueryBuilder<mdlInspectionLevel> {
  50. let query = mdlInspectionLevel.query(on: req.db)
  51. .with(\.$inspectiontype)
  52. .with(\.$templates)
  53. .with(\.$inspections)
  54. if let id = req.parameters.get("id", as: Int.self){
  55. return query.filter(\.$id == id)
  56. } else{
  57. return query
  58. }
  59. }
  60. //create Output
  61. func createRead(_ output: mdlInspectionLevel) -> mdlInspectionLevel.Output {
  62. return mdlInspectionLevel.Output(
  63. id: output.id!,
  64. name: output.name,
  65. description: output.description,
  66. isactive: output.isactive,
  67. createdate: output.createdate,
  68. createuserid: output.createuserid,
  69. updatedate: output.updatedate,
  70. updateuserid: output.updateuserid,
  71. inspectiontype: output.$inspectiontype.value!,
  72. templates: output.$templates.value!,
  73. inspections: output.$inspections.value!
  74. )
  75. }
  76. func mapOutput(_ output: Optional<mdlInspectionLevel>.WrappedType, _ input: mdlInspectionLevel.Input){
  77. output.name = input.name;
  78. output.description = input.description;
  79. output.isactive = input.isactive;
  80. output.createdate = input.createdate;
  81. output.updatedate = input.updatedate;
  82. output.createuserid = input.createuserid;
  83. output.updateuserid = input.updateuserid;
  84. output.$inspectiontype.id = input.inspectiontype.id!;
  85. output.$templates.value = input.templates;
  86. output.$inspections.value = input.inspections
  87. }
  88. func createUnit(_ input: mdlInspectionLevel.Input) -> mdlInspectionLevel {
  89. return mdlInspectionLevel(
  90. id: input.id ?? nil
  91. , name: input.name
  92. , description: input.description
  93. , isactive: input.isactive
  94. , createuserid: input.createuserid ?? -1
  95. , createdate: input.createdate
  96. , updateuserid: input.updateuserid ?? -1
  97. , updatedate: input.updatedate
  98. , inspectiontype: input.inspectiontype
  99. , templates: input.templates
  100. , inspections: input.inspections
  101. )
  102. }
  103. //update function
  104. func save(_ output: Optional<mdlInspectionLevel>.WrappedType, req: Request) -> EventLoopFuture<mdlInspectionLevel.Output> {
  105. return output
  106. .save(on: req.db)
  107. .map{mdlInspectionLevel.Output(
  108. id: output.id!
  109. , name: output.name
  110. , description: output.description
  111. , isactive: output.isactive
  112. , createdate: output.createdate
  113. , createuserid: output.createuserid
  114. , updatedate: output.updatedate
  115. , updateuserid: output.updateuserid
  116. , inspectiontype: output.inspectiontype
  117. , templates: output.templates
  118. , inspections: output.inspections
  119. )
  120. }
  121. }
  122. //create function
  123. func save(_ output: mdlInspectionLevel, _ req: Request, _ input: mdlInspectionLevel.Input) -> EventLoopFuture<mdlInspectionLevel.Output> {
  124. return output
  125. .save(on: req.db)
  126. .map{
  127. mdlInspectionLevel.Output(
  128. id: output.id!
  129. , name: output.name
  130. , description: output.description
  131. , isactive: output.isactive
  132. , createdate: output.createdate
  133. , createuserid: output.createuserid
  134. , updatedate: output.updatedate
  135. , updateuserid: output.updateuserid
  136. , inspectiontype: input.inspectiontype
  137. , templates: input.templates
  138. , inspections: input.inspections
  139. )
  140. }
  141. }
  142. }
  143. extension InspectionLevelCtrl: RouteCollection{
  144. func boot(routes: RoutesBuilder) throws{
  145. let apigroup = routes.grouped("inspectionlevels")
  146. apigroup.get("", use: readAll)
  147. apigroup.get(":id", use: read)
  148. apigroup.post("", use: create)
  149. apigroup.post(":id", use: update)
  150. apigroup.delete(":id", use: delete)
  151. }
  152. }