選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。
 
 
 

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