Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 

119 linhas
3.9 KiB

  1. //
  2. // mdlDepartment.swift
  3. //
  4. //
  5. // Created by Michiel Carman on 4/11/2024.
  6. //
  7. import Fluent
  8. import Vapor
  9. import Foundation
  10. import FluentKit
  11. final class mdlDepartment: Model, Content {
  12. static let schema = "department"
  13. @ID(custom: "id", generatedBy: .database) var id: Int?
  14. @Field(key: "name") var name: String?
  15. @OptionalParent(key: "departmentId") var parent: mdlDepartment?
  16. @Children(for: \.$parent) var subdepartments: [mdlDepartment]
  17. @Siblings(through: mdlXAssocDept.self, from: \.$department, to: \.$associate) var associates: [mdlAssociate]
  18. @Siblings(through: mdlXDocDept.self, from: \.$department, to: \.$document) var documents: [mdlDocument]
  19. @Timestamp(key: "createDate", on: .create) var createdate: Date?
  20. @Field(key: "createUserId") var createuserid: Int?
  21. @Timestamp(key: "updateDate", on: .update) var updatedate: Date?
  22. @Field(key: "updateUserId") var updateuserid: Int?
  23. init() { }
  24. init(id: Int? = nil,
  25. name: String? = nil,
  26. parentid: Int? = nil,
  27. createuserid: Int? = nil,
  28. updateuserid: Int? = nil
  29. ) {
  30. self.id = id
  31. self.name = name
  32. if(parentid != nil) { self.$parent.$id.value = parentid }
  33. self.createuserid = createuserid
  34. self.updateuserid = updateuserid
  35. }
  36. }
  37. extension mdlDepartment{
  38. struct Create: AsyncMigration {
  39. func prepare(on database: Database) async throws {
  40. try await database.schema("department")
  41. .field("id", .int, .identifier(auto: true))
  42. .field("name", .string)
  43. .field("departmentId", .int)
  44. .field("createDate", .datetime)
  45. .field("createUserId", .int)
  46. .field("updateDate", .datetime)
  47. .field("updateUserId", .int)
  48. .create()
  49. }
  50. func revert(on database: Database) async throws {
  51. try await database.schema("department").delete()
  52. }
  53. }
  54. struct AddParentReference: AsyncMigration {
  55. func prepare(on database: Database) async throws {
  56. try await database.schema("department")
  57. .deleteField("departmentId")
  58. .field("departmentId", .int, .references("department", "id"))
  59. .update()
  60. }
  61. func revert(on database: Database) async throws {
  62. try await database.schema("department")
  63. .deleteField("departmentId")
  64. .field("departmentId", .int)
  65. .update()
  66. }
  67. }
  68. public static func ObjQuery(req: Request) async throws -> QueryBuilder<mdlDepartment>{
  69. let query = mdlDepartment.query(on: req.db)
  70. .with(\.$subdepartments)
  71. .with(\.$parent)
  72. .with(\.$associates)
  73. .with(\.$documents){
  74. doc in doc
  75. .with(\.$doctype)
  76. .with(\.$parent)
  77. .with(\.$subsequent)
  78. }
  79. if let x = req.parameters.get("x"){
  80. let decryptedString = try CustomCrypto.DecryptString(input: x)
  81. let array = decryptedString.components(separatedBy: ":")
  82. ///This call is the standard get by id call
  83. if (array.count == 1){
  84. let id = Int(array[0])!
  85. return query.filter(\.$id == id)
  86. }
  87. ///Optional else if if there are multiple parameters in the call
  88. ///You must include an else for each call that has parameters
  89. ///Need to check the endpoint to get the right filter statements
  90. ///else if(x.count == 2){
  91. /// let username = x[0]
  92. /// let password = x[1]
  93. /// return query.filter(\.$username == username).filter(\.$password == password)
  94. ///}
  95. else{
  96. throw Abort(.badRequest)
  97. }
  98. }
  99. else{
  100. return query
  101. }
  102. }
  103. }