// // mdlLocation.swift // // // Created by Michiel Carman on 1/24/24. // import Fluent import Vapor import Foundation import FluentKit final class mdlLocation: Model, Content { static let schema = "location" @ID(custom: "id", generatedBy: .database) var id: Int? @Field(key: "name") var name: String? @Field(key: "address") var address: String? @Field(key: "address2") var address2: String? @Field(key: "city") var city: String? @Field(key: "postalCode") var postalcode: String? @Field(key: "dealerCode") var dealercode: String? @Field(key: "fileLocation") var filelocation: String? @Field(key: "isActive") var isactive: Bool? @Parent(key: "stateId") var state: mdlState @OptionalParent(key: "unitId") var unit: mdlUnit? // @OptionalParent(key: "labSettingId") var labsetting: mdlLabSetting? @Children(for: \.$location) var users: [mdlUser] @OptionalChild(for: \.$location) var labsetting: mdlLabSetting? @Timestamp(key: "createDate", on: .create) var createdate: Date? @Field(key: "createUserId") var createuserid: Int? @Timestamp(key: "updateDate", on: .update) var updatedate: Date? @Field(key: "updateUserId") var updateuserid: Int? init() { } init(id: Int? = nil, name: String? = nil, address: String? = nil, address2: String? = nil, city: String? = nil, postalcode: String? = nil, dealercode: String? = nil, filelocation: String? = nil, isactive: Bool? = nil, createuserid: Int? = nil, updateuserid: Int? = nil, stateid: Int, unitid: Int? = nil ) { self.id = id self.name = name self.address = address self.address2 = address2 self.city = city self.postalcode = postalcode self.dealercode = dealercode self.filelocation = filelocation self.isactive = isactive self.createuserid = createuserid self.updateuserid = updateuserid self.$state.$id.value = stateid if(unitid != nil) {self.$unit.$id.value = unitid} } } extension mdlLocation{ struct Create: AsyncMigration { func prepare(on database: Database) async throws { try await database.schema("location") .field("id", .int, .identifier(auto: true)) .field("name", .string) .field("address", .string) .field("address2", .string) .field("city", .string) .field("postalCode", .string) .field("dealerCode", .string) .field("fileLocation", .string) .field("isActive", .bool) .field("createDate", .datetime) .field("createUserId", .int) .field("updateDate", .datetime) .field("updateUserId", .int) .field("stateId", .int) .field("unitId", .int) .field("labSettingId", .int) .create() } func revert(on database: Database) async throws { try await database.schema("location").delete() } } struct Mod1: AsyncMigration { func prepare(on database: Database) async throws { try await database.schema("location") .deleteField("labSettingId") .update() } func revert(on database: Database) async throws { try await database.schema("location") .field("labSettingId", .int) .update() } } public static func ObjQuery(req: Request) async throws -> QueryBuilder{ let query = mdlLocation.query(on: req.db) .with(\.$state) .with(\.$unit) .with(\.$users) .with(\.$labsetting) if let x = req.parameters.get("x"){ let decryptedString = try CustomCrypto.DecryptString(input: x) let array = decryptedString.components(separatedBy: ":") ///This call is the standard get by id call if (array.count == 1){ let id = Int(array[0])! return query.filter(\.$id == id) } ///Optional else if if there are multiple parameters in the call ///You must include an else for each call that has parameters ///Need to check the endpoint to get the right filter statements ///else if(x.count == 2){ /// let username = x[0] /// let password = x[1] /// return query.filter(\.$username == username).filter(\.$password == password) ///} else{ throw Abort(.badRequest) } } else{ return query } } }