Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.
 
 

124 Zeilen
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) {
  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 decryptedString = try CustomCrypto.DecryptString(input: x)
  81. let array = decryptedString.components(separatedBy: ":")
  82. ///This call is the standard get by id call
  83. if (array.count == 1){
  84. let id = Int(array[0])!
  85. return query.filter(\.$id == id)
  86. }
  87. ///Optional else if if there are multiple parameters in the call
  88. ///You must include an else for each call that has parameters
  89. ///Need to check the endpoint to get the right filter statements
  90. ///else if(x.count == 2){
  91. /// let username = x[0]
  92. /// let password = x[1]
  93. /// return query.filter(\.$username == username).filter(\.$password == password)
  94. ///}
  95. else{
  96. throw Abort(.badRequest)
  97. }
  98. }
  99. else{
  100. return query
  101. }
  102. }
  103. }