|
- //
- // mdlObjProperty.swift
- //
- //
- // Created by Michiel Carman on 1/24/24.
- //
-
- import Fluent
- import Vapor
- import Foundation
- import FluentKit
-
- final class mdlObjProperty: Model, Content {
- static let schema = "objProperty"
-
- @ID(custom: "id", generatedBy: .database) var id: Int?
-
- @Field(key: "label") var label: String?
- @Field(key: "value") var value: String?
- @Field(key: "isActive") var isactive: Bool?
- @OptionalParent(key: "equipmentId") var equipment: mdlEquipment?
-
-
- @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, label: String? = nil, value: String? = nil, isactive: Bool? = nil, createuserid: Int? = nil, updateuserid: Int? = nil, equipmentid: Int? = nil) {
- self.id = id
- self.label = label
- self.value = value
- self.isactive = isactive
- if(equipmentid != nil) { self.$equipment.$id.value = equipmentid }
-
- self.createuserid = createuserid
- self.updateuserid = updateuserid
- }
- }
-
- extension mdlObjProperty{
- struct Create: AsyncMigration {
- func prepare(on database: Database) async throws {
- try await database.schema("objProperty")
- .field("id", .int, .identifier(auto: true))
- .field("label", .string)
- .field("value", .string)
- .field("isActive", .bool)
- .field("equipmentId", .int, .references("equipment", "id"))
- .field("createDate", .datetime)
- .field("createUserId", .int)
- .field("updateDate", .datetime)
- .field("updateUserId", .int)
- .create()
- }
-
- func revert(on database: Database) async throws {
- try await database.schema("objProperty").delete()
- }
- }
- public static func ObjQuery(req: Request) async throws -> QueryBuilder<mdlObjProperty>{
-
- let query = mdlObjProperty.query(on: req.db)
- .with(\.$equipment)
-
- 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
- }
- }
- }
-
-
|