No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.
 
 

151 líneas
4.9 KiB

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