25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

136 lines
4.0 KiB

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