Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.
 
 

156 Zeilen
5.5 KiB

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