|
- //
- // mdlStatus.swift
- //
- //
- // Created by Michiel Carman on 1/24/24.
- //
-
- import Fluent
- import Vapor
- import Foundation
- import FluentKit
-
- final class mdlStatus: Model, Content {
- static let schema = "status"
-
- @ID(custom: "id", generatedBy: .database) var id: Int?
-
- @Field(key: "name") var name: String?
- @Field(key: "description") var description: String?
-
- @Children(for: \.$status) var workorders: [mdlWorkOrder]
-
- @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, createuserid: Int? = nil, updateuserid: Int? = nil) {
- self.id = id
-
- self.name = name
- self.description = description
-
- self.createuserid = createuserid
- self.updateuserid = updateuserid
- }
- }
-
- extension mdlStatus{
- struct Create: AsyncMigration {
- func prepare(on database: Database) async throws {
- try await database.schema("status")
- .field("id", .int, .identifier(auto: true))
- .field("name", .string)
- .field("description", .string)
- .field("createDate", .datetime)
- .field("createUserId", .int)
- .field("updateDate", .datetime)
- .field("updateUserId", .int)
- .create()
- }
-
- func revert(on database: Database) async throws {
- try await database.schema("status").delete()
- }
- }
- struct Seed: Migration{
- func prepare(on database: Database) -> EventLoopFuture<Void> {
- let mdls: [mdlStatus] = [
- .init(id: 1, name: "Open", description: nil, createuserid: -1, updateuserid: -1),
- .init(id: 2, name: "Synced", description: nil, createuserid: -1, updateuserid: -1),
- .init(id: 3, name: "Completed", description: nil, createuserid: -1, updateuserid: -1),
- .init(id: 4, name: "Approved", description: nil, createuserid: -1, updateuserid: -1),
- .init(id: 5, name: "Incomplete", description: nil, createuserid: -1, updateuserid: -1),
- .init(id: 6, name: "Reassigned", description: nil, createuserid: -1, updateuserid: -1),
- .init(id: 7, name: "Pending", description: nil, createuserid: -1, updateuserid: -1),
- .init(id: 8, name: "Deleted", description: nil, createuserid: -1, updateuserid: -1)
- ]
- return mdls.map { mdl in
- mdl.save(on: database)
- }
- .flatten(on: database.eventLoop)
- }
-
- func revert(on database: Database) -> EventLoopFuture<Void> {
- mdlStatus.query(on: database).delete()
- }
-
-
- }
- public static func ObjQuery(req: Request) async throws -> QueryBuilder<mdlStatus>{
-
- let query = mdlStatus.query(on: req.db)
- .with(\.$workorders)
-
- 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
- }
- }
- }
-
-
|