Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

125 строки
4.0 KiB

  1. //
  2. // mdlContact.swift
  3. //
  4. //
  5. // Created by Michiel Carman on 1/24/24.
  6. //
  7. import Fluent
  8. import Vapor
  9. import Foundation
  10. import FluentKit
  11. final class mdlContact: Model, Content {
  12. static let schema = "contact"
  13. @ID(custom: "id", generatedBy: .database) var id: Int?
  14. @Field(key: "firstName") var firstname: String?
  15. @Field(key: "lastName") var lastname: String?
  16. @Field(key: "description") var description: String?
  17. @Field(key: "isActive") var isactive: Bool?
  18. @Parent(key: "customerId") var customer: mdlCustomer
  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, firstname: String? = nil, lastname: String? = nil, isactive: Bool? = nil, createuserid: Int? = nil, updateuserid: Int? = nil, customerid: Int? = nil) {
  25. self.id = id
  26. self.firstname = firstname
  27. self.lastname = lastname
  28. self.isactive = isactive
  29. self.$customer.$id.value = customerid!
  30. self.createuserid = createuserid
  31. self.updateuserid = updateuserid
  32. }
  33. }
  34. extension mdlContact{
  35. struct Create: AsyncMigration {
  36. func prepare(on database: Database) async throws {
  37. try await database.schema("contact")
  38. .field("id", .int, .identifier(auto: true))
  39. .field("firstName", .string)
  40. .field("lastName", .string)
  41. .field("isActive", .bool)
  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("contact").delete()
  50. }
  51. }
  52. struct makeParentCustomer: AsyncMigration{
  53. func prepare(on database: Database) async throws {
  54. try await database.schema("contact")
  55. .field("customerId", .int, .references("customer", "id"))
  56. .update()
  57. }
  58. func revert(on database: Database) async throws {
  59. try await database.schema("contact")
  60. .deleteField("customerId" )
  61. .update()
  62. }
  63. }
  64. struct Mod1: AsyncMigration{
  65. func prepare(on database: Database) async throws {
  66. try await database.schema("contact")
  67. .field("description", .string)
  68. .update()
  69. }
  70. func revert(on database: Database) async throws {
  71. try await database.schema("contact")
  72. .deleteField("description" )
  73. .update()
  74. }
  75. }
  76. public static func ObjQuery(req: Request) async throws -> QueryBuilder<mdlContact>{
  77. let query = mdlContact.query(on: req.db)
  78. .with(\.$customer)
  79. if let x = req.parameters.get("x"){
  80. let dataQuery = req.parameters.get("x")
  81. let decryptedString = try CustomCrypto.DecryptString(input: dataQuery!)
  82. let x = decryptedString.components(separatedBy: ":")
  83. ///This call is the standard get by id call
  84. if (x.count == 1){
  85. let id = Int(x[0])!
  86. return query.filter(\.$id == id)
  87. }
  88. ///Optional else if if there are multiple parameters in the call
  89. ///You must include an else for each call that has parameters
  90. ///Need to check the endpoint to get the right filter statements
  91. ///else if(x.count == 2){
  92. /// let username = x[0]
  93. /// let password = x[1]
  94. /// return query.filter(\.$username == username).filter(\.$password == password)
  95. ///}
  96. else{
  97. throw Abort(.badRequest)
  98. }
  99. }
  100. else{
  101. return query
  102. }
  103. }
  104. }