Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.
 
 

114 řádky
3.8 KiB

  1. //
  2. // mdlContactInfo.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 mdlContactInfo: Model, Content {
  12. static let schema = "contactInfo"
  13. @ID(custom: "id", generatedBy: .database) var id: Int?
  14. @Field(key: "label") var label: String?
  15. @Field(key: "value") var value: String?
  16. @Field(key: "isActive") var isactive: Bool?
  17. @Parent(key: "contactTypeId") var contacttype: mdlContactType
  18. @OptionalParent(key: "contact") var contact: mdlContact?
  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, label: String? = nil, value: String? = nil, isactive: Bool? = nil, createuserid: Int? = nil, updateuserid: Int? = nil, contacttypeid: Int, contactid: Int? = nil) {
  25. self.id = id
  26. self.label = label
  27. self.value = value
  28. self.isactive = isactive
  29. self.$contacttype.$id.value = contacttypeid
  30. if(contactid != nil) { self.$contact.$id.value = contactid }
  31. self.createuserid = createuserid
  32. self.updateuserid = updateuserid
  33. }
  34. }
  35. extension mdlContactInfo{
  36. struct Create: AsyncMigration {
  37. func prepare(on database: Database) async throws {
  38. try await database.schema("contactInfo")
  39. .field("id", .int, .identifier(auto: true))
  40. .field("value", .string)
  41. .field("isActive", .bool)
  42. .field("contactTypeId", .int, .references("contactType", "id"))
  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("contactInfo").delete()
  51. }
  52. }
  53. struct Mod1: AsyncMigration{
  54. func prepare(on database: Database) async throws {
  55. try await database.schema("contactInfo")
  56. .field("label", .string)
  57. .field("contactId", .int, .references("contact", "id"))
  58. .update()
  59. }
  60. func revert(on database: Database) async throws {
  61. try await database.schema("contactInfo")
  62. .deleteField("label")
  63. .deleteField("contactId")
  64. .update()
  65. }
  66. }
  67. public static func ObjQuery(req: Request) async throws -> QueryBuilder<mdlContactInfo>{
  68. let query = mdlContactInfo.query(on: req.db)
  69. .with(\.$contacttype)
  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. }