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.
 
 
 

111 wiersze
3.4 KiB

  1. //
  2. // mdlUser.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. import JWT
  12. final class mdlAssociate: Model, Content {
  13. static let schema = "associate"
  14. @ID(custom: "id", generatedBy: .database) var id: Int?
  15. @Field(key: "firstName") var firstname: String?
  16. @Field(key: "lastName") var lastname: String?
  17. @Siblings(through: mdlXAssocDept.self, from: \.$associate, to: \.$department) var departments: [mdlDepartment]
  18. @Timestamp(key: "createDate", on: .create) var createdate: Date?
  19. @Field(key: "createUserId") var createuserid: Int?
  20. @Timestamp(key: "updateDate", on: .update) var updatedate: Date?
  21. @Field(key: "updateUserId") var updateuserid: Int?
  22. init() { }
  23. init(id: Int? = nil,
  24. firstname: String? = nil,
  25. lastname: String? = nil,
  26. createuserid: Int? = nil,
  27. updateuserid: Int? = nil
  28. ) {
  29. self.id = id
  30. self.firstname = firstname
  31. self.lastname = lastname
  32. self.createuserid = createuserid
  33. self.updateuserid = updateuserid
  34. }
  35. }
  36. extension mdlAssociate{
  37. struct User: JWTPayload{
  38. var id: Int
  39. var email: String
  40. var password: String
  41. func verify(using signer: JWTSigner) throws{
  42. }
  43. }
  44. struct Create: AsyncMigration {
  45. func prepare(on database: Database) async throws {
  46. try await database.schema("associate")
  47. .field("id", .int, .identifier(auto: true))
  48. .field("firstName", .string)
  49. .field("lastName", .string)
  50. .field("createDate", .datetime)
  51. .field("createUserId", .int)
  52. .field("updateDate", .datetime)
  53. .field("updateUserId", .int)
  54. .create()
  55. }
  56. func revert(on database: Database) async throws {
  57. try await database.schema("associate").delete()
  58. }
  59. }
  60. public static func ObjQuery(req: Request) async throws -> QueryBuilder<mdlAssociate>{
  61. let query = mdlAssociate.query(on: req.db)
  62. .with(\.$departments){
  63. dept in dept.with(\.$documents){
  64. doc in doc
  65. .with(\.$doctype)
  66. .with(\.$parent)
  67. .with(\.$subsequent)
  68. }
  69. }
  70. if let x = req.parameters.get("x"){
  71. let decryptedString = try CustomCrypto.DecryptString(input: x)
  72. let array = decryptedString.components(separatedBy: ":")
  73. ///This call is the standard get by id call
  74. if (array.count == 1){
  75. let id = Int(array[0])!
  76. return query.filter(\.$id == id)
  77. }
  78. ///Optional else if if there are multiple parameters in the call
  79. ///You must include an else for each call that has parameters
  80. ///Need to check the endpoint to get the right filter statements
  81. ///else if(x.count == 2){
  82. /// let username = x[0]
  83. /// let password = x[1]
  84. /// return query.filter(\.$username == username).filter(\.$password == password)
  85. ///}
  86. else{
  87. throw Abort(.badRequest)
  88. }
  89. }
  90. else{
  91. return query
  92. }
  93. }
  94. }