|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118 |
- //
- // mdlGate.swift
- //
- //
- // Created by Michiel Carman on 1/24/24.
- //
-
- import Fluent
- import Vapor
- import Foundation
- import FluentKit
-
- final class mdlGate: Model, Content {
- static let schema = "gate"
-
- @ID(custom: "id", generatedBy: .database) var id: Int?
-
- @Field(key: "name") var name: String?
- @Field(key: "description") var description: String?
- @Field(key: "isActive") var isactive: Bool?
- @Field(key: "sortOrder") var sortorder: Int?
-
- @Parent(key: "locationId") var location: mdlLocation
-
- @Children(for: \.$gate) var users: [mdlUser]
- @Children(for: \.$gate) var values: [mdlTblValue]
-
- @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, description: String? = nil, isactive: Bool? = nil, sortorder: Int? = 0, createuserid: Int? = nil, updateuserid: Int? = nil, locationid: Int) {
- self.id = id
-
- self.name = name
- self.description = description
- self.isactive = isactive
- self.sortorder = sortorder
-
- self.$location.$id.value = locationid
-
- self.createuserid = createuserid
- self.updateuserid = updateuserid
- }
- }
-
- extension mdlGate{
- struct Create: AsyncMigration {
- func prepare(on database: Database) async throws {
- try await database.schema("gate")
- .field("id", .int, .identifier(auto: true))
- .field("name", .string)
- .field("description", .string)
- .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("gate").delete()
- }
- }
- struct Mod1: AsyncMigration{
- func prepare(on database: Database) async throws {
- try await database.schema("gate")
- .field("locationId", .int, .references("location", "id"))
- .field("sortOrder", .int)
- .update()
- }
- func revert(on database: Database) async throws {
- try await database.schema("gate")
- .deleteField("locationId")
- .deleteField("sortOrder")
- .update()
- }
- }
- public static func ObjQuery(req: Request) async throws -> QueryBuilder<mdlGate>{
-
- let query = mdlGate.query(on: req.db)
- .with(\.$users)
- .with(\.$values)
-
- 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
- }
- }
- }
-
-
-
-
|