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.
 
 

152 lines
4.8 KiB

  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,
  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. if(unitid != nil) {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 decryptedString = try CustomCrypto.DecryptString(input: x)
  107. let array = decryptedString.components(separatedBy: ":")
  108. ///This call is the standard get by id call
  109. if (array.count == 1){
  110. let id = Int(array[0])!
  111. return query.filter(\.$id == id)
  112. }
  113. ///Optional else if if there are multiple parameters in the call
  114. ///You must include an else for each call that has parameters
  115. ///Need to check the endpoint to get the right filter statements
  116. ///else if(x.count == 2){
  117. /// let username = x[0]
  118. /// let password = x[1]
  119. /// return query.filter(\.$username == username).filter(\.$password == password)
  120. ///}
  121. else{
  122. throw Abort(.badRequest)
  123. }
  124. }
  125. else{
  126. return query
  127. }
  128. }
  129. }