|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126 |
- //
- // mdlNote.swift
- //
- //
- // Created by Michiel Carman on 2/16/24.
- //
-
-
- import Fluent
- import Vapor
- import Foundation
- import FluentKit
-
- final class mdlNote: Model, Content {
- static let schema = "note"
-
- @ID(custom: "id", generatedBy: .database) var id: Int?
- @Parent(key: "noteTypeId") var type: mdlNoteType
- @Field(key: "text") var text: String?
- @Field(key: "filePath") var filepath: String?
-
- @OptionalParent(key: "workOrderId") var workorder: mdlWorkOrder?
- @OptionalParent(key: "inspectionId") var inspection: mdlInspection?
- @OptionalParent(key: "inspectionItemId") var item: mdlInspectionItem?
-
- @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,
- typeid: Int,
- text: String? = nil,
- filepath: String? = nil,
- workorderid: Int? = nil,
- inspectionid: Int? = nil,
- inspectionitemid: Int? = nil,
- createuserid: Int? = nil,
- updateuserid: Int? = nil
- ) {
- self.id = id
- self.$type.$id.value = typeid
- self.text = text
- self.filepath = filepath
- self.$workorder.$id.value = workorderid!
- self.$inspection.$id.value = inspectionid!
- self.$item.$id.value = inspectionitemid!
- self.createuserid = createuserid
- self.updateuserid = updateuserid
- }
- }
- extension mdlNote{
- struct Create: AsyncMigration {
- func prepare(on database: Database) async throws {
- try await database.schema("note")
- .field("id", .int, .identifier(auto: true))
- .field("noteTypeId", .int, .references("noteType", "id"))
- .field("workOrderId", .int, .references("workOrder", "id"))
- .field("inspectionId", .int, .references("inspection", "id"))
- .field("inspectionItemId", .int, .references("inspectionItem", "id"))
-
- .field("text", .string)
- .field("filepath", .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("note").delete()
- }
- }
- struct Mod1: AsyncMigration {
- func prepare(on database: Database) async throws {
- try await database.schema("note")
- .deleteField("filepath")
- .field("filePath", .string)
- .update()
- }
-
- func revert(on database: Database) async throws {
- try await database.schema("note")
- .deleteField("filePath")
- .field("filepath", .string)
- .update()
- }
- }
- public static func ObjQuery(req: Request) async throws -> QueryBuilder<mdlNote>{
-
- let query = mdlNote.query(on: req.db)
- .with(\.$type)
- .with(\.$workorder)
- .with(\.$inspection)
- .with(\.$item)
-
- 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
- }
- }
- }
-
|