Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.
 
 

157 řádky
5.2 KiB

  1. //
  2. // NoteCtrl.swift
  3. //
  4. //
  5. // Created by Michiel Carman on 11/12/21.
  6. //
  7. import Foundation
  8. import Vapor
  9. import Fluent
  10. import FluentPostgresDriver
  11. final class NoteCtrl{
  12. //Create Unit
  13. func create(req: Request) throws -> EventLoopFuture<mdlNote.Output>{
  14. let input = try req.content.decode(mdlNote.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<mdlNote.Output>{
  20. let input = try req.content.decode(mdlNote.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<mdlNote.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<mdlNote.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 mdlNote.find(id, on: req.db).unwrap(or: Abort(.notFound)).flatMap{ $0.delete(on: req.db)}.map{.ok}
  45. }
  46. }
  47. extension NoteCtrl{
  48. //Query Settings
  49. func qbase(req: Request) throws -> QueryBuilder<mdlNote> {
  50. let query = mdlNote.query(on: req.db).with(\.$notetype).with(\.$workorder).with(\.$inspectioncategoryitem)
  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: mdlNote) -> mdlNote.Output {
  59. return mdlNote.Output(
  60. id: output.id!
  61. , text: output.text
  62. , createdate: output.createdate
  63. , createuserid: output.createuserid
  64. , updatedate: output.updatedate
  65. , updateuserid: output.updateuserid
  66. , workorder: output.$workorder.value!
  67. , inspectioncategoryitem: output.$inspectioncategoryitem.value!
  68. , notetype: output.$notetype.value!
  69. )
  70. }
  71. func mapOutput(_ output: Optional<mdlNote>.WrappedType, _ input: mdlNote.Input){
  72. output.text = input.text;
  73. output.$workorder.id = input.workorder.id!;
  74. output.$inspectioncategoryitem.id = input.inspectioncategoryitem.id!;
  75. output.$notetype.id = input.notetype.id!;
  76. output.createdate = input.createdate;
  77. output.updatedate = input.updatedate;
  78. output.createuserid = input.createuserid;
  79. output.updateuserid = input.updateuserid
  80. }
  81. func createUnit(_ input: mdlNote.Input) -> mdlNote {
  82. return mdlNote(
  83. id: input.id ?? nil
  84. , text: input.text
  85. , createuserid: input.createuserid ?? -1
  86. , createdate: input.createdate
  87. , updateuserid: input.updateuserid ?? -1
  88. , updatedate: input.updatedate
  89. , workorder: input.workorder
  90. , inspectioncategoryitem: input.inspectioncategoryitem
  91. , notetype: input.notetype
  92. )
  93. }
  94. //update function
  95. func save(_ output: Optional<mdlNote>.WrappedType, req: Request) -> EventLoopFuture<mdlNote.Output> {
  96. return output
  97. .save(on: req.db)
  98. .map{mdlNote.Output(
  99. id: output.id!
  100. , text: output.text
  101. , createdate: output.createdate
  102. , createuserid: output.createuserid
  103. , updatedate: output.updatedate
  104. , updateuserid: output.updateuserid
  105. , workorder: output.workorder
  106. , inspectioncategoryitem: output.inspectioncategoryitem
  107. , notetype: output.notetype
  108. )
  109. }
  110. }
  111. //create function
  112. func save(_ output: mdlNote, _ req: Request, _ input: mdlNote.Input) -> EventLoopFuture<mdlNote.Output> {
  113. return output
  114. .save(on: req.db)
  115. .map{
  116. mdlNote.Output(
  117. id: output.id!
  118. , text: output.text
  119. , createdate: output.createdate
  120. , createuserid: output.createuserid
  121. , updatedate: output.updatedate
  122. , updateuserid: output.updateuserid
  123. , workorder: input.workorder
  124. , inspectioncategoryitem: input.inspectioncategoryitem
  125. , notetype: input.notetype
  126. )
  127. }
  128. }
  129. }
  130. extension NoteCtrl: RouteCollection{
  131. func boot(routes: RoutesBuilder) throws{
  132. let apigroup = routes.grouped("notes")
  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. }