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.
 
 
 

93 wiersze
2.9 KiB

  1. //
  2. // mdlXDocDept.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 mdlXDocDept: Model, Content {
  12. static let schema = "xDocDept"
  13. @ID(custom: "id", generatedBy: .database) var id: Int?
  14. @Parent(key: "documentId") var document: mdlDocument
  15. @Parent(key: "departmentId") var department: mdlDepartment
  16. @Timestamp(key: "createDate", on: .create) var createdate: Date?
  17. @Field(key: "createUserId") var createuserid: Int?
  18. @Timestamp(key: "updateDate", on: .update) var updatedate: Date?
  19. @Field(key: "updateUserId") var updateuserid: Int?
  20. init() { }
  21. init(id: Int? = nil,
  22. documentid: Int,
  23. departmentid: Int,
  24. createuserid: Int? = nil,
  25. updateuserid: Int? = nil
  26. ) {
  27. self.id = id
  28. self.$document.$id.value = documentid
  29. self.$department.$id.value = departmentid
  30. self.createuserid = createuserid
  31. self.updateuserid = updateuserid
  32. }
  33. }
  34. extension mdlXDocDept{
  35. struct Create: AsyncMigration {
  36. func prepare(on database: Database) async throws {
  37. try await database.schema("xDocDept")
  38. .field("id", .int, .identifier(auto: true))
  39. .field("documentId", .int, .references("document", "id"))
  40. .field("departmentId", .int, .references("department", "id"))
  41. .field("createDate", .datetime)
  42. .field("createUserId", .int)
  43. .field("updateDate", .datetime)
  44. .field("updateUserId", .int)
  45. .create()
  46. }
  47. func revert(on database: Database) async throws {
  48. try await database.schema("xDocDept").delete()
  49. }
  50. }
  51. public static func ObjQuery(req: Request) async throws -> QueryBuilder<mdlXDocDept>{
  52. let query = mdlXDocDept.query(on: req.db)
  53. .with(\.$document)
  54. .with(\.$department)
  55. if let x = req.parameters.get("x"){
  56. let decryptedString = try CustomCrypto.DecryptString(input: x)
  57. let array = decryptedString.components(separatedBy: ":")
  58. ///This call is the standard get by id call
  59. if (array.count == 1){
  60. let id = Int(array[0])!
  61. return query.filter(\.$id == id)
  62. }
  63. ///Optional else if if there are multiple parameters in the call
  64. ///You must include an else for each call that has parameters
  65. ///Need to check the endpoint to get the right filter statements
  66. ///else if(x.count == 2){
  67. /// let username = x[0]
  68. /// let password = x[1]
  69. /// return query.filter(\.$username == username).filter(\.$password == password)
  70. ///}
  71. else{
  72. throw Abort(.badRequest)
  73. }
  74. }
  75. else{
  76. return query
  77. }
  78. }
  79. }