You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

126 lines
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. @Children(for: \.$contact) var info: [mdlContactInfo]
  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, firstname: String? = nil, lastname: String? = nil, isactive: Bool? = nil, createuserid: Int? = nil, updateuserid: Int? = nil, customerid: Int) {
  26. self.id = id
  27. self.firstname = firstname
  28. self.lastname = lastname
  29. self.isactive = isactive
  30. self.$customer.$id.value = customerid
  31. self.createuserid = createuserid
  32. self.updateuserid = updateuserid
  33. }
  34. }
  35. extension mdlContact{
  36. struct Create: AsyncMigration {
  37. func prepare(on database: Database) async throws {
  38. try await database.schema("contact")
  39. .field("id", .int, .identifier(auto: true))
  40. .field("firstName", .string)
  41. .field("lastName", .string)
  42. .field("isActive", .bool)
  43. .field("createDate", .datetime)
  44. .field("createUserId", .int)
  45. .field("updateDate", .datetime)
  46. .field("updateUserId", .int)
  47. .create()
  48. }
  49. func revert(on database: Database) async throws {
  50. try await database.schema("contact").delete()
  51. }
  52. }
  53. struct makeParentCustomer: AsyncMigration{
  54. func prepare(on database: Database) async throws {
  55. try await database.schema("contact")
  56. .field("customerId", .int, .references("customer", "id"))
  57. .update()
  58. }
  59. func revert(on database: Database) async throws {
  60. try await database.schema("contact")
  61. .deleteField("customerId" )
  62. .update()
  63. }
  64. }
  65. struct Mod1: AsyncMigration{
  66. func prepare(on database: Database) async throws {
  67. try await database.schema("contact")
  68. .field("description", .string)
  69. .update()
  70. }
  71. func revert(on database: Database) async throws {
  72. try await database.schema("contact")
  73. .deleteField("description" )
  74. .update()
  75. }
  76. }
  77. public static func ObjQuery(req: Request) async throws -> QueryBuilder<mdlContact>{
  78. let query = mdlContact.query(on: req.db)
  79. .with(\.$customer)
  80. if let x = req.parameters.get("x"){
  81. let decryptedString = try CustomCrypto.DecryptString(input: x)
  82. let array = decryptedString.components(separatedBy: ":")
  83. ///This call is the standard get by id call
  84. if (array.count == 1){
  85. let id = Int(array[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. }