Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.
 
 

191 wiersze
6.4 KiB

  1. //
  2. // OilSampleLabelCtrl.swift
  3. //
  4. //
  5. // Created by Michiel Carman on 11/11/21.
  6. //
  7. import Foundation
  8. import Vapor
  9. import Fluent
  10. import FluentPostgresDriver
  11. final class OilSampleLabelCtrl{
  12. //Create Unit
  13. func create(req: Request) throws -> EventLoopFuture<mdlOilSampleLabel.Output>{
  14. let input = try req.content.decode(mdlOilSampleLabel.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<mdlOilSampleLabel.Output>{
  20. let input = try req.content.decode(mdlOilSampleLabel.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<mdlOilSampleLabel.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<mdlOilSampleLabel.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 mdlOilSampleLabel.find(id, on: req.db).unwrap(or: Abort(.notFound)).flatMap{ $0.delete(on: req.db)}.map{.ok}
  45. }
  46. }
  47. extension OilSampleLabelCtrl{
  48. //Query Settings
  49. func qbase(req: Request) throws -> QueryBuilder<mdlOilSampleLabel> {
  50. let query = mdlOilSampleLabel.query(on: req.db).with(\.$labsetting)
  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. /*
  58. ,name: String
  59. ,description: String
  60. ,size: String
  61. ,offset: String
  62. ,hrez: String
  63. ,vrez: String
  64. ,height: String
  65. ,quantity: Int
  66. */
  67. //create Output
  68. func createRead(_ output: mdlOilSampleLabel) -> mdlOilSampleLabel.Output {
  69. return mdlOilSampleLabel.Output(
  70. id: output.id!,
  71. name: output.name,
  72. description: output.description,
  73. size: output.size,
  74. offset: output.offset,
  75. hrez: output.hrez,
  76. vrez: output.vrez,
  77. height: output.height,
  78. quantity: output.quantity,
  79. createdate: output.createdate,
  80. createuserid: output.createuserid,
  81. updatedate: output.updatedate,
  82. updateuserid: output.updateuserid,
  83. labsetting: output.$labsetting.value!
  84. )
  85. }
  86. func mapOutput(_ output: Optional<mdlOilSampleLabel>.WrappedType, _ input: mdlOilSampleLabel.Input){
  87. output.name = input.name;
  88. output.description = input.description;
  89. output.size = input.size;
  90. output.offset = input.offset;
  91. output.hrez = input.hrez;
  92. output.vrez = input.vrez;
  93. output.height = input.height;
  94. output.quantity = input.quantity;
  95. output.createdate = input.createdate;
  96. output.updatedate = input.updatedate;
  97. output.createuserid = input.createuserid;
  98. output.updateuserid = input.updateuserid;
  99. output.$labsetting.id = input.labsetting.id!
  100. }
  101. func createUnit(_ input: mdlOilSampleLabel.Input) -> mdlOilSampleLabel {
  102. return mdlOilSampleLabel(
  103. id: input.id ?? nil
  104. , name: input.name
  105. , description: input.description
  106. , size: input.size
  107. , offset: input.offset
  108. , hrez: input.hrez
  109. , vrez: input.vrez
  110. , height: input.height
  111. , quantity: input.quantity
  112. , createdate: input.createdate
  113. , createuserid: input.createuserid ?? -1
  114. , updatedate: input.updatedate
  115. , updateuserid: input.updateuserid ?? -1
  116. , labsetting: input.labsetting
  117. )
  118. }
  119. //update function
  120. func save(_ output: Optional<mdlOilSampleLabel>.WrappedType, req: Request) -> EventLoopFuture<mdlOilSampleLabel.Output> {
  121. return output
  122. .save(on: req.db)
  123. .map{mdlOilSampleLabel.Output(
  124. id: output.id!
  125. , name: output.name
  126. , description: output.description
  127. , size: output.size
  128. , offset: output.offset
  129. , hrez: output.hrez
  130. , vrez: output.vrez
  131. , height: output.height
  132. , quantity: output.quantity
  133. , createdate: output.createdate
  134. , createuserid: output.createuserid
  135. , updatedate: output.updatedate
  136. , updateuserid: output.updateuserid
  137. , labsetting: output.labsetting
  138. )
  139. }
  140. }
  141. //create function
  142. func save(_ output: mdlOilSampleLabel, _ req: Request, _ input: mdlOilSampleLabel.Input) -> EventLoopFuture<mdlOilSampleLabel.Output> {
  143. return output
  144. .save(on: req.db)
  145. .map{
  146. mdlOilSampleLabel.Output(
  147. id: output.id!
  148. , name: output.name
  149. , description: output.description
  150. , size: output.size
  151. , offset: output.offset
  152. , hrez: output.hrez
  153. , vrez: output.vrez
  154. , height: output.height
  155. , quantity: output.quantity
  156. , createdate: output.createdate
  157. , createuserid: output.createuserid
  158. , updatedate: output.updatedate
  159. , updateuserid: output.updateuserid
  160. , labsetting: input.labsetting
  161. )
  162. }
  163. }
  164. }
  165. extension OilSampleLabelCtrl: RouteCollection{
  166. func boot(routes: RoutesBuilder) throws{
  167. let apigroup = routes.grouped("oilsamplelabels")
  168. apigroup.get("", use: readAll)
  169. apigroup.get(":id", use: read)
  170. apigroup.post("", use: create)
  171. apigroup.post(":id", use: update)
  172. apigroup.delete(":id", use: delete)
  173. }
  174. }