Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.
 
 
 

121 wiersze
4.0 KiB

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