|
- //
- // mdlDepartment.swift
- //
- //
- // Created by Michiel Carman on 4/11/2024.
- //
-
-
- import Fluent
- import Vapor
- import Foundation
- import FluentKit
-
- final class mdlDepartment: Model, Content {
- static let schema = "department"
-
- @ID(custom: "id", generatedBy: .database) var id: Int?
-
- @Field(key: "name") var name: String?
- @OptionalParent(key: "departmentId") var parent: mdlDepartment?
- @Children(for: \.$parent) var subdepartments: [mdlDepartment]
-
- @Siblings(through: mdlXAssocDept.self, from: \.$department, to: \.$associate) var associates: [mdlAssociate]
- @Siblings(through: mdlXDocDept.self, from: \.$department, to: \.$document) var documents: [mdlDocument]
-
- @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,
- parentid: Int? = nil,
- createuserid: Int? = nil,
- updateuserid: Int? = nil
- ) {
- self.id = id
- self.name = name
- if(parentid != nil) { self.$parent.$id.value = parentid }
- self.createuserid = createuserid
- self.updateuserid = updateuserid
- }
- }
- extension mdlDepartment{
- struct Create: AsyncMigration {
- func prepare(on database: Database) async throws {
- try await database.schema("department")
- .field("id", .int, .identifier(auto: true))
- .field("name", .string)
- .field("departmentId", .int)
- .field("createDate", .datetime)
- .field("createUserId", .int)
- .field("updateDate", .datetime)
- .field("updateUserId", .int)
- .create()
- }
-
- func revert(on database: Database) async throws {
- try await database.schema("department").delete()
- }
- }
- struct AddParentReference: AsyncMigration {
- func prepare(on database: Database) async throws {
- try await database.schema("department")
- .deleteField("departmentId")
- .field("departmentId", .int, .references("department", "id"))
- .update()
- }
-
- func revert(on database: Database) async throws {
- try await database.schema("department")
- .deleteField("departmentId")
- .field("departmentId", .int)
- .update()
- }
- }
- public static func ObjQuery(req: Request) async throws -> QueryBuilder<mdlDepartment>{
-
- let query = mdlDepartment.query(on: req.db)
- .with(\.$subdepartments)
- .with(\.$parent)
- .with(\.$associates)
- .with(\.$documents){
- doc in doc
- .with(\.$doctype)
- .with(\.$parent)
- .with(\.$subsequent)
- }
-
- 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
- }
- }
- }
|