Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.

mdlLocation.swift 4.9 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. //
  2. // mdlLocation.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 mdlLocation: Model, Content {
  12. static let schema = "location"
  13. @ID(custom: "id", generatedBy: .database) var id: Int?
  14. @Field(key: "name") var name: String?
  15. @Field(key: "address") var address: String?
  16. @Field(key: "address2") var address2: String?
  17. @Field(key: "city") var city: String?
  18. @Field(key: "postalCode") var postalcode: String?
  19. @Field(key: "dealerCode") var dealercode: String?
  20. @Field(key: "fileLocation") var filelocation: String?
  21. @Field(key: "isActive") var isactive: Bool?
  22. @Parent(key: "stateId") var state: mdlState
  23. @OptionalParent(key: "unitId") var unit: mdlUnit?
  24. // @OptionalParent(key: "labSettingId") var labsetting: mdlLabSetting?
  25. @Children(for: \.$location) var users: [mdlUser]
  26. @OptionalChild(for: \.$location) var labsetting: mdlLabSetting?
  27. @Timestamp(key: "createDate", on: .create) var createdate: Date?
  28. @Field(key: "createUserId") var createuserid: Int?
  29. @Timestamp(key: "updateDate", on: .update) var updatedate: Date?
  30. @Field(key: "updateUserId") var updateuserid: Int?
  31. init() { }
  32. init(id: Int? = nil,
  33. name: String? = nil,
  34. address: String? = nil,
  35. address2: String? = nil,
  36. city: String? = nil,
  37. postalcode: String? = nil,
  38. dealercode: String? = nil,
  39. filelocation: String? = nil,
  40. isactive: Bool? = nil,
  41. createuserid: Int? = nil,
  42. updateuserid: Int? = nil,
  43. stateid: Int? = nil,
  44. unitid: Int? = nil
  45. ) {
  46. self.id = id
  47. self.name = name
  48. self.address = address
  49. self.address2 = address2
  50. self.city = city
  51. self.postalcode = postalcode
  52. self.dealercode = dealercode
  53. self.filelocation = filelocation
  54. self.isactive = isactive
  55. self.createuserid = createuserid
  56. self.updateuserid = updateuserid
  57. self.$state.$id.value = stateid!
  58. self.$unit.$id.value = unitid!
  59. }
  60. }
  61. extension mdlLocation{
  62. struct Create: AsyncMigration {
  63. func prepare(on database: Database) async throws {
  64. try await database.schema("location")
  65. .field("id", .int, .identifier(auto: true))
  66. .field("name", .string)
  67. .field("address", .string)
  68. .field("address2", .string)
  69. .field("city", .string)
  70. .field("postalCode", .string)
  71. .field("dealerCode", .string)
  72. .field("fileLocation", .string)
  73. .field("isActive", .bool)
  74. .field("createDate", .datetime)
  75. .field("createUserId", .int)
  76. .field("updateDate", .datetime)
  77. .field("updateUserId", .int)
  78. .field("stateId", .int)
  79. .field("unitId", .int)
  80. .field("labSettingId", .int)
  81. .create()
  82. }
  83. func revert(on database: Database) async throws {
  84. try await database.schema("location").delete()
  85. }
  86. }
  87. struct Mod1: AsyncMigration {
  88. func prepare(on database: Database) async throws {
  89. try await database.schema("location")
  90. .deleteField("labSettingId")
  91. .update()
  92. }
  93. func revert(on database: Database) async throws {
  94. try await database.schema("location")
  95. .field("labSettingId", .int)
  96. .update()
  97. }
  98. }
  99. public static func ObjQuery(req: Request) async throws -> QueryBuilder<mdlLocation>{
  100. let query = mdlLocation.query(on: req.db)
  101. .with(\.$state)
  102. .with(\.$unit)
  103. .with(\.$users)
  104. .with(\.$labsetting)
  105. if let x = req.parameters.get("x"){
  106. let dataQuery = req.parameters.get("x")
  107. let decryptedString = try CustomCrypto.DecryptString(input: dataQuery!)
  108. let x = decryptedString.components(separatedBy: ":")
  109. ///This call is the standard get by id call
  110. if (x.count == 1){
  111. let id = Int(x[0])!
  112. return query.filter(\.$id == id)
  113. }
  114. ///Optional else if if there are multiple parameters in the call
  115. ///You must include an else for each call that has parameters
  116. ///Need to check the endpoint to get the right filter statements
  117. ///else if(x.count == 2){
  118. /// let username = x[0]
  119. /// let password = x[1]
  120. /// return query.filter(\.$username == username).filter(\.$password == password)
  121. ///}
  122. else{
  123. throw Abort(.badRequest)
  124. }
  125. }
  126. else{
  127. return query
  128. }
  129. }
  130. }