// // mdlSpecItem.swift // // // Created by Michiel Carman on 1/24/24. // import Fluent import Vapor import Foundation import FluentKit final class mdlSpecItem: Model, Content { static let schema = "specItem" @ID(custom: "id", generatedBy: .database) var id: Int? @Field(key: "name") var name: String? @Field(key: "qualifier") var qualifier: String? @Field(key: "sortOrder") var sortorder: Int? @Field(key: "isActive") var isactive: Bool? @Parent(key: "specId") var spec: mdlSpec @Parent(key: "comparisonTypeId") var comparisontype: mdlComparisonType @Parent(key: "unitId") var unit: mdlUnit @Children(for: \.$specitem) var values: [mdlSpecItemValue] @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, qualifier: String? = nil, isactive: Bool? = true, createuserid: Int? = nil, sortorder: Int? = nil, updateuserid: Int? = nil, specid: Int? = nil, comparisontypeid: Int? = nil, unitid: Int? = nil) { self.id = id self.name = name self.qualifier = qualifier self.sortorder = sortorder self.$spec.$id.value = specid! self.$comparisontype.$id.value = comparisontypeid! self.$unit.$id.value = unitid! self.isactive = isactive self.createuserid = createuserid self.updateuserid = updateuserid } } extension mdlSpecItem{ struct Create: AsyncMigration { func prepare(on database: Database) async throws { try await database.schema("specItem") .field("id", .int, .identifier(auto: true)) .field("name", .string) .field("qualifier", .string) .field("sortOrder", .int) .field("specId", .int, .references("spec", "id")) .field("comparisonTypeId", .int, .references("comparisonType", "id")) .field("unitId", .int, .references("unit", "id")) .field("isActive", .bool) .field("createDate", .datetime) .field("createUserId", .int) .field("updateDate", .datetime) .field("updateUserId", .int) .create() } func revert(on database: Database) async throws { try await database.schema("specItem").delete() } } public static func ObjQuery(req: Request) async throws -> QueryBuilder{ let query = mdlSpecItem.query(on: req.db) .with(\.$spec) .with(\.$comparisontype) .with(\.$unit) .with(\.$values) if let x = req.parameters.get("x"){ let dataQuery = req.parameters.get("x") let decryptedString = try CustomCrypto.DecryptString(input: dataQuery!) let x = decryptedString.components(separatedBy: ":") ///This call is the standard get by id call if (x.count == 1){ let id = Int(x[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 } } }