From 2dade783753e89da73c678462f8de0cb94c36d31 Mon Sep 17 00:00:00 2001 From: rpm-mcarman <65228808+rpm-mcarman@users.noreply.github.com> Date: Fri, 5 Apr 2024 22:25:07 -0400 Subject: [PATCH] Modifications to api endpoints --- .../AuthControllers/cntrlRole.swift | 77 +++++--- .../AuthControllers/cntrlSegment.swift | 67 +++++-- .../AuthControllers/cntrlUser.swift | 75 +++++--- .../ChildControllers/cntrlResponseType.swift | 67 +++++-- .../ChildControllers/cntrlUnit.swift | 67 +++++-- .../BaseControllers/cntrlCategory.swift | 78 ++++++-- .../BaseControllers/cntrlComparisonType.swift | 67 +++++-- .../BaseControllers/cntrlContactType.swift | 67 +++++-- .../BaseControllers/cntrlDataType.swift | 80 +++++--- .../BaseControllers/cntrlDivision.swift | 69 +++++-- .../BaseControllers/cntrlEquipmentType.swift | 67 +++++-- .../BaseControllers/cntrlFluid.swift | 67 +++++-- .../BaseControllers/cntrlFuel.swift | 67 +++++-- .../BaseControllers/cntrlGate.swift | 69 +++++-- .../cntrlIncompleteReason.swift | 70 +++++-- .../BaseControllers/cntrlInputType.swift | 67 +++++-- .../BaseControllers/cntrlJobSite.swift | 67 +++++-- .../BaseControllers/cntrlLevel.swift | 67 +++++-- .../BaseControllers/cntrlLocation.swift | 67 +++++-- .../BaseControllers/cntrlNoteType.swift | 67 +++++-- .../BaseControllers/cntrlPart.swift | 67 +++++-- .../BaseControllers/cntrlPriority.swift | 67 +++++-- .../BaseControllers/cntrlReason.swift | 67 +++++-- .../BaseControllers/cntrlRepairType.swift | 67 +++++-- .../BaseControllers/cntrlSMSCCode.swift | 67 +++++-- .../BaseControllers/cntrlSignatureType.swift | 67 +++++-- .../BaseControllers/cntrlState.swift | 67 +++++-- .../BaseControllers/cntrlStatus.swift | 67 +++++-- .../BaseControllers/cntrlValueType.swift | 78 ++++++-- .../BaseControllers/cntrlViscosity.swift | 75 +++++--- .../cntrlCompartment.swift | 67 +++++-- .../cntrlCompartmentLocation.swift | 75 +++++--- .../CustomerControllers/cntrlContact.swift | 67 +++++-- .../cntrlContactInfo.swift | 67 +++++-- .../CustomerControllers/cntrlCustomer.swift | 67 +++++-- .../CustomerControllers/cntrlEquipment.swift | 67 +++++-- .../CustomerControllers/cntrlMake.swift | 67 +++++-- .../CustomerControllers/cntrlModel.swift | 67 +++++-- .../cntrlObjectProperty.swift | 71 ++++++-- .../CustomerControllers/cntrlProduct.swift | 67 +++++-- .../EncryptionController.swift | 17 ++ .../ItemControllers/cntrlItem.swift | 67 +++++-- .../cntrlItemResponseType.swift | 67 +++++-- .../LabControllers/cntrlLabSetting.swift | 67 +++++-- .../cntrlXFluidViscosity.swift | 67 +++++-- .../LinkingControllers/cntrlXUserRole.swift | 67 +++++-- .../OilSampleControllers/cntrlOilSample.swift | 67 +++++-- .../SpecControllers/cntrlSpec.swift | 67 +++++-- .../SpecControllers/cntrlSpecItem.swift | 67 +++++-- .../SpecControllers/cntrlSpecItemValue.swift | 67 +++++-- .../TemplateControllers/cntrlTemplate.swift | 67 +++++-- .../cntrlTemplateCategory.swift | 67 +++++-- .../cntrlTemplateItemSpec.swift | 67 +++++-- .../cntrlWorkOrderAttachment.swift | 67 +++++-- .../Consumptions/cntrlConsumption.swift | 67 +++++-- .../Inspections/cntrlInspection.swift | 67 +++++-- .../Inspections/cntrlInspectionCategory.swift | 67 +++++-- .../Inspections/cntrlInspectionItem.swift | 67 +++++-- .../cntrlInspectionItemResponse.swift | 67 +++++-- .../Inspections/cntrlInspectionItemSpec.swift | 67 +++++-- .../cntrlInspectionItemSpecItem.swift | 67 +++++-- .../cntrlInspectionItemSpecItemTest.swift | 67 +++++-- .../cntrlInspectionItemSpecItemValue.swift | 67 +++++-- .../Inspections/cntrlInspectionLevel.swift | 67 +++++-- .../Inspections/cntrlInspectionType.swift | 67 +++++-- .../Notes/cntrlNote.swift | 67 +++++-- .../Parts/cntrlWorkOrderPart.swift | 67 +++++-- .../SignatureControllers/cntrlSignature.swift | 67 +++++-- .../WorkOrderControllers/cntrlWorkOrder.swift | 67 +++++-- .../cntrlWorkOrderFlow.swift | 69 +++++-- .../cntrlWorkOrderSegment.swift | 67 +++++-- Sources/App/Models/AuthTables/mdlRole.swift | 9 +- .../App/Models/AuthTables/mdlSegment.swift | 9 +- Sources/App/Models/AuthTables/mdlUser.swift | 29 +-- .../ChildTables/mdlResponseType.swift | 13 +- .../BaseTables/ChildTables/mdlUnit.swift | 13 +- .../App/Models/BaseTables/mdlCategory.swift | 9 +- .../Models/BaseTables/mdlComparisonType.swift | 9 +- .../Models/BaseTables/mdlContactType.swift | 9 +- .../App/Models/BaseTables/mdlDataType.swift | 9 +- .../App/Models/BaseTables/mdlDivision.swift | 9 +- .../Models/BaseTables/mdlEquipmentType.swift | 9 +- Sources/App/Models/BaseTables/mdlFluid.swift | 9 +- Sources/App/Models/BaseTables/mdlFuel.swift | 9 +- Sources/App/Models/BaseTables/mdlGate.swift | 9 +- .../BaseTables/mdlIncompleteReason.swift | 9 +- .../App/Models/BaseTables/mdlInputType.swift | 9 +- .../App/Models/BaseTables/mdlJobSite.swift | 9 +- Sources/App/Models/BaseTables/mdlLevel.swift | 9 +- .../App/Models/BaseTables/mdlLocation.swift | 15 +- .../App/Models/BaseTables/mdlNoteType.swift | 9 +- Sources/App/Models/BaseTables/mdlPart.swift | 9 +- .../App/Models/BaseTables/mdlPriority.swift | 9 +- Sources/App/Models/BaseTables/mdlReason.swift | 9 +- .../App/Models/BaseTables/mdlRepairType.swift | 9 +- .../App/Models/BaseTables/mdlSMSCCode.swift | 9 +- .../Models/BaseTables/mdlSignatureType.swift | 9 +- Sources/App/Models/BaseTables/mdlState.swift | 9 +- Sources/App/Models/BaseTables/mdlStatus.swift | 9 +- .../App/Models/BaseTables/mdlValueType.swift | 9 +- .../App/Models/BaseTables/mdlViscosity.swift | 9 +- .../CompartmentModule/mdlCompartment.swift | 9 +- .../mdlCompartmentLocation.swift | 9 +- .../Models/CustomerTables/mdlContact.swift | 13 +- .../CustomerTables/mdlContactInfo.swift | 13 +- .../Models/CustomerTables/mdlCustomer.swift | 9 +- .../Models/CustomerTables/mdlEquipment.swift | 18 +- .../App/Models/CustomerTables/mdlMake.swift | 9 +- .../App/Models/CustomerTables/mdlModel.swift | 9 +- .../CustomerTables/mdlObjectProperty.swift | 13 +- .../Models/CustomerTables/mdlProduct.swift | 9 +- Sources/App/Models/ItemTables/mdlItem.swift | 9 +- .../ItemTables/mdlItemResponseType.swift | 15 +- .../App/Models/LabTables/mdlLabSetting.swift | 13 +- .../LinkingTables/XFluidViscosity.swift | 13 +- .../App/Models/LinkingTables/XUserRole.swift | 12 +- .../LinkingTables/XWorkOrderSegment.swift | 9 +- Sources/App/Models/SpecTables/mdlSpec.swift | 13 +- .../App/Models/SpecTables/mdlSpecItem.swift | 17 +- .../Models/SpecTables/mdlSpecItemValue.swift | 15 +- .../Models/TemplateTables/mdlTemplate.swift | 21 +-- .../TemplateTables/mdlTemplateCategory.swift | 9 +- .../TemplateTables/mdlTemplateItemSpec.swift | 13 +- .../Attachments/mdlWorkOrderAttachment.swift | 9 +- .../Consumptions/mdlConsumption.swift | 9 +- .../Inspections/mdlInspection.swift | 9 +- .../Inspections/mdlInspectionCategory.swift | 9 +- .../Inspections/mdlInspectionItem.swift | 13 +- .../mdlInspectionItemResponse.swift | 9 +- .../Inspections/mdlInspectionItemSpec.swift | 9 +- .../mdlInspectionItemSpecItem.swift | 9 +- .../mdlInspectionItemSpecItemTest.swift | 9 +- .../mdlInspectionItemSpecItemValue.swift | 9 +- .../Inspections/mdlInspectionLevel.swift | 9 +- .../Inspections/mdlInspectionType.swift | 9 +- .../WorkOrderTables/Notes/mdlNote.swift | 9 +- .../Parts/mdlWorkOrderPart.swift | 9 +- .../Samples/mdlOilSample.swift | 9 +- .../Signatures/mdlSignature.swift | 9 +- .../Models/WorkOrderTables/mdlWorkOrder.swift | 9 +- .../WorkOrderTables/mdlWorkOrderFlow.swift | 9 +- .../WorkOrderTables/mdlWorkOrderSegment.swift | 9 +- Sources/App/routes.swift | 4 +- Tests/AppTests/UserTests.swift | 171 +++++++++++------- 144 files changed, 3930 insertions(+), 1783 deletions(-) diff --git a/Sources/App/Controllers/AuthControllers/cntrlRole.swift b/Sources/App/Controllers/AuthControllers/cntrlRole.swift index 2e7969d..128938c 100644 --- a/Sources/App/Controllers/AuthControllers/cntrlRole.swift +++ b/Sources/App/Controllers/AuthControllers/cntrlRole.swift @@ -1,12 +1,15 @@ // -// File.swift -// +// cntrlRole.swift // -// Created by Michiel Carman on 4/1/24. +// +// Created by Michiel Carman on 4/5/2024. // import Fluent import Vapor +import Crypto + + struct cntrlRole: RouteCollection { func boot(routes: RoutesBuilder) throws { @@ -14,34 +17,62 @@ struct cntrlRole: RouteCollection { rts.get(use: index) rts.get(":x", use: read) rts.post(use: create) - rts.group(":x") { rt in - rts.delete(use: delete) - } - ///Add custom routes here + rts.delete(":x", use: delete) + //Additional routs added here } - - func index(req: Request) async throws -> [mdlRole] { - try await mdlRole.ObjQuery(req: req).all() + + func index(req: Request) async throws -> Response { + guard let obj = try? await mdlRole.ObjQuery(req: req).all() else { + throw Abort(.badGateway, reason: "Something went wrong with this api.") + } + guard let response = try? await obj.encodeResponse(status: .ok, for: req) else{ + throw Abort(.noContent, reason: "Something went wrong decoding data. Array could not be shown.") + } + return response } - func read(req:Request) async throws -> mdlRole{ - return try await mdlRole.ObjQuery(req: req).first()! + func read(req:Request) async throws -> Response{ + guard let obj = try? await mdlRole.ObjQuery(req: req).first() else{ + throw Abort(.notFound, reason: "Role was not found.") + } + guard let response = try? await obj.encodeResponse(status: .ok, for: req) else { + throw Abort(.noContent, reason: "Something went wrong decoding object.") + } + return response } - - func create(req: Request) async throws -> mdlRole { - let obj = try req.content.decode(mdlRole.self) + + func create(req: Request) async throws -> Response { + + guard let obj = try? req.content.decode(mdlRole.self) else { + throw Abort(.notAcceptable, reason: "Role json not formatted correctly or is missing parents.") + } + var exists = false if(obj.id != nil){ obj._$idExists = true + exists = true + } + guard let _ = try? await obj.save(on: req.db) else{ + throw Abort(.conflict, reason: "Something conflicts in the database. Role cannot be saved.") } - try await obj.save(on: req.db) - return obj + + guard let response = try? await obj.encodeResponse(status: exists ? .accepted : .created, for: req) else{ + throw Abort(.noContent, reason: "Something went wrong decoding data. Role has been saved.") + } + return response } - - func delete(req: Request) async throws -> HTTPStatus { - guard let obj = try await mdlRole.find(req.parameters.get("id"), on: req.db) else { - throw Abort(.notFound) + + + func delete(req: Request) async throws -> Response { + guard let obj = try? await mdlRole.ObjQuery(req: req).first() else { + throw Abort(.notFound, reason: "Role's ID was not supplied. Role will not be deleted") + } + guard let _ = try? await obj.delete(on: req.db) else{ + throw Abort(.conflict, reason: "There was a conflict deleting Role. Make sure it has not children.") } - try await obj.delete(on: req.db) - return .noContent + guard let response = try? await obj.encodeResponse(status: .ok, for: req) else{ + throw Abort(.noContent, reason: "Something went wrong decoding data. Role has been deleted.") + } + return response } } + diff --git a/Sources/App/Controllers/AuthControllers/cntrlSegment.swift b/Sources/App/Controllers/AuthControllers/cntrlSegment.swift index 0d55cc7..81bad71 100644 --- a/Sources/App/Controllers/AuthControllers/cntrlSegment.swift +++ b/Sources/App/Controllers/AuthControllers/cntrlSegment.swift @@ -2,11 +2,14 @@ // cntrlSegment.swift // // -// Created by Michiel Carman on 4/1/24. +// Created by Michiel Carman on 4/5/2024. // import Fluent import Vapor +import Crypto + + struct cntrlSegment: RouteCollection { func boot(routes: RoutesBuilder) throws { @@ -14,34 +17,62 @@ struct cntrlSegment: RouteCollection { rts.get(use: index) rts.get(":x", use: read) rts.post(use: create) - rts.group(":x") { rt in - rts.delete(use: delete) - } - ///Add custom routes here + rts.delete(":x", use: delete) + //Additional routs added here } - func index(req: Request) async throws -> [mdlSegment] { - try await mdlSegment.ObjQuery(req: req).all() + func index(req: Request) async throws -> Response { + guard let obj = try? await mdlSegment.ObjQuery(req: req).all() else { + throw Abort(.badGateway, reason: "Something went wrong with this api.") + } + guard let response = try? await obj.encodeResponse(status: .ok, for: req) else{ + throw Abort(.noContent, reason: "Something went wrong decoding data. Array could not be shown.") + } + return response } - func read(req:Request) async throws -> mdlSegment{ - return try await mdlSegment.ObjQuery(req: req).first()! + func read(req:Request) async throws -> Response{ + guard let obj = try? await mdlSegment.ObjQuery(req: req).first() else{ + throw Abort(.notFound, reason: "Segment was not found.") + } + guard let response = try? await obj.encodeResponse(status: .ok, for: req) else { + throw Abort(.noContent, reason: "Something went wrong decoding object.") + } + return response } - func create(req: Request) async throws -> mdlSegment { - let obj = try req.content.decode(mdlSegment.self) + func create(req: Request) async throws -> Response { + + guard let obj = try? req.content.decode(mdlSegment.self) else { + throw Abort(.notAcceptable, reason: "Segment json not formatted correctly or is missing parents.") + } + var exists = false if(obj.id != nil){ obj._$idExists = true + exists = true + } + guard let _ = try? await obj.save(on: req.db) else{ + throw Abort(.conflict, reason: "Something conflicts in the database. Segment cannot be saved.") } - try await obj.save(on: req.db) - return obj + + guard let response = try? await obj.encodeResponse(status: exists ? .accepted : .created, for: req) else{ + throw Abort(.noContent, reason: "Something went wrong decoding data. Segment has been saved.") + } + return response } - func delete(req: Request) async throws -> HTTPStatus { - guard let obj = try await mdlSegment.find(req.parameters.get("id"), on: req.db) else { - throw Abort(.notFound) + + func delete(req: Request) async throws -> Response { + guard let obj = try? await mdlSegment.ObjQuery(req: req).first() else { + throw Abort(.notFound, reason: "Segment's ID was not supplied. Segment will not be deleted") } - try await obj.delete(on: req.db) - return .noContent + guard let _ = try? await obj.delete(on: req.db) else{ + throw Abort(.conflict, reason: "There was a conflict deleting Segment. Make sure it has not children.") + } + guard let response = try? await obj.encodeResponse(status: .ok, for: req) else{ + throw Abort(.noContent, reason: "Something went wrong decoding data. Segment has been deleted.") + } + return response } } + diff --git a/Sources/App/Controllers/AuthControllers/cntrlUser.swift b/Sources/App/Controllers/AuthControllers/cntrlUser.swift index ca2983a..5582029 100644 --- a/Sources/App/Controllers/AuthControllers/cntrlUser.swift +++ b/Sources/App/Controllers/AuthControllers/cntrlUser.swift @@ -1,5 +1,5 @@ // -// File.swift +// cntrlUser.swift // // // Created by Michiel Carman on 4/1/24. @@ -18,43 +18,70 @@ struct cntrlUser: RouteCollection { rts.get("login", ":x", use: login) rts.get(":x", use: read) rts.post(use: create) - rts.group(":x") { todo in - rts.delete(use: delete) - } - } - - func api(req: Request) async throws -> String{ - return Environment.get("API_KEY")! + rts.delete(":x", use: delete) } - func login(req: Request) async throws -> mdlUser{ - return try await mdlUser.ObjQuery(req: req).first()! + func login(req: Request) async throws -> Response{ + guard let obj = try? await mdlUser.ObjQuery(req: req).first() else { + throw Abort(.unauthorized, reason: "Username or Password or Both are invalid.") + } + guard let response = try? await obj.encodeResponse(status: .ok, for: req) else { + throw Abort(.noContent, reason: "Something went wrong decoding data. User has been authenticated.") + } + return response } - func index(req: Request) async throws -> [mdlUser] { - //try await mdlUser.query(on: req.db).with(\.$roles).all() - return try await mdlUser.ObjQuery(req: req).all() + func index(req: Request) async throws -> Response { + guard let obj = try? await mdlUser.ObjQuery(req: req).all() else { + throw Abort(.badGateway, reason: "Something went wrong with this api.") + } + guard let response = try? await obj.encodeResponse(status: .ok, for: req) else{ + throw Abort(.noContent, reason: "Something went wrong decoding data. Array could not be shown.") + } + return response } - func read(req:Request) async throws -> mdlUser{ - return try await mdlUser.ObjQuery(req: req).first()! + func read(req:Request) async throws -> Response{ + guard let obj = try? await mdlUser.ObjQuery(req: req).first() else{ + throw Abort(.notFound, reason: "User was not found.") + } + guard let response = try? await obj.encodeResponse(status: .ok, for: req) else { + throw Abort(.noContent, reason: "Something went wrong decoding object.") + } + return response } - func create(req: Request) async throws -> mdlUser { - let obj = try req.content.decode(mdlUser.self) + func create(req: Request) async throws -> Response { + + guard let obj = try? req.content.decode(mdlUser.self) else { + throw Abort(.notAcceptable, reason: "User json not formatted correctly or is missing parents.") + } + var exists = false if(obj.id != nil){ obj._$idExists = true + exists = true + } + guard let _ = try? await obj.save(on: req.db) else{ + throw Abort(.conflict, reason: "Something conflicts in the database. User cannot be saved.") } - try await obj.save(on: req.db) - return obj + + guard let response = try? await obj.encodeResponse(status: exists ? .accepted : .created, for: req) else{ + throw Abort(.noContent, reason: "Something went wrong decoding data. User has been saved.") + } + return response } - func delete(req: Request) async throws -> HTTPStatus { - guard let obj = try await mdlUser.find(req.parameters.get("x"), on: req.db) else { - throw Abort(.notFound) + func delete(req: Request) async throws -> Response { + guard let obj = try? await mdlUser.ObjQuery(req: req).first() else { + throw Abort(.notFound, reason: "User's ID was not supplied. User will not be deleted") + } + guard let _ = try? await obj.delete(on: req.db) else{ + throw Abort(.conflict, reason: "There was a conflict deleting User. Make sure it has not children.") + } + guard let response = try? await obj.encodeResponse(status: .ok, for: req) else{ + throw Abort(.noContent, reason: "Something went wrong decoding data. User has been deleted.") } - try await obj.delete(on: req.db) - return .noContent + return response } } diff --git a/Sources/App/Controllers/BaseControllers/ChildControllers/cntrlResponseType.swift b/Sources/App/Controllers/BaseControllers/ChildControllers/cntrlResponseType.swift index 6a54921..b5de84d 100644 --- a/Sources/App/Controllers/BaseControllers/ChildControllers/cntrlResponseType.swift +++ b/Sources/App/Controllers/BaseControllers/ChildControllers/cntrlResponseType.swift @@ -2,11 +2,14 @@ // cntrlResponseType.swift // // -// Created by Michiel Carman on 4/1/24. +// Created by Michiel Carman on 4/5/2024. // import Fluent import Vapor +import Crypto + + struct cntrlResponseType: RouteCollection { func boot(routes: RoutesBuilder) throws { @@ -14,34 +17,62 @@ struct cntrlResponseType: RouteCollection { rts.get(use: index) rts.get(":x", use: read) rts.post(use: create) - rts.group(":x") { rt in - rts.delete(use: delete) - } - ///Add custom routes here + rts.delete(":x", use: delete) + //Additional routs added here } - func index(req: Request) async throws -> [mdlResponseType] { - try await mdlResponseType.ObjQuery(req: req).all() + func index(req: Request) async throws -> Response { + guard let obj = try? await mdlResponseType.ObjQuery(req: req).all() else { + throw Abort(.badGateway, reason: "Something went wrong with this api.") + } + guard let response = try? await obj.encodeResponse(status: .ok, for: req) else{ + throw Abort(.noContent, reason: "Something went wrong decoding data. Array could not be shown.") + } + return response } - func read(req:Request) async throws -> mdlResponseType{ - return try await mdlResponseType.ObjQuery(req: req).first()! + func read(req:Request) async throws -> Response{ + guard let obj = try? await mdlResponseType.ObjQuery(req: req).first() else{ + throw Abort(.notFound, reason: "ResponseType was not found.") + } + guard let response = try? await obj.encodeResponse(status: .ok, for: req) else { + throw Abort(.noContent, reason: "Something went wrong decoding object.") + } + return response } - func create(req: Request) async throws -> mdlResponseType { - let obj = try req.content.decode(mdlResponseType.self) + func create(req: Request) async throws -> Response { + + guard let obj = try? req.content.decode(mdlResponseType.self) else { + throw Abort(.notAcceptable, reason: "ResponseType json not formatted correctly or is missing parents.") + } + var exists = false if(obj.id != nil){ obj._$idExists = true + exists = true + } + guard let _ = try? await obj.save(on: req.db) else{ + throw Abort(.conflict, reason: "Something conflicts in the database. ResponseType cannot be saved.") } - try await obj.save(on: req.db) - return obj + + guard let response = try? await obj.encodeResponse(status: exists ? .accepted : .created, for: req) else{ + throw Abort(.noContent, reason: "Something went wrong decoding data. ResponseType has been saved.") + } + return response } - func delete(req: Request) async throws -> HTTPStatus { - guard let obj = try await mdlResponseType.find(req.parameters.get("id"), on: req.db) else { - throw Abort(.notFound) + + func delete(req: Request) async throws -> Response { + guard let obj = try? await mdlResponseType.ObjQuery(req: req).first() else { + throw Abort(.notFound, reason: "ResponseType's ID was not supplied. ResponseType will not be deleted") } - try await obj.delete(on: req.db) - return .noContent + guard let _ = try? await obj.delete(on: req.db) else{ + throw Abort(.conflict, reason: "There was a conflict deleting ResponseType. Make sure it has not children.") + } + guard let response = try? await obj.encodeResponse(status: .ok, for: req) else{ + throw Abort(.noContent, reason: "Something went wrong decoding data. ResponseType has been deleted.") + } + return response } } + diff --git a/Sources/App/Controllers/BaseControllers/ChildControllers/cntrlUnit.swift b/Sources/App/Controllers/BaseControllers/ChildControllers/cntrlUnit.swift index a49c461..374d4f3 100644 --- a/Sources/App/Controllers/BaseControllers/ChildControllers/cntrlUnit.swift +++ b/Sources/App/Controllers/BaseControllers/ChildControllers/cntrlUnit.swift @@ -2,11 +2,14 @@ // cntrlUnit.swift // // -// Created by Michiel Carman on 4/1/24. +// Created by Michiel Carman on 4/5/2024. // import Fluent import Vapor +import Crypto + + struct cntrlUnit: RouteCollection { func boot(routes: RoutesBuilder) throws { @@ -14,34 +17,62 @@ struct cntrlUnit: RouteCollection { rts.get(use: index) rts.get(":x", use: read) rts.post(use: create) - rts.group(":x") { rt in - rts.delete(use: delete) - } - ///Add custom routes here + rts.delete(":x", use: delete) + //Additional routs added here } - func index(req: Request) async throws -> [mdlUnit] { - try await mdlUnit.ObjQuery(req: req).all() + func index(req: Request) async throws -> Response { + guard let obj = try? await mdlUnit.ObjQuery(req: req).all() else { + throw Abort(.badGateway, reason: "Something went wrong with this api.") + } + guard let response = try? await obj.encodeResponse(status: .ok, for: req) else{ + throw Abort(.noContent, reason: "Something went wrong decoding data. Array could not be shown.") + } + return response } - func read(req:Request) async throws -> mdlUnit{ - return try await mdlUnit.ObjQuery(req: req).first()! + func read(req:Request) async throws -> Response{ + guard let obj = try? await mdlUnit.ObjQuery(req: req).first() else{ + throw Abort(.notFound, reason: "Unit was not found.") + } + guard let response = try? await obj.encodeResponse(status: .ok, for: req) else { + throw Abort(.noContent, reason: "Something went wrong decoding object.") + } + return response } - func create(req: Request) async throws -> mdlUnit { - let obj = try req.content.decode(mdlUnit.self) + func create(req: Request) async throws -> Response { + + guard let obj = try? req.content.decode(mdlUnit.self) else { + throw Abort(.notAcceptable, reason: "Unit json not formatted correctly or is missing parents.") + } + var exists = false if(obj.id != nil){ obj._$idExists = true + exists = true + } + guard let _ = try? await obj.save(on: req.db) else{ + throw Abort(.conflict, reason: "Something conflicts in the database. Unit cannot be saved.") } - try await obj.save(on: req.db) - return obj + + guard let response = try? await obj.encodeResponse(status: exists ? .accepted : .created, for: req) else{ + throw Abort(.noContent, reason: "Something went wrong decoding data. Unit has been saved.") + } + return response } - func delete(req: Request) async throws -> HTTPStatus { - guard let obj = try await mdlUnit.find(req.parameters.get("id"), on: req.db) else { - throw Abort(.notFound) + + func delete(req: Request) async throws -> Response { + guard let obj = try? await mdlUnit.ObjQuery(req: req).first() else { + throw Abort(.notFound, reason: "Unit's ID was not supplied. Unit will not be deleted") } - try await obj.delete(on: req.db) - return .noContent + guard let _ = try? await obj.delete(on: req.db) else{ + throw Abort(.conflict, reason: "There was a conflict deleting Unit. Make sure it has not children.") + } + guard let response = try? await obj.encodeResponse(status: .ok, for: req) else{ + throw Abort(.noContent, reason: "Something went wrong decoding data. Unit has been deleted.") + } + return response } } + diff --git a/Sources/App/Controllers/BaseControllers/cntrlCategory.swift b/Sources/App/Controllers/BaseControllers/cntrlCategory.swift index a78eefe..d61b74d 100644 --- a/Sources/App/Controllers/BaseControllers/cntrlCategory.swift +++ b/Sources/App/Controllers/BaseControllers/cntrlCategory.swift @@ -1,5 +1,15 @@ +// +// cntrlCategory.swift +// +// +// Created by Michiel Carman on 4/5/2024. +// + import Fluent import Vapor +import Crypto + + struct cntrlCategory: RouteCollection { func boot(routes: RoutesBuilder) throws { @@ -7,34 +17,62 @@ struct cntrlCategory: RouteCollection { rts.get(use: index) rts.get(":x", use: read) rts.post(use: create) - rts.group(":x") { rt in - rts.delete(use: delete) - } - ///Add custom routes here + rts.delete(":x", use: delete) + //Additional routs added here } - - func index(req: Request) async throws -> [mdlCategory] { - try await mdlCategory.ObjQuery(req: req).all() + + func index(req: Request) async throws -> Response { + guard let obj = try? await mdlCategory.ObjQuery(req: req).all() else { + throw Abort(.badGateway, reason: "Something went wrong with this api.") + } + guard let response = try? await obj.encodeResponse(status: .ok, for: req) else{ + throw Abort(.noContent, reason: "Something went wrong decoding data. Array could not be shown.") + } + return response } - func read(req:Request) async throws -> mdlCategory{ - return try await mdlCategory.ObjQuery(req: req).first()! + func read(req:Request) async throws -> Response{ + guard let obj = try? await mdlCategory.ObjQuery(req: req).first() else{ + throw Abort(.notFound, reason: "Category was not found.") + } + guard let response = try? await obj.encodeResponse(status: .ok, for: req) else { + throw Abort(.noContent, reason: "Something went wrong decoding object.") + } + return response } - - func create(req: Request) async throws -> mdlCategory { - let obj = try req.content.decode(mdlCategory.self) + + func create(req: Request) async throws -> Response { + + guard let obj = try? req.content.decode(mdlCategory.self) else { + throw Abort(.notAcceptable, reason: "Category json not formatted correctly or is missing parents.") + } + var exists = false if(obj.id != nil){ obj._$idExists = true + exists = true + } + guard let _ = try? await obj.save(on: req.db) else{ + throw Abort(.conflict, reason: "Something conflicts in the database. Category cannot be saved.") } - try await obj.save(on: req.db) - return obj + + guard let response = try? await obj.encodeResponse(status: exists ? .accepted : .created, for: req) else{ + throw Abort(.noContent, reason: "Something went wrong decoding data. Category has been saved.") + } + return response } - - func delete(req: Request) async throws -> HTTPStatus { - guard let obj = try await mdlCategory.find(req.parameters.get("id"), on: req.db) else { - throw Abort(.notFound) + + + func delete(req: Request) async throws -> Response { + guard let obj = try? await mdlCategory.ObjQuery(req: req).first() else { + throw Abort(.notFound, reason: "Category's ID was not supplied. Category will not be deleted") + } + guard let _ = try? await obj.delete(on: req.db) else{ + throw Abort(.conflict, reason: "There was a conflict deleting Category. Make sure it has not children.") } - try await obj.delete(on: req.db) - return .noContent + guard let response = try? await obj.encodeResponse(status: .ok, for: req) else{ + throw Abort(.noContent, reason: "Something went wrong decoding data. Category has been deleted.") + } + return response } } + diff --git a/Sources/App/Controllers/BaseControllers/cntrlComparisonType.swift b/Sources/App/Controllers/BaseControllers/cntrlComparisonType.swift index 0c21278..ab40055 100644 --- a/Sources/App/Controllers/BaseControllers/cntrlComparisonType.swift +++ b/Sources/App/Controllers/BaseControllers/cntrlComparisonType.swift @@ -2,11 +2,14 @@ // cntrlComparisonType.swift // // -// Created by Michiel Carman on 4/1/24. +// Created by Michiel Carman on 4/5/2024. // import Fluent import Vapor +import Crypto + + struct cntrlComparisonType: RouteCollection { func boot(routes: RoutesBuilder) throws { @@ -14,34 +17,62 @@ struct cntrlComparisonType: RouteCollection { rts.get(use: index) rts.get(":x", use: read) rts.post(use: create) - rts.group(":x") { rt in - rts.delete(use: delete) - } - ///Add custom routes here + rts.delete(":x", use: delete) + //Additional routs added here } - func index(req: Request) async throws -> [mdlComparisonType] { - try await mdlComparisonType.ObjQuery(req: req).all() + func index(req: Request) async throws -> Response { + guard let obj = try? await mdlComparisonType.ObjQuery(req: req).all() else { + throw Abort(.badGateway, reason: "Something went wrong with this api.") + } + guard let response = try? await obj.encodeResponse(status: .ok, for: req) else{ + throw Abort(.noContent, reason: "Something went wrong decoding data. Array could not be shown.") + } + return response } - func read(req:Request) async throws -> mdlComparisonType{ - return try await mdlComparisonType.ObjQuery(req: req).first()! + func read(req:Request) async throws -> Response{ + guard let obj = try? await mdlComparisonType.ObjQuery(req: req).first() else{ + throw Abort(.notFound, reason: "ComparisonType was not found.") + } + guard let response = try? await obj.encodeResponse(status: .ok, for: req) else { + throw Abort(.noContent, reason: "Something went wrong decoding object.") + } + return response } - func create(req: Request) async throws -> mdlComparisonType { - let obj = try req.content.decode(mdlComparisonType.self) + func create(req: Request) async throws -> Response { + + guard let obj = try? req.content.decode(mdlComparisonType.self) else { + throw Abort(.notAcceptable, reason: "ComparisonType json not formatted correctly or is missing parents.") + } + var exists = false if(obj.id != nil){ obj._$idExists = true + exists = true + } + guard let _ = try? await obj.save(on: req.db) else{ + throw Abort(.conflict, reason: "Something conflicts in the database. ComparisonType cannot be saved.") } - try await obj.save(on: req.db) - return obj + + guard let response = try? await obj.encodeResponse(status: exists ? .accepted : .created, for: req) else{ + throw Abort(.noContent, reason: "Something went wrong decoding data. ComparisonType has been saved.") + } + return response } - func delete(req: Request) async throws -> HTTPStatus { - guard let obj = try await mdlComparisonType.find(req.parameters.get("id"), on: req.db) else { - throw Abort(.notFound) + + func delete(req: Request) async throws -> Response { + guard let obj = try? await mdlComparisonType.ObjQuery(req: req).first() else { + throw Abort(.notFound, reason: "ComparisonType's ID was not supplied. ComparisonType will not be deleted") } - try await obj.delete(on: req.db) - return .noContent + guard let _ = try? await obj.delete(on: req.db) else{ + throw Abort(.conflict, reason: "There was a conflict deleting ComparisonType. Make sure it has not children.") + } + guard let response = try? await obj.encodeResponse(status: .ok, for: req) else{ + throw Abort(.noContent, reason: "Something went wrong decoding data. ComparisonType has been deleted.") + } + return response } } + diff --git a/Sources/App/Controllers/BaseControllers/cntrlContactType.swift b/Sources/App/Controllers/BaseControllers/cntrlContactType.swift index f2194a6..183ec85 100644 --- a/Sources/App/Controllers/BaseControllers/cntrlContactType.swift +++ b/Sources/App/Controllers/BaseControllers/cntrlContactType.swift @@ -2,11 +2,14 @@ // cntrlContactType.swift // // -// Created by Michiel Carman on 4/1/24. +// Created by Michiel Carman on 4/5/2024. // import Fluent import Vapor +import Crypto + + struct cntrlContactType: RouteCollection { func boot(routes: RoutesBuilder) throws { @@ -14,34 +17,62 @@ struct cntrlContactType: RouteCollection { rts.get(use: index) rts.get(":x", use: read) rts.post(use: create) - rts.group(":x") { rt in - rts.delete(use: delete) - } - ///Add custom routes here + rts.delete(":x", use: delete) + //Additional routs added here } - func index(req: Request) async throws -> [mdlContactType] { - try await mdlContactType.ObjQuery(req: req).all() + func index(req: Request) async throws -> Response { + guard let obj = try? await mdlContactType.ObjQuery(req: req).all() else { + throw Abort(.badGateway, reason: "Something went wrong with this api.") + } + guard let response = try? await obj.encodeResponse(status: .ok, for: req) else{ + throw Abort(.noContent, reason: "Something went wrong decoding data. Array could not be shown.") + } + return response } - func read(req:Request) async throws -> mdlContactType{ - return try await mdlContactType.ObjQuery(req: req).first()! + func read(req:Request) async throws -> Response{ + guard let obj = try? await mdlContactType.ObjQuery(req: req).first() else{ + throw Abort(.notFound, reason: "ContactType was not found.") + } + guard let response = try? await obj.encodeResponse(status: .ok, for: req) else { + throw Abort(.noContent, reason: "Something went wrong decoding object.") + } + return response } - func create(req: Request) async throws -> mdlContactType { - let obj = try req.content.decode(mdlContactType.self) + func create(req: Request) async throws -> Response { + + guard let obj = try? req.content.decode(mdlContactType.self) else { + throw Abort(.notAcceptable, reason: "ContactType json not formatted correctly or is missing parents.") + } + var exists = false if(obj.id != nil){ obj._$idExists = true + exists = true + } + guard let _ = try? await obj.save(on: req.db) else{ + throw Abort(.conflict, reason: "Something conflicts in the database. ContactType cannot be saved.") } - try await obj.save(on: req.db) - return obj + + guard let response = try? await obj.encodeResponse(status: exists ? .accepted : .created, for: req) else{ + throw Abort(.noContent, reason: "Something went wrong decoding data. ContactType has been saved.") + } + return response } - func delete(req: Request) async throws -> HTTPStatus { - guard let obj = try await mdlContactType.find(req.parameters.get("id"), on: req.db) else { - throw Abort(.notFound) + + func delete(req: Request) async throws -> Response { + guard let obj = try? await mdlContactType.ObjQuery(req: req).first() else { + throw Abort(.notFound, reason: "ContactType's ID was not supplied. ContactType will not be deleted") } - try await obj.delete(on: req.db) - return .noContent + guard let _ = try? await obj.delete(on: req.db) else{ + throw Abort(.conflict, reason: "There was a conflict deleting ContactType. Make sure it has not children.") + } + guard let response = try? await obj.encodeResponse(status: .ok, for: req) else{ + throw Abort(.noContent, reason: "Something went wrong decoding data. ContactType has been deleted.") + } + return response } } + diff --git a/Sources/App/Controllers/BaseControllers/cntrlDataType.swift b/Sources/App/Controllers/BaseControllers/cntrlDataType.swift index 2452e35..cc7de6c 100644 --- a/Sources/App/Controllers/BaseControllers/cntrlDataType.swift +++ b/Sources/App/Controllers/BaseControllers/cntrlDataType.swift @@ -1,40 +1,78 @@ +// +// cntrlDataType.swift +// +// +// Created by Michiel Carman on 4/5/2024. +// + import Fluent import Vapor +import Crypto + + -struct cntrlDataTypes: RouteCollection { +struct cntrlDataType: RouteCollection { func boot(routes: RoutesBuilder) throws { let rts = routes.grouped("datatypes") rts.get(use: index) rts.get(":x", use: read) rts.post(use: create) - rts.group(":x") { rt in - rts.delete(use: delete) - } - ///Add custom routes here + rts.delete(":x", use: delete) + //Additional routs added here } - - func index(req: Request) async throws -> [mdlDataType] { - try await mdlDataType.ObjQuery(req: req).with(\.$units).all() + + func index(req: Request) async throws -> Response { + guard let obj = try? await mdlDataType.ObjQuery(req: req).all() else { + throw Abort(.badGateway, reason: "Something went wrong with this api.") + } + guard let response = try? await obj.encodeResponse(status: .ok, for: req) else{ + throw Abort(.noContent, reason: "Something went wrong decoding data. Array could not be shown.") + } + return response } - func read(req:Request) async throws -> mdlDataType{ - return try await mdlDataType.ObjQuery(req: req).first()! + func read(req:Request) async throws -> Response{ + guard let obj = try? await mdlDataType.ObjQuery(req: req).first() else{ + throw Abort(.notFound, reason: "DataType was not found.") + } + guard let response = try? await obj.encodeResponse(status: .ok, for: req) else { + throw Abort(.noContent, reason: "Something went wrong decoding object.") + } + return response } - - func create(req: Request) async throws -> mdlDataType { - let obj = try req.content.decode(mdlDataType.self) + + func create(req: Request) async throws -> Response { + + guard let obj = try? req.content.decode(mdlDataType.self) else { + throw Abort(.notAcceptable, reason: "DataType json not formatted correctly or is missing parents.") + } + var exists = false if(obj.id != nil){ obj._$idExists = true + exists = true + } + guard let _ = try? await obj.save(on: req.db) else{ + throw Abort(.conflict, reason: "Something conflicts in the database. DataType cannot be saved.") } - try await obj.save(on: req.db) - return obj + + guard let response = try? await obj.encodeResponse(status: exists ? .accepted : .created, for: req) else{ + throw Abort(.noContent, reason: "Something went wrong decoding data. DataType has been saved.") + } + return response } - - func delete(req: Request) async throws -> HTTPStatus { - guard let obj = try await mdlDataType.find(req.parameters.get("id"), on: req.db) else { - throw Abort(.notFound) + + + func delete(req: Request) async throws -> Response { + guard let obj = try? await mdlDataType.ObjQuery(req: req).first() else { + throw Abort(.notFound, reason: "DataType's ID was not supplied. DataType will not be deleted") + } + guard let _ = try? await obj.delete(on: req.db) else{ + throw Abort(.conflict, reason: "There was a conflict deleting DataType. Make sure it has not children.") } - try await obj.delete(on: req.db) - return .noContent + guard let response = try? await obj.encodeResponse(status: .ok, for: req) else{ + throw Abort(.noContent, reason: "Something went wrong decoding data. DataType has been deleted.") + } + return response } } + diff --git a/Sources/App/Controllers/BaseControllers/cntrlDivision.swift b/Sources/App/Controllers/BaseControllers/cntrlDivision.swift index 7aba2be..6be9d0b 100644 --- a/Sources/App/Controllers/BaseControllers/cntrlDivision.swift +++ b/Sources/App/Controllers/BaseControllers/cntrlDivision.swift @@ -1,12 +1,15 @@ // -// Division.swift +// cntrlDivision.swift // // -// Created by Michiel Carman on 4/1/24. +// Created by Michiel Carman on 4/5/2024. // import Fluent import Vapor +import Crypto + + struct cntrlDivision: RouteCollection { func boot(routes: RoutesBuilder) throws { @@ -14,34 +17,62 @@ struct cntrlDivision: RouteCollection { rts.get(use: index) rts.get(":x", use: read) rts.post(use: create) - rts.group(":x") { rt in - rts.delete(use: delete) - } - ///Add custom routes here + rts.delete(":x", use: delete) + //Additional routs added here } - func index(req: Request) async throws -> [mdlDivision] { - try await mdlDivision.ObjQuery(req: req).all() + func index(req: Request) async throws -> Response { + guard let obj = try? await mdlDivision.ObjQuery(req: req).all() else { + throw Abort(.badGateway, reason: "Something went wrong with this api.") + } + guard let response = try? await obj.encodeResponse(status: .ok, for: req) else{ + throw Abort(.noContent, reason: "Something went wrong decoding data. Array could not be shown.") + } + return response } - func read(req:Request) async throws -> mdlDivision{ - return try await mdlDivision.ObjQuery(req: req).first()! + func read(req:Request) async throws -> Response{ + guard let obj = try? await mdlDivision.ObjQuery(req: req).first() else{ + throw Abort(.notFound, reason: "Division was not found.") + } + guard let response = try? await obj.encodeResponse(status: .ok, for: req) else { + throw Abort(.noContent, reason: "Something went wrong decoding object.") + } + return response } - func create(req: Request) async throws -> mdlDivision { - let obj = try req.content.decode(mdlDivision.self) + func create(req: Request) async throws -> Response { + + guard let obj = try? req.content.decode(mdlDivision.self) else { + throw Abort(.notAcceptable, reason: "Division json not formatted correctly or is missing parents.") + } + var exists = false if(obj.id != nil){ obj._$idExists = true + exists = true + } + guard let _ = try? await obj.save(on: req.db) else{ + throw Abort(.conflict, reason: "Something conflicts in the database. Division cannot be saved.") } - try await obj.save(on: req.db) - return obj + + guard let response = try? await obj.encodeResponse(status: exists ? .accepted : .created, for: req) else{ + throw Abort(.noContent, reason: "Something went wrong decoding data. Division has been saved.") + } + return response } - func delete(req: Request) async throws -> HTTPStatus { - guard let obj = try await mdlDivision.find(req.parameters.get("id"), on: req.db) else { - throw Abort(.notFound) + + func delete(req: Request) async throws -> Response { + guard let obj = try? await mdlDivision.ObjQuery(req: req).first() else { + throw Abort(.notFound, reason: "Division's ID was not supplied. Division will not be deleted") } - try await obj.delete(on: req.db) - return .noContent + guard let _ = try? await obj.delete(on: req.db) else{ + throw Abort(.conflict, reason: "There was a conflict deleting Division. Make sure it has not children.") + } + guard let response = try? await obj.encodeResponse(status: .ok, for: req) else{ + throw Abort(.noContent, reason: "Something went wrong decoding data. Division has been deleted.") + } + return response } } + diff --git a/Sources/App/Controllers/BaseControllers/cntrlEquipmentType.swift b/Sources/App/Controllers/BaseControllers/cntrlEquipmentType.swift index ff4f32d..92f39f0 100644 --- a/Sources/App/Controllers/BaseControllers/cntrlEquipmentType.swift +++ b/Sources/App/Controllers/BaseControllers/cntrlEquipmentType.swift @@ -2,11 +2,14 @@ // cntrlEquipmentType.swift // // -// Created by Michiel Carman on 4/1/24. +// Created by Michiel Carman on 4/5/2024. // import Fluent import Vapor +import Crypto + + struct cntrlEquipmentType: RouteCollection { func boot(routes: RoutesBuilder) throws { @@ -14,34 +17,62 @@ struct cntrlEquipmentType: RouteCollection { rts.get(use: index) rts.get(":x", use: read) rts.post(use: create) - rts.group(":x") { rt in - rts.delete(use: delete) - } - ///Add custom routes here + rts.delete(":x", use: delete) + //Additional routs added here } - func index(req: Request) async throws -> [mdlEquipmentType] { - try await mdlEquipmentType.ObjQuery(req: req).all() + func index(req: Request) async throws -> Response { + guard let obj = try? await mdlEquipmentType.ObjQuery(req: req).all() else { + throw Abort(.badGateway, reason: "Something went wrong with this api.") + } + guard let response = try? await obj.encodeResponse(status: .ok, for: req) else{ + throw Abort(.noContent, reason: "Something went wrong decoding data. Array could not be shown.") + } + return response } - func read(req:Request) async throws -> mdlEquipmentType{ - return try await mdlEquipmentType.ObjQuery(req: req).first()! + func read(req:Request) async throws -> Response{ + guard let obj = try? await mdlEquipmentType.ObjQuery(req: req).first() else{ + throw Abort(.notFound, reason: "EquipmentType was not found.") + } + guard let response = try? await obj.encodeResponse(status: .ok, for: req) else { + throw Abort(.noContent, reason: "Something went wrong decoding object.") + } + return response } - func create(req: Request) async throws -> mdlEquipmentType { - let obj = try req.content.decode(mdlEquipmentType.self) + func create(req: Request) async throws -> Response { + + guard let obj = try? req.content.decode(mdlEquipmentType.self) else { + throw Abort(.notAcceptable, reason: "EquipmentType json not formatted correctly or is missing parents.") + } + var exists = false if(obj.id != nil){ obj._$idExists = true + exists = true + } + guard let _ = try? await obj.save(on: req.db) else{ + throw Abort(.conflict, reason: "Something conflicts in the database. EquipmentType cannot be saved.") } - try await obj.save(on: req.db) - return obj + + guard let response = try? await obj.encodeResponse(status: exists ? .accepted : .created, for: req) else{ + throw Abort(.noContent, reason: "Something went wrong decoding data. EquipmentType has been saved.") + } + return response } - func delete(req: Request) async throws -> HTTPStatus { - guard let obj = try await mdlEquipmentType.find(req.parameters.get("id"), on: req.db) else { - throw Abort(.notFound) + + func delete(req: Request) async throws -> Response { + guard let obj = try? await mdlEquipmentType.ObjQuery(req: req).first() else { + throw Abort(.notFound, reason: "EquipmentType's ID was not supplied. EquipmentType will not be deleted") } - try await obj.delete(on: req.db) - return .noContent + guard let _ = try? await obj.delete(on: req.db) else{ + throw Abort(.conflict, reason: "There was a conflict deleting EquipmentType. Make sure it has not children.") + } + guard let response = try? await obj.encodeResponse(status: .ok, for: req) else{ + throw Abort(.noContent, reason: "Something went wrong decoding data. EquipmentType has been deleted.") + } + return response } } + diff --git a/Sources/App/Controllers/BaseControllers/cntrlFluid.swift b/Sources/App/Controllers/BaseControllers/cntrlFluid.swift index bba7b9e..7546aa0 100644 --- a/Sources/App/Controllers/BaseControllers/cntrlFluid.swift +++ b/Sources/App/Controllers/BaseControllers/cntrlFluid.swift @@ -2,11 +2,14 @@ // cntrlFluid.swift // // -// Created by Michiel Carman on 4/1/24. +// Created by Michiel Carman on 4/5/2024. // import Fluent import Vapor +import Crypto + + struct cntrlFluid: RouteCollection { func boot(routes: RoutesBuilder) throws { @@ -14,34 +17,62 @@ struct cntrlFluid: RouteCollection { rts.get(use: index) rts.get(":x", use: read) rts.post(use: create) - rts.group(":x") { rt in - rts.delete(use: delete) - } - ///Add custom routes here + rts.delete(":x", use: delete) + //Additional routs added here } - func index(req: Request) async throws -> [mdlFluid] { - try await mdlFluid.ObjQuery(req: req).all() + func index(req: Request) async throws -> Response { + guard let obj = try? await mdlFluid.ObjQuery(req: req).all() else { + throw Abort(.badGateway, reason: "Something went wrong with this api.") + } + guard let response = try? await obj.encodeResponse(status: .ok, for: req) else{ + throw Abort(.noContent, reason: "Something went wrong decoding data. Array could not be shown.") + } + return response } - func read(req:Request) async throws -> mdlFluid{ - return try await mdlFluid.ObjQuery(req: req).first()! + func read(req:Request) async throws -> Response{ + guard let obj = try? await mdlFluid.ObjQuery(req: req).first() else{ + throw Abort(.notFound, reason: "Fluid was not found.") + } + guard let response = try? await obj.encodeResponse(status: .ok, for: req) else { + throw Abort(.noContent, reason: "Something went wrong decoding object.") + } + return response } - func create(req: Request) async throws -> mdlFluid { - let obj = try req.content.decode(mdlFluid.self) + func create(req: Request) async throws -> Response { + + guard let obj = try? req.content.decode(mdlFluid.self) else { + throw Abort(.notAcceptable, reason: "Fluid json not formatted correctly or is missing parents.") + } + var exists = false if(obj.id != nil){ obj._$idExists = true + exists = true + } + guard let _ = try? await obj.save(on: req.db) else{ + throw Abort(.conflict, reason: "Something conflicts in the database. Fluid cannot be saved.") } - try await obj.save(on: req.db) - return obj + + guard let response = try? await obj.encodeResponse(status: exists ? .accepted : .created, for: req) else{ + throw Abort(.noContent, reason: "Something went wrong decoding data. Fluid has been saved.") + } + return response } - func delete(req: Request) async throws -> HTTPStatus { - guard let obj = try await mdlFluid.find(req.parameters.get("id"), on: req.db) else { - throw Abort(.notFound) + + func delete(req: Request) async throws -> Response { + guard let obj = try? await mdlFluid.ObjQuery(req: req).first() else { + throw Abort(.notFound, reason: "Fluid's ID was not supplied. Fluid will not be deleted") } - try await obj.delete(on: req.db) - return .noContent + guard let _ = try? await obj.delete(on: req.db) else{ + throw Abort(.conflict, reason: "There was a conflict deleting Fluid. Make sure it has not children.") + } + guard let response = try? await obj.encodeResponse(status: .ok, for: req) else{ + throw Abort(.noContent, reason: "Something went wrong decoding data. Fluid has been deleted.") + } + return response } } + diff --git a/Sources/App/Controllers/BaseControllers/cntrlFuel.swift b/Sources/App/Controllers/BaseControllers/cntrlFuel.swift index cba15f0..ddfb221 100644 --- a/Sources/App/Controllers/BaseControllers/cntrlFuel.swift +++ b/Sources/App/Controllers/BaseControllers/cntrlFuel.swift @@ -2,11 +2,14 @@ // cntrlFuel.swift // // -// Created by Michiel Carman on 4/1/24. +// Created by Michiel Carman on 4/5/2024. // import Fluent import Vapor +import Crypto + + struct cntrlFuel: RouteCollection { func boot(routes: RoutesBuilder) throws { @@ -14,34 +17,62 @@ struct cntrlFuel: RouteCollection { rts.get(use: index) rts.get(":x", use: read) rts.post(use: create) - rts.group(":x") { rt in - rts.delete(use: delete) - } - ///Add custom routes here + rts.delete(":x", use: delete) + //Additional routs added here } - func index(req: Request) async throws -> [mdlFuel] { - try await mdlFuel.ObjQuery(req: req).all() + func index(req: Request) async throws -> Response { + guard let obj = try? await mdlFuel.ObjQuery(req: req).all() else { + throw Abort(.badGateway, reason: "Something went wrong with this api.") + } + guard let response = try? await obj.encodeResponse(status: .ok, for: req) else{ + throw Abort(.noContent, reason: "Something went wrong decoding data. Array could not be shown.") + } + return response } - func read(req:Request) async throws -> mdlFuel{ - return try await mdlFuel.ObjQuery(req: req).first()! + func read(req:Request) async throws -> Response{ + guard let obj = try? await mdlFuel.ObjQuery(req: req).first() else{ + throw Abort(.notFound, reason: "Fuel was not found.") + } + guard let response = try? await obj.encodeResponse(status: .ok, for: req) else { + throw Abort(.noContent, reason: "Something went wrong decoding object.") + } + return response } - func create(req: Request) async throws -> mdlFuel { - let obj = try req.content.decode(mdlFuel.self) + func create(req: Request) async throws -> Response { + + guard let obj = try? req.content.decode(mdlFuel.self) else { + throw Abort(.notAcceptable, reason: "Fuel json not formatted correctly or is missing parents.") + } + var exists = false if(obj.id != nil){ obj._$idExists = true + exists = true + } + guard let _ = try? await obj.save(on: req.db) else{ + throw Abort(.conflict, reason: "Something conflicts in the database. Fuel cannot be saved.") } - try await obj.save(on: req.db) - return obj + + guard let response = try? await obj.encodeResponse(status: exists ? .accepted : .created, for: req) else{ + throw Abort(.noContent, reason: "Something went wrong decoding data. Fuel has been saved.") + } + return response } - func delete(req: Request) async throws -> HTTPStatus { - guard let obj = try await mdlFuel.find(req.parameters.get("id"), on: req.db) else { - throw Abort(.notFound) + + func delete(req: Request) async throws -> Response { + guard let obj = try? await mdlFuel.ObjQuery(req: req).first() else { + throw Abort(.notFound, reason: "Fuel's ID was not supplied. Fuel will not be deleted") } - try await obj.delete(on: req.db) - return .noContent + guard let _ = try? await obj.delete(on: req.db) else{ + throw Abort(.conflict, reason: "There was a conflict deleting Fuel. Make sure it has not children.") + } + guard let response = try? await obj.encodeResponse(status: .ok, for: req) else{ + throw Abort(.noContent, reason: "Something went wrong decoding data. Fuel has been deleted.") + } + return response } } + diff --git a/Sources/App/Controllers/BaseControllers/cntrlGate.swift b/Sources/App/Controllers/BaseControllers/cntrlGate.swift index 1554327..f0b8669 100644 --- a/Sources/App/Controllers/BaseControllers/cntrlGate.swift +++ b/Sources/App/Controllers/BaseControllers/cntrlGate.swift @@ -1,12 +1,15 @@ // -// Gate.swift +// cntrlGate.swift // // -// Created by Michiel Carman on 4/1/24. +// Created by Michiel Carman on 4/5/2024. // import Fluent import Vapor +import Crypto + + struct cntrlGate: RouteCollection { func boot(routes: RoutesBuilder) throws { @@ -14,34 +17,62 @@ struct cntrlGate: RouteCollection { rts.get(use: index) rts.get(":x", use: read) rts.post(use: create) - rts.group(":x") { rt in - rts.delete(use: delete) - } - ///Add custom routes here + rts.delete(":x", use: delete) + //Additional routs added here } - func index(req: Request) async throws -> [mdlGate] { - try await mdlGate.ObjQuery(req: req).all() + func index(req: Request) async throws -> Response { + guard let obj = try? await mdlGate.ObjQuery(req: req).all() else { + throw Abort(.badGateway, reason: "Something went wrong with this api.") + } + guard let response = try? await obj.encodeResponse(status: .ok, for: req) else{ + throw Abort(.noContent, reason: "Something went wrong decoding data. Array could not be shown.") + } + return response } - func read(req:Request) async throws -> mdlGate{ - return try await mdlGate.ObjQuery(req: req).first()! + func read(req:Request) async throws -> Response{ + guard let obj = try? await mdlGate.ObjQuery(req: req).first() else{ + throw Abort(.notFound, reason: "Gate was not found.") + } + guard let response = try? await obj.encodeResponse(status: .ok, for: req) else { + throw Abort(.noContent, reason: "Something went wrong decoding object.") + } + return response } - func create(req: Request) async throws -> mdlGate { - let obj = try req.content.decode(mdlGate.self) + func create(req: Request) async throws -> Response { + + guard let obj = try? req.content.decode(mdlGate.self) else { + throw Abort(.notAcceptable, reason: "Gate json not formatted correctly or is missing parents.") + } + var exists = false if(obj.id != nil){ obj._$idExists = true + exists = true + } + guard let _ = try? await obj.save(on: req.db) else{ + throw Abort(.conflict, reason: "Something conflicts in the database. Gate cannot be saved.") } - try await obj.save(on: req.db) - return obj + + guard let response = try? await obj.encodeResponse(status: exists ? .accepted : .created, for: req) else{ + throw Abort(.noContent, reason: "Something went wrong decoding data. Gate has been saved.") + } + return response } - func delete(req: Request) async throws -> HTTPStatus { - guard let obj = try await mdlGate.find(req.parameters.get("id"), on: req.db) else { - throw Abort(.notFound) + + func delete(req: Request) async throws -> Response { + guard let obj = try? await mdlGate.ObjQuery(req: req).first() else { + throw Abort(.notFound, reason: "Gate's ID was not supplied. Gate will not be deleted") } - try await obj.delete(on: req.db) - return .noContent + guard let _ = try? await obj.delete(on: req.db) else{ + throw Abort(.conflict, reason: "There was a conflict deleting Gate. Make sure it has not children.") + } + guard let response = try? await obj.encodeResponse(status: .ok, for: req) else{ + throw Abort(.noContent, reason: "Something went wrong decoding data. Gate has been deleted.") + } + return response } } + diff --git a/Sources/App/Controllers/BaseControllers/cntrlIncompleteReason.swift b/Sources/App/Controllers/BaseControllers/cntrlIncompleteReason.swift index 2b32876..298f2be 100644 --- a/Sources/App/Controllers/BaseControllers/cntrlIncompleteReason.swift +++ b/Sources/App/Controllers/BaseControllers/cntrlIncompleteReason.swift @@ -2,11 +2,14 @@ // cntrlIncompleteReason.swift // // -// Created by Michiel Carman on 4/1/24. +// Created by Michiel Carman on 4/5/2024. // import Fluent import Vapor +import Crypto + + struct cntrlIncompleteReason: RouteCollection { func boot(routes: RoutesBuilder) throws { @@ -14,31 +17,62 @@ struct cntrlIncompleteReason: RouteCollection { rts.get(use: index) rts.get(":x", use: read) rts.post(use: create) - rts.group(":x") { rt in - rts.delete(use: delete) - } - ///Add custom routes here + rts.delete(":x", use: delete) + //Additional routs added here } - func index(req: Request) async throws -> [mdlIncompleteReason] { - try await mdlIncompleteReason.ObjQuery(req: req).all() + func index(req: Request) async throws -> Response { + guard let obj = try? await mdlIncompleteReason.ObjQuery(req: req).all() else { + throw Abort(.badGateway, reason: "Something went wrong with this api.") + } + guard let response = try? await obj.encodeResponse(status: .ok, for: req) else{ + throw Abort(.noContent, reason: "Something went wrong decoding data. Array could not be shown.") + } + return response } - func read(req:Request) async throws -> mdlIncompleteReason{ - return try await mdlIncompleteReason.ObjQuery(req: req).first()! + func read(req:Request) async throws -> Response{ + guard let obj = try? await mdlIncompleteReason.ObjQuery(req: req).first() else{ + throw Abort(.notFound, reason: "IncompleteReason was not found.") + } + guard let response = try? await obj.encodeResponse(status: .ok, for: req) else { + throw Abort(.noContent, reason: "Something went wrong decoding object.") + } + return response } - func create(req: Request) async throws -> mdlIncompleteReason { - let obj = try req.content.decode(mdlIncompleteReason.self) - try await obj.save(on: req.db) - return obj + func create(req: Request) async throws -> Response { + + guard let obj = try? req.content.decode(mdlIncompleteReason.self) else { + throw Abort(.notAcceptable, reason: "IncompleteReason json not formatted correctly or is missing parents.") + } + var exists = false + if(obj.id != nil){ + obj._$idExists = true + exists = true + } + guard let _ = try? await obj.save(on: req.db) else{ + throw Abort(.conflict, reason: "Something conflicts in the database. IncompleteReason cannot be saved.") + } + + guard let response = try? await obj.encodeResponse(status: exists ? .accepted : .created, for: req) else{ + throw Abort(.noContent, reason: "Something went wrong decoding data. IncompleteReason has been saved.") + } + return response } - func delete(req: Request) async throws -> HTTPStatus { - guard let obj = try await mdlIncompleteReason.find(req.parameters.get("id"), on: req.db) else { - throw Abort(.notFound) + + func delete(req: Request) async throws -> Response { + guard let obj = try? await mdlIncompleteReason.ObjQuery(req: req).first() else { + throw Abort(.notFound, reason: "IncompleteReason's ID was not supplied. IncompleteReason will not be deleted") } - try await obj.delete(on: req.db) - return .noContent + guard let _ = try? await obj.delete(on: req.db) else{ + throw Abort(.conflict, reason: "There was a conflict deleting IncompleteReason. Make sure it has not children.") + } + guard let response = try? await obj.encodeResponse(status: .ok, for: req) else{ + throw Abort(.noContent, reason: "Something went wrong decoding data. IncompleteReason has been deleted.") + } + return response } } + diff --git a/Sources/App/Controllers/BaseControllers/cntrlInputType.swift b/Sources/App/Controllers/BaseControllers/cntrlInputType.swift index f466472..50bd852 100644 --- a/Sources/App/Controllers/BaseControllers/cntrlInputType.swift +++ b/Sources/App/Controllers/BaseControllers/cntrlInputType.swift @@ -2,11 +2,14 @@ // cntrlInputType.swift // // -// Created by Michiel Carman on 4/1/24. +// Created by Michiel Carman on 4/5/2024. // import Fluent import Vapor +import Crypto + + struct cntrlInputType: RouteCollection { func boot(routes: RoutesBuilder) throws { @@ -14,34 +17,62 @@ struct cntrlInputType: RouteCollection { rts.get(use: index) rts.get(":x", use: read) rts.post(use: create) - rts.group(":x") { rt in - rts.delete(use: delete) - } - ///Add custom routes here + rts.delete(":x", use: delete) + //Additional routs added here } - func index(req: Request) async throws -> [mdlInputType] { - try await mdlInputType.ObjQuery(req: req).all() + func index(req: Request) async throws -> Response { + guard let obj = try? await mdlInputType.ObjQuery(req: req).all() else { + throw Abort(.badGateway, reason: "Something went wrong with this api.") + } + guard let response = try? await obj.encodeResponse(status: .ok, for: req) else{ + throw Abort(.noContent, reason: "Something went wrong decoding data. Array could not be shown.") + } + return response } - func read(req:Request) async throws -> mdlInputType{ - return try await mdlInputType.ObjQuery(req: req).first()! + func read(req:Request) async throws -> Response{ + guard let obj = try? await mdlInputType.ObjQuery(req: req).first() else{ + throw Abort(.notFound, reason: "InputType was not found.") + } + guard let response = try? await obj.encodeResponse(status: .ok, for: req) else { + throw Abort(.noContent, reason: "Something went wrong decoding object.") + } + return response } - func create(req: Request) async throws -> mdlInputType { - let obj = try req.content.decode(mdlInputType.self) + func create(req: Request) async throws -> Response { + + guard let obj = try? req.content.decode(mdlInputType.self) else { + throw Abort(.notAcceptable, reason: "InputType json not formatted correctly or is missing parents.") + } + var exists = false if(obj.id != nil){ obj._$idExists = true + exists = true + } + guard let _ = try? await obj.save(on: req.db) else{ + throw Abort(.conflict, reason: "Something conflicts in the database. InputType cannot be saved.") } - try await obj.save(on: req.db) - return obj + + guard let response = try? await obj.encodeResponse(status: exists ? .accepted : .created, for: req) else{ + throw Abort(.noContent, reason: "Something went wrong decoding data. InputType has been saved.") + } + return response } - func delete(req: Request) async throws -> HTTPStatus { - guard let obj = try await mdlInputType.find(req.parameters.get("id"), on: req.db) else { - throw Abort(.notFound) + + func delete(req: Request) async throws -> Response { + guard let obj = try? await mdlInputType.ObjQuery(req: req).first() else { + throw Abort(.notFound, reason: "InputType's ID was not supplied. InputType will not be deleted") } - try await obj.delete(on: req.db) - return .noContent + guard let _ = try? await obj.delete(on: req.db) else{ + throw Abort(.conflict, reason: "There was a conflict deleting InputType. Make sure it has not children.") + } + guard let response = try? await obj.encodeResponse(status: .ok, for: req) else{ + throw Abort(.noContent, reason: "Something went wrong decoding data. InputType has been deleted.") + } + return response } } + diff --git a/Sources/App/Controllers/BaseControllers/cntrlJobSite.swift b/Sources/App/Controllers/BaseControllers/cntrlJobSite.swift index 4b92151..b171e96 100644 --- a/Sources/App/Controllers/BaseControllers/cntrlJobSite.swift +++ b/Sources/App/Controllers/BaseControllers/cntrlJobSite.swift @@ -2,11 +2,14 @@ // cntrlJobSite.swift // // -// Created by Michiel Carman on 4/1/24. +// Created by Michiel Carman on 4/5/2024. // import Fluent import Vapor +import Crypto + + struct cntrlJobSite: RouteCollection { func boot(routes: RoutesBuilder) throws { @@ -14,34 +17,62 @@ struct cntrlJobSite: RouteCollection { rts.get(use: index) rts.get(":x", use: read) rts.post(use: create) - rts.group(":x") { rt in - rts.delete(use: delete) - } - ///Add custom routes here + rts.delete(":x", use: delete) + //Additional routs added here } - func index(req: Request) async throws -> [mdlJobSite] { - try await mdlJobSite.ObjQuery(req: req).all() + func index(req: Request) async throws -> Response { + guard let obj = try? await mdlJobSite.ObjQuery(req: req).all() else { + throw Abort(.badGateway, reason: "Something went wrong with this api.") + } + guard let response = try? await obj.encodeResponse(status: .ok, for: req) else{ + throw Abort(.noContent, reason: "Something went wrong decoding data. Array could not be shown.") + } + return response } - func read(req:Request) async throws -> mdlJobSite{ - return try await mdlJobSite.ObjQuery(req: req).first()! + func read(req:Request) async throws -> Response{ + guard let obj = try? await mdlJobSite.ObjQuery(req: req).first() else{ + throw Abort(.notFound, reason: "JobSite was not found.") + } + guard let response = try? await obj.encodeResponse(status: .ok, for: req) else { + throw Abort(.noContent, reason: "Something went wrong decoding object.") + } + return response } - func create(req: Request) async throws -> mdlJobSite { - let obj = try req.content.decode(mdlJobSite.self) + func create(req: Request) async throws -> Response { + + guard let obj = try? req.content.decode(mdlJobSite.self) else { + throw Abort(.notAcceptable, reason: "JobSite json not formatted correctly or is missing parents.") + } + var exists = false if(obj.id != nil){ obj._$idExists = true + exists = true + } + guard let _ = try? await obj.save(on: req.db) else{ + throw Abort(.conflict, reason: "Something conflicts in the database. JobSite cannot be saved.") } - try await obj.save(on: req.db) - return obj + + guard let response = try? await obj.encodeResponse(status: exists ? .accepted : .created, for: req) else{ + throw Abort(.noContent, reason: "Something went wrong decoding data. JobSite has been saved.") + } + return response } - func delete(req: Request) async throws -> HTTPStatus { - guard let obj = try await mdlJobSite.find(req.parameters.get("id"), on: req.db) else { - throw Abort(.notFound) + + func delete(req: Request) async throws -> Response { + guard let obj = try? await mdlJobSite.ObjQuery(req: req).first() else { + throw Abort(.notFound, reason: "JobSite's ID was not supplied. JobSite will not be deleted") } - try await obj.delete(on: req.db) - return .noContent + guard let _ = try? await obj.delete(on: req.db) else{ + throw Abort(.conflict, reason: "There was a conflict deleting JobSite. Make sure it has not children.") + } + guard let response = try? await obj.encodeResponse(status: .ok, for: req) else{ + throw Abort(.noContent, reason: "Something went wrong decoding data. JobSite has been deleted.") + } + return response } } + diff --git a/Sources/App/Controllers/BaseControllers/cntrlLevel.swift b/Sources/App/Controllers/BaseControllers/cntrlLevel.swift index 93bcc0b..5923801 100644 --- a/Sources/App/Controllers/BaseControllers/cntrlLevel.swift +++ b/Sources/App/Controllers/BaseControllers/cntrlLevel.swift @@ -2,11 +2,14 @@ // cntrlLevel.swift // // -// Created by Michiel Carman on 4/1/24. +// Created by Michiel Carman on 4/5/2024. // import Fluent import Vapor +import Crypto + + struct cntrlLevel: RouteCollection { func boot(routes: RoutesBuilder) throws { @@ -14,34 +17,62 @@ struct cntrlLevel: RouteCollection { rts.get(use: index) rts.get(":x", use: read) rts.post(use: create) - rts.group(":x") { rt in - rts.delete(use: delete) - } - ///Add custom routes here + rts.delete(":x", use: delete) + //Additional routs added here } - func index(req: Request) async throws -> [mdlLevel] { - try await mdlLevel.query(on: req.db).all() + func index(req: Request) async throws -> Response { + guard let obj = try? await mdlLevel.ObjQuery(req: req).all() else { + throw Abort(.badGateway, reason: "Something went wrong with this api.") + } + guard let response = try? await obj.encodeResponse(status: .ok, for: req) else{ + throw Abort(.noContent, reason: "Something went wrong decoding data. Array could not be shown.") + } + return response } - func read(req:Request) async throws -> mdlLevel{ - return try await mdlLevel.ObjQuery(req: req).first()! + func read(req:Request) async throws -> Response{ + guard let obj = try? await mdlLevel.ObjQuery(req: req).first() else{ + throw Abort(.notFound, reason: "Level was not found.") + } + guard let response = try? await obj.encodeResponse(status: .ok, for: req) else { + throw Abort(.noContent, reason: "Something went wrong decoding object.") + } + return response } - func create(req: Request) async throws -> mdlLevel { - let obj = try req.content.decode(mdlLevel.self) + func create(req: Request) async throws -> Response { + + guard let obj = try? req.content.decode(mdlLevel.self) else { + throw Abort(.notAcceptable, reason: "Level json not formatted correctly or is missing parents.") + } + var exists = false if(obj.id != nil){ obj._$idExists = true + exists = true + } + guard let _ = try? await obj.save(on: req.db) else{ + throw Abort(.conflict, reason: "Something conflicts in the database. Level cannot be saved.") } - try await obj.save(on: req.db) - return obj + + guard let response = try? await obj.encodeResponse(status: exists ? .accepted : .created, for: req) else{ + throw Abort(.noContent, reason: "Something went wrong decoding data. Level has been saved.") + } + return response } - func delete(req: Request) async throws -> HTTPStatus { - guard let obj = try await mdlLevel.find(req.parameters.get("id"), on: req.db) else { - throw Abort(.notFound) + + func delete(req: Request) async throws -> Response { + guard let obj = try? await mdlLevel.ObjQuery(req: req).first() else { + throw Abort(.notFound, reason: "Level's ID was not supplied. Level will not be deleted") } - try await obj.delete(on: req.db) - return .noContent + guard let _ = try? await obj.delete(on: req.db) else{ + throw Abort(.conflict, reason: "There was a conflict deleting Level. Make sure it has not children.") + } + guard let response = try? await obj.encodeResponse(status: .ok, for: req) else{ + throw Abort(.noContent, reason: "Something went wrong decoding data. Level has been deleted.") + } + return response } } + diff --git a/Sources/App/Controllers/BaseControllers/cntrlLocation.swift b/Sources/App/Controllers/BaseControllers/cntrlLocation.swift index 2f3ae1d..98ddb3b 100644 --- a/Sources/App/Controllers/BaseControllers/cntrlLocation.swift +++ b/Sources/App/Controllers/BaseControllers/cntrlLocation.swift @@ -2,11 +2,14 @@ // cntrlLocation.swift // // -// Created by Michiel Carman on 4/1/24. +// Created by Michiel Carman on 4/5/2024. // import Fluent import Vapor +import Crypto + + struct cntrlLocation: RouteCollection { func boot(routes: RoutesBuilder) throws { @@ -14,34 +17,62 @@ struct cntrlLocation: RouteCollection { rts.get(use: index) rts.get(":x", use: read) rts.post(use: create) - rts.group(":x") { rt in - rts.delete(use: delete) - } - ///Add custom routes here + rts.delete(":x", use: delete) + //Additional routs added here } - func index(req: Request) async throws -> [mdlLocation] { - try await mdlLocation.ObjQuery(req: req).all() + func index(req: Request) async throws -> Response { + guard let obj = try? await mdlLocation.ObjQuery(req: req).all() else { + throw Abort(.badGateway, reason: "Something went wrong with this api.") + } + guard let response = try? await obj.encodeResponse(status: .ok, for: req) else{ + throw Abort(.noContent, reason: "Something went wrong decoding data. Array could not be shown.") + } + return response } - func read(req:Request) async throws -> mdlLocation{ - return try await mdlLocation.ObjQuery(req: req).first()! + func read(req:Request) async throws -> Response{ + guard let obj = try? await mdlLocation.ObjQuery(req: req).first() else{ + throw Abort(.notFound, reason: "Location was not found.") + } + guard let response = try? await obj.encodeResponse(status: .ok, for: req) else { + throw Abort(.noContent, reason: "Something went wrong decoding object.") + } + return response } - func create(req: Request) async throws -> mdlLocation { - let obj = try req.content.decode(mdlLocation.self) + func create(req: Request) async throws -> Response { + + guard let obj = try? req.content.decode(mdlLocation.self) else { + throw Abort(.notAcceptable, reason: "Location json not formatted correctly or is missing parents.") + } + var exists = false if(obj.id != nil){ obj._$idExists = true + exists = true + } + guard let _ = try? await obj.save(on: req.db) else{ + throw Abort(.conflict, reason: "Something conflicts in the database. Location cannot be saved.") } - try await obj.save(on: req.db) - return obj + + guard let response = try? await obj.encodeResponse(status: exists ? .accepted : .created, for: req) else{ + throw Abort(.noContent, reason: "Something went wrong decoding data. Location has been saved.") + } + return response } - func delete(req: Request) async throws -> HTTPStatus { - guard let obj = try await mdlLocation.find(req.parameters.get("id"), on: req.db) else { - throw Abort(.notFound) + + func delete(req: Request) async throws -> Response { + guard let obj = try? await mdlLocation.ObjQuery(req: req).first() else { + throw Abort(.notFound, reason: "Location's ID was not supplied. Location will not be deleted") } - try await obj.delete(on: req.db) - return .noContent + guard let _ = try? await obj.delete(on: req.db) else{ + throw Abort(.conflict, reason: "There was a conflict deleting Location. Make sure it has not children.") + } + guard let response = try? await obj.encodeResponse(status: .ok, for: req) else{ + throw Abort(.noContent, reason: "Something went wrong decoding data. Location has been deleted.") + } + return response } } + diff --git a/Sources/App/Controllers/BaseControllers/cntrlNoteType.swift b/Sources/App/Controllers/BaseControllers/cntrlNoteType.swift index 15c613d..ec1f929 100644 --- a/Sources/App/Controllers/BaseControllers/cntrlNoteType.swift +++ b/Sources/App/Controllers/BaseControllers/cntrlNoteType.swift @@ -2,11 +2,14 @@ // cntrlNoteType.swift // // -// Created by Michiel Carman on 4/1/24. +// Created by Michiel Carman on 4/5/2024. // import Fluent import Vapor +import Crypto + + struct cntrlNoteType: RouteCollection { func boot(routes: RoutesBuilder) throws { @@ -14,34 +17,62 @@ struct cntrlNoteType: RouteCollection { rts.get(use: index) rts.get(":x", use: read) rts.post(use: create) - rts.group(":x") { rt in - rts.delete(use: delete) - } - ///Add custom routes here + rts.delete(":x", use: delete) + //Additional routs added here } - func index(req: Request) async throws -> [mdlNoteType] { - try await mdlNoteType.ObjQuery(req: req).all() + func index(req: Request) async throws -> Response { + guard let obj = try? await mdlNoteType.ObjQuery(req: req).all() else { + throw Abort(.badGateway, reason: "Something went wrong with this api.") + } + guard let response = try? await obj.encodeResponse(status: .ok, for: req) else{ + throw Abort(.noContent, reason: "Something went wrong decoding data. Array could not be shown.") + } + return response } - func read(req:Request) async throws -> mdlNoteType{ - return try await mdlNoteType.ObjQuery(req: req).first()! + func read(req:Request) async throws -> Response{ + guard let obj = try? await mdlNoteType.ObjQuery(req: req).first() else{ + throw Abort(.notFound, reason: "NoteType was not found.") + } + guard let response = try? await obj.encodeResponse(status: .ok, for: req) else { + throw Abort(.noContent, reason: "Something went wrong decoding object.") + } + return response } - func create(req: Request) async throws -> mdlNoteType { - let obj = try req.content.decode(mdlNoteType.self) + func create(req: Request) async throws -> Response { + + guard let obj = try? req.content.decode(mdlNoteType.self) else { + throw Abort(.notAcceptable, reason: "NoteType json not formatted correctly or is missing parents.") + } + var exists = false if(obj.id != nil){ obj._$idExists = true + exists = true + } + guard let _ = try? await obj.save(on: req.db) else{ + throw Abort(.conflict, reason: "Something conflicts in the database. NoteType cannot be saved.") } - try await obj.save(on: req.db) - return obj + + guard let response = try? await obj.encodeResponse(status: exists ? .accepted : .created, for: req) else{ + throw Abort(.noContent, reason: "Something went wrong decoding data. NoteType has been saved.") + } + return response } - func delete(req: Request) async throws -> HTTPStatus { - guard let obj = try await mdlNoteType.find(req.parameters.get("id"), on: req.db) else { - throw Abort(.notFound) + + func delete(req: Request) async throws -> Response { + guard let obj = try? await mdlNoteType.ObjQuery(req: req).first() else { + throw Abort(.notFound, reason: "NoteType's ID was not supplied. NoteType will not be deleted") } - try await obj.delete(on: req.db) - return .noContent + guard let _ = try? await obj.delete(on: req.db) else{ + throw Abort(.conflict, reason: "There was a conflict deleting NoteType. Make sure it has not children.") + } + guard let response = try? await obj.encodeResponse(status: .ok, for: req) else{ + throw Abort(.noContent, reason: "Something went wrong decoding data. NoteType has been deleted.") + } + return response } } + diff --git a/Sources/App/Controllers/BaseControllers/cntrlPart.swift b/Sources/App/Controllers/BaseControllers/cntrlPart.swift index ee123b3..63254be 100644 --- a/Sources/App/Controllers/BaseControllers/cntrlPart.swift +++ b/Sources/App/Controllers/BaseControllers/cntrlPart.swift @@ -2,11 +2,14 @@ // cntrlPart.swift // // -// Created by Michiel Carman on 4/1/24. +// Created by Michiel Carman on 4/5/2024. // import Fluent import Vapor +import Crypto + + struct cntrlPart: RouteCollection { func boot(routes: RoutesBuilder) throws { @@ -14,34 +17,62 @@ struct cntrlPart: RouteCollection { rts.get(use: index) rts.get(":x", use: read) rts.post(use: create) - rts.group(":x") { rt in - rts.delete(use: delete) - } - ///Add custom routes here + rts.delete(":x", use: delete) + //Additional routs added here } - func index(req: Request) async throws -> [mdlPart] { - try await mdlPart.ObjQuery(req: req).all() + func index(req: Request) async throws -> Response { + guard let obj = try? await mdlPart.ObjQuery(req: req).all() else { + throw Abort(.badGateway, reason: "Something went wrong with this api.") + } + guard let response = try? await obj.encodeResponse(status: .ok, for: req) else{ + throw Abort(.noContent, reason: "Something went wrong decoding data. Array could not be shown.") + } + return response } - func read(req:Request) async throws -> mdlPart{ - return try await mdlPart.ObjQuery(req: req).first()! + func read(req:Request) async throws -> Response{ + guard let obj = try? await mdlPart.ObjQuery(req: req).first() else{ + throw Abort(.notFound, reason: "Part was not found.") + } + guard let response = try? await obj.encodeResponse(status: .ok, for: req) else { + throw Abort(.noContent, reason: "Something went wrong decoding object.") + } + return response } - func create(req: Request) async throws -> mdlPart { - let obj = try req.content.decode(mdlPart.self) + func create(req: Request) async throws -> Response { + + guard let obj = try? req.content.decode(mdlPart.self) else { + throw Abort(.notAcceptable, reason: "Part json not formatted correctly or is missing parents.") + } + var exists = false if(obj.id != nil){ obj._$idExists = true + exists = true + } + guard let _ = try? await obj.save(on: req.db) else{ + throw Abort(.conflict, reason: "Something conflicts in the database. Part cannot be saved.") } - try await obj.save(on: req.db) - return obj + + guard let response = try? await obj.encodeResponse(status: exists ? .accepted : .created, for: req) else{ + throw Abort(.noContent, reason: "Something went wrong decoding data. Part has been saved.") + } + return response } - func delete(req: Request) async throws -> HTTPStatus { - guard let obj = try await mdlPart.find(req.parameters.get("id"), on: req.db) else { - throw Abort(.notFound) + + func delete(req: Request) async throws -> Response { + guard let obj = try? await mdlPart.ObjQuery(req: req).first() else { + throw Abort(.notFound, reason: "Part's ID was not supplied. Part will not be deleted") } - try await obj.delete(on: req.db) - return .noContent + guard let _ = try? await obj.delete(on: req.db) else{ + throw Abort(.conflict, reason: "There was a conflict deleting Part. Make sure it has not children.") + } + guard let response = try? await obj.encodeResponse(status: .ok, for: req) else{ + throw Abort(.noContent, reason: "Something went wrong decoding data. Part has been deleted.") + } + return response } } + diff --git a/Sources/App/Controllers/BaseControllers/cntrlPriority.swift b/Sources/App/Controllers/BaseControllers/cntrlPriority.swift index 37d9194..39b77ee 100644 --- a/Sources/App/Controllers/BaseControllers/cntrlPriority.swift +++ b/Sources/App/Controllers/BaseControllers/cntrlPriority.swift @@ -2,11 +2,14 @@ // cntrlPriority.swift // // -// Created by Michiel Carman on 4/1/24. +// Created by Michiel Carman on 4/5/2024. // import Fluent import Vapor +import Crypto + + struct cntrlPriority: RouteCollection { func boot(routes: RoutesBuilder) throws { @@ -14,34 +17,62 @@ struct cntrlPriority: RouteCollection { rts.get(use: index) rts.get(":x", use: read) rts.post(use: create) - rts.group(":x") { rt in - rts.delete(use: delete) - } - ///Add custom routes here + rts.delete(":x", use: delete) + //Additional routs added here } - func index(req: Request) async throws -> [mdlPriority] { - try await mdlPriority.ObjQuery(req: req).all() + func index(req: Request) async throws -> Response { + guard let obj = try? await mdlPriority.ObjQuery(req: req).all() else { + throw Abort(.badGateway, reason: "Something went wrong with this api.") + } + guard let response = try? await obj.encodeResponse(status: .ok, for: req) else{ + throw Abort(.noContent, reason: "Something went wrong decoding data. Array could not be shown.") + } + return response } - func read(req:Request) async throws -> mdlPriority{ - return try await mdlPriority.ObjQuery(req: req).first()! + func read(req:Request) async throws -> Response{ + guard let obj = try? await mdlPriority.ObjQuery(req: req).first() else{ + throw Abort(.notFound, reason: "Priority was not found.") + } + guard let response = try? await obj.encodeResponse(status: .ok, for: req) else { + throw Abort(.noContent, reason: "Something went wrong decoding object.") + } + return response } - func create(req: Request) async throws -> mdlPriority { - let obj = try req.content.decode(mdlPriority.self) + func create(req: Request) async throws -> Response { + + guard let obj = try? req.content.decode(mdlPriority.self) else { + throw Abort(.notAcceptable, reason: "Priority json not formatted correctly or is missing parents.") + } + var exists = false if(obj.id != nil){ obj._$idExists = true + exists = true + } + guard let _ = try? await obj.save(on: req.db) else{ + throw Abort(.conflict, reason: "Something conflicts in the database. Priority cannot be saved.") } - try await obj.save(on: req.db) - return obj + + guard let response = try? await obj.encodeResponse(status: exists ? .accepted : .created, for: req) else{ + throw Abort(.noContent, reason: "Something went wrong decoding data. Priority has been saved.") + } + return response } - func delete(req: Request) async throws -> HTTPStatus { - guard let obj = try await mdlPriority.find(req.parameters.get("id"), on: req.db) else { - throw Abort(.notFound) + + func delete(req: Request) async throws -> Response { + guard let obj = try? await mdlPriority.ObjQuery(req: req).first() else { + throw Abort(.notFound, reason: "Priority's ID was not supplied. Priority will not be deleted") } - try await obj.delete(on: req.db) - return .noContent + guard let _ = try? await obj.delete(on: req.db) else{ + throw Abort(.conflict, reason: "There was a conflict deleting Priority. Make sure it has not children.") + } + guard let response = try? await obj.encodeResponse(status: .ok, for: req) else{ + throw Abort(.noContent, reason: "Something went wrong decoding data. Priority has been deleted.") + } + return response } } + diff --git a/Sources/App/Controllers/BaseControllers/cntrlReason.swift b/Sources/App/Controllers/BaseControllers/cntrlReason.swift index a3a2024..04eda90 100644 --- a/Sources/App/Controllers/BaseControllers/cntrlReason.swift +++ b/Sources/App/Controllers/BaseControllers/cntrlReason.swift @@ -2,11 +2,14 @@ // cntrlReason.swift // // -// Created by Michiel Carman on 4/1/24. +// Created by Michiel Carman on 4/5/2024. // import Fluent import Vapor +import Crypto + + struct cntrlReason: RouteCollection { func boot(routes: RoutesBuilder) throws { @@ -14,34 +17,62 @@ struct cntrlReason: RouteCollection { rts.get(use: index) rts.get(":x", use: read) rts.post(use: create) - rts.group(":x") { rt in - rts.delete(use: delete) - } - ///Add custom routes here + rts.delete(":x", use: delete) + //Additional routs added here } - func index(req: Request) async throws -> [mdlReason] { - try await mdlReason.ObjQuery(req: req).all() + func index(req: Request) async throws -> Response { + guard let obj = try? await mdlReason.ObjQuery(req: req).all() else { + throw Abort(.badGateway, reason: "Something went wrong with this api.") + } + guard let response = try? await obj.encodeResponse(status: .ok, for: req) else{ + throw Abort(.noContent, reason: "Something went wrong decoding data. Array could not be shown.") + } + return response } - func read(req:Request) async throws -> mdlReason{ - return try await mdlReason.ObjQuery(req: req).first()! + func read(req:Request) async throws -> Response{ + guard let obj = try? await mdlReason.ObjQuery(req: req).first() else{ + throw Abort(.notFound, reason: "Reason was not found.") + } + guard let response = try? await obj.encodeResponse(status: .ok, for: req) else { + throw Abort(.noContent, reason: "Something went wrong decoding object.") + } + return response } - func create(req: Request) async throws -> mdlReason { - let obj = try req.content.decode(mdlReason.self) + func create(req: Request) async throws -> Response { + + guard let obj = try? req.content.decode(mdlReason.self) else { + throw Abort(.notAcceptable, reason: "Reason json not formatted correctly or is missing parents.") + } + var exists = false if(obj.id != nil){ obj._$idExists = true + exists = true + } + guard let _ = try? await obj.save(on: req.db) else{ + throw Abort(.conflict, reason: "Something conflicts in the database. Reason cannot be saved.") } - try await obj.save(on: req.db) - return obj + + guard let response = try? await obj.encodeResponse(status: exists ? .accepted : .created, for: req) else{ + throw Abort(.noContent, reason: "Something went wrong decoding data. Reason has been saved.") + } + return response } - func delete(req: Request) async throws -> HTTPStatus { - guard let obj = try await mdlReason.find(req.parameters.get("id"), on: req.db) else { - throw Abort(.notFound) + + func delete(req: Request) async throws -> Response { + guard let obj = try? await mdlReason.ObjQuery(req: req).first() else { + throw Abort(.notFound, reason: "Reason's ID was not supplied. Reason will not be deleted") } - try await obj.delete(on: req.db) - return .noContent + guard let _ = try? await obj.delete(on: req.db) else{ + throw Abort(.conflict, reason: "There was a conflict deleting Reason. Make sure it has not children.") + } + guard let response = try? await obj.encodeResponse(status: .ok, for: req) else{ + throw Abort(.noContent, reason: "Something went wrong decoding data. Reason has been deleted.") + } + return response } } + diff --git a/Sources/App/Controllers/BaseControllers/cntrlRepairType.swift b/Sources/App/Controllers/BaseControllers/cntrlRepairType.swift index 8318b5f..fc8fc7b 100644 --- a/Sources/App/Controllers/BaseControllers/cntrlRepairType.swift +++ b/Sources/App/Controllers/BaseControllers/cntrlRepairType.swift @@ -2,11 +2,14 @@ // cntrlRepairType.swift // // -// Created by Michiel Carman on 4/1/24. +// Created by Michiel Carman on 4/5/2025. // import Fluent import Vapor +import Crypto + + struct cntrlRepairType: RouteCollection { func boot(routes: RoutesBuilder) throws { @@ -14,34 +17,62 @@ struct cntrlRepairType: RouteCollection { rts.get(use: index) rts.get(":x", use: read) rts.post(use: create) - rts.group(":x") { rt in - rts.delete(use: delete) - } - ///Add custom routes here + rts.delete(":x", use: delete) + //Additional routs added here } - func index(req: Request) async throws -> [mdlRepairType] { - try await mdlRepairType.ObjQuery(req: req).all() + func index(req: Request) async throws -> Response { + guard let obj = try? await mdlRepairType.ObjQuery(req: req).all() else { + throw Abort(.badGateway, reason: "Something went wrong with this api.") + } + guard let response = try? await obj.encodeResponse(status: .ok, for: req) else{ + throw Abort(.noContent, reason: "Something went wrong decoding data. Array could not be shown.") + } + return response } - func read(req:Request) async throws -> mdlRepairType{ - return try await mdlRepairType.ObjQuery(req: req).first()! + func read(req:Request) async throws -> Response{ + guard let obj = try? await mdlRepairType.ObjQuery(req: req).first() else{ + throw Abort(.notFound, reason: "RepairType was not found.") + } + guard let response = try? await obj.encodeResponse(status: .ok, for: req) else { + throw Abort(.noContent, reason: "Something went wrong decoding object.") + } + return response } - func create(req: Request) async throws -> mdlRepairType { - let obj = try req.content.decode(mdlRepairType.self) + func create(req: Request) async throws -> Response { + + guard let obj = try? req.content.decode(mdlRepairType.self) else { + throw Abort(.notAcceptable, reason: "RepairType json not formatted correctly or is missing parents.") + } + var exists = false if(obj.id != nil){ obj._$idExists = true + exists = true + } + guard let _ = try? await obj.save(on: req.db) else{ + throw Abort(.conflict, reason: "Something conflicts in the database. RepairType cannot be saved.") } - try await obj.save(on: req.db) - return obj + + guard let response = try? await obj.encodeResponse(status: exists ? .accepted : .created, for: req) else{ + throw Abort(.noContent, reason: "Something went wrong decoding data. RepairType has been saved.") + } + return response } - func delete(req: Request) async throws -> HTTPStatus { - guard let obj = try await mdlRepairType.find(req.parameters.get("id"), on: req.db) else { - throw Abort(.notFound) + + func delete(req: Request) async throws -> Response { + guard let obj = try? await mdlRepairType.ObjQuery(req: req).first() else { + throw Abort(.notFound, reason: "RepairType's ID was not supplied. RepairType will not be deleted") } - try await obj.delete(on: req.db) - return .noContent + guard let _ = try? await obj.delete(on: req.db) else{ + throw Abort(.conflict, reason: "There was a conflict deleting RepairType. Make sure it has not children.") + } + guard let response = try? await obj.encodeResponse(status: .ok, for: req) else{ + throw Abort(.noContent, reason: "Something went wrong decoding data. RepairType has been deleted.") + } + return response } } + diff --git a/Sources/App/Controllers/BaseControllers/cntrlSMSCCode.swift b/Sources/App/Controllers/BaseControllers/cntrlSMSCCode.swift index 6521cb8..4b0237e 100644 --- a/Sources/App/Controllers/BaseControllers/cntrlSMSCCode.swift +++ b/Sources/App/Controllers/BaseControllers/cntrlSMSCCode.swift @@ -2,11 +2,14 @@ // cntrlSMSCCode.swift // // -// Created by Michiel Carman on 4/1/24. +// Created by Michiel Carman on 4/5/2024. // import Fluent import Vapor +import Crypto + + struct cntrlSMSCCode: RouteCollection { func boot(routes: RoutesBuilder) throws { @@ -14,34 +17,62 @@ struct cntrlSMSCCode: RouteCollection { rts.get(use: index) rts.get(":x", use: read) rts.post(use: create) - rts.group(":x") { rt in - rts.delete(use: delete) - } - ///Add custom routes here + rts.delete(":x", use: delete) + //Additional routs added here } - func index(req: Request) async throws -> [mdlSMSCCode] { - try await mdlSMSCCode.ObjQuery(req: req).all() + func index(req: Request) async throws -> Response { + guard let obj = try? await mdlSMSCCode.ObjQuery(req: req).all() else { + throw Abort(.badGateway, reason: "Something went wrong with this api.") + } + guard let response = try? await obj.encodeResponse(status: .ok, for: req) else{ + throw Abort(.noContent, reason: "Something went wrong decoding data. Array could not be shown.") + } + return response } - func read(req:Request) async throws -> mdlSMSCCode{ - return try await mdlSMSCCode.ObjQuery(req: req).first()! + func read(req:Request) async throws -> Response{ + guard let obj = try? await mdlSMSCCode.ObjQuery(req: req).first() else{ + throw Abort(.notFound, reason: "SMSCCode was not found.") + } + guard let response = try? await obj.encodeResponse(status: .ok, for: req) else { + throw Abort(.noContent, reason: "Something went wrong decoding object.") + } + return response } - func create(req: Request) async throws -> mdlSMSCCode { - let obj = try req.content.decode(mdlSMSCCode.self) + func create(req: Request) async throws -> Response { + + guard let obj = try? req.content.decode(mdlSMSCCode.self) else { + throw Abort(.notAcceptable, reason: "SMSCCode json not formatted correctly or is missing parents.") + } + var exists = false if(obj.id != nil){ obj._$idExists = true + exists = true + } + guard let _ = try? await obj.save(on: req.db) else{ + throw Abort(.conflict, reason: "Something conflicts in the database. SMSCCode cannot be saved.") } - try await obj.save(on: req.db) - return obj + + guard let response = try? await obj.encodeResponse(status: exists ? .accepted : .created, for: req) else{ + throw Abort(.noContent, reason: "Something went wrong decoding data. SMSCCode has been saved.") + } + return response } - func delete(req: Request) async throws -> HTTPStatus { - guard let obj = try await mdlSMSCCode.find(req.parameters.get("id"), on: req.db) else { - throw Abort(.notFound) + + func delete(req: Request) async throws -> Response { + guard let obj = try? await mdlSMSCCode.ObjQuery(req: req).first() else { + throw Abort(.notFound, reason: "SMSCCode's ID was not supplied. SMSCCode will not be deleted") } - try await obj.delete(on: req.db) - return .noContent + guard let _ = try? await obj.delete(on: req.db) else{ + throw Abort(.conflict, reason: "There was a conflict deleting SMSCCode. Make sure it has not children.") + } + guard let response = try? await obj.encodeResponse(status: .ok, for: req) else{ + throw Abort(.noContent, reason: "Something went wrong decoding data. SMSCCode has been deleted.") + } + return response } } + diff --git a/Sources/App/Controllers/BaseControllers/cntrlSignatureType.swift b/Sources/App/Controllers/BaseControllers/cntrlSignatureType.swift index fc4d279..e0e8b28 100644 --- a/Sources/App/Controllers/BaseControllers/cntrlSignatureType.swift +++ b/Sources/App/Controllers/BaseControllers/cntrlSignatureType.swift @@ -2,11 +2,14 @@ // cntrlSignatureType.swift // // -// Created by Michiel Carman on 4/1/24. +// Created by Michiel Carman on 4/5/2024. // import Fluent import Vapor +import Crypto + + struct cntrlSignatureType: RouteCollection { func boot(routes: RoutesBuilder) throws { @@ -14,34 +17,62 @@ struct cntrlSignatureType: RouteCollection { rts.get(use: index) rts.get(":x", use: read) rts.post(use: create) - rts.group(":x") { rt in - rts.delete(use: delete) - } - ///Add custom routes here + rts.delete(":x", use: delete) + //Additional routs added here } - func index(req: Request) async throws -> [mdlSignatureType] { - try await mdlSignatureType.ObjQuery(req: req).all() + func index(req: Request) async throws -> Response { + guard let obj = try? await mdlSignatureType.ObjQuery(req: req).all() else { + throw Abort(.badGateway, reason: "Something went wrong with this api.") + } + guard let response = try? await obj.encodeResponse(status: .ok, for: req) else{ + throw Abort(.noContent, reason: "Something went wrong decoding data. Array could not be shown.") + } + return response } - func read(req:Request) async throws -> mdlSignatureType{ - return try await mdlSignatureType.ObjQuery(req: req).first()! + func read(req:Request) async throws -> Response{ + guard let obj = try? await mdlSignatureType.ObjQuery(req: req).first() else{ + throw Abort(.notFound, reason: "SignatureType was not found.") + } + guard let response = try? await obj.encodeResponse(status: .ok, for: req) else { + throw Abort(.noContent, reason: "Something went wrong decoding object.") + } + return response } - func create(req: Request) async throws -> mdlSignatureType { - let obj = try req.content.decode(mdlSignatureType.self) + func create(req: Request) async throws -> Response { + + guard let obj = try? req.content.decode(mdlSignatureType.self) else { + throw Abort(.notAcceptable, reason: "SignatureType json not formatted correctly or is missing parents.") + } + var exists = false if(obj.id != nil){ obj._$idExists = true + exists = true + } + guard let _ = try? await obj.save(on: req.db) else{ + throw Abort(.conflict, reason: "Something conflicts in the database. SignatureType cannot be saved.") } - try await obj.save(on: req.db) - return obj + + guard let response = try? await obj.encodeResponse(status: exists ? .accepted : .created, for: req) else{ + throw Abort(.noContent, reason: "Something went wrong decoding data. SignatureType has been saved.") + } + return response } - func delete(req: Request) async throws -> HTTPStatus { - guard let obj = try await mdlSignatureType.find(req.parameters.get("id"), on: req.db) else { - throw Abort(.notFound) + + func delete(req: Request) async throws -> Response { + guard let obj = try? await mdlSignatureType.ObjQuery(req: req).first() else { + throw Abort(.notFound, reason: "SignatureType's ID was not supplied. SignatureType will not be deleted") } - try await obj.delete(on: req.db) - return .noContent + guard let _ = try? await obj.delete(on: req.db) else{ + throw Abort(.conflict, reason: "There was a conflict deleting SignatureType. Make sure it has not children.") + } + guard let response = try? await obj.encodeResponse(status: .ok, for: req) else{ + throw Abort(.noContent, reason: "Something went wrong decoding data. SignatureType has been deleted.") + } + return response } } + diff --git a/Sources/App/Controllers/BaseControllers/cntrlState.swift b/Sources/App/Controllers/BaseControllers/cntrlState.swift index a99b08b..c8cdf93 100644 --- a/Sources/App/Controllers/BaseControllers/cntrlState.swift +++ b/Sources/App/Controllers/BaseControllers/cntrlState.swift @@ -2,11 +2,14 @@ // cntrlState.swift // // -// Created by Michiel Carman on 4/1/24. +// Created by Michiel Carman on 4/5/2024. // import Fluent import Vapor +import Crypto + + struct cntrlState: RouteCollection { func boot(routes: RoutesBuilder) throws { @@ -14,34 +17,62 @@ struct cntrlState: RouteCollection { rts.get(use: index) rts.get(":x", use: read) rts.post(use: create) - rts.group(":x") { rt in - rts.delete(use: delete) - } - ///Add custom routes here + rts.delete(":x", use: delete) + //Additional routs added here } - func index(req: Request) async throws -> [mdlState] { - try await mdlState.ObjQuery(req: req).all() + func index(req: Request) async throws -> Response { + guard let obj = try? await mdlState.ObjQuery(req: req).all() else { + throw Abort(.badGateway, reason: "Something went wrong with this api.") + } + guard let response = try? await obj.encodeResponse(status: .ok, for: req) else{ + throw Abort(.noContent, reason: "Something went wrong decoding data. Array could not be shown.") + } + return response } - func read(req:Request) async throws -> mdlState{ - return try await mdlState.ObjQuery(req: req).first()! + func read(req:Request) async throws -> Response{ + guard let obj = try? await mdlState.ObjQuery(req: req).first() else{ + throw Abort(.notFound, reason: "State was not found.") + } + guard let response = try? await obj.encodeResponse(status: .ok, for: req) else { + throw Abort(.noContent, reason: "Something went wrong decoding object.") + } + return response } - func create(req: Request) async throws -> mdlState { - let obj = try req.content.decode(mdlState.self) + func create(req: Request) async throws -> Response { + + guard let obj = try? req.content.decode(mdlState.self) else { + throw Abort(.notAcceptable, reason: "State json not formatted correctly or is missing parents.") + } + var exists = false if(obj.id != nil){ obj._$idExists = true + exists = true + } + guard let _ = try? await obj.save(on: req.db) else{ + throw Abort(.conflict, reason: "Something conflicts in the database. State cannot be saved.") } - try await obj.save(on: req.db) - return obj + + guard let response = try? await obj.encodeResponse(status: exists ? .accepted : .created, for: req) else{ + throw Abort(.noContent, reason: "Something went wrong decoding data. State has been saved.") + } + return response } - func delete(req: Request) async throws -> HTTPStatus { - guard let obj = try await mdlState.find(req.parameters.get("id"), on: req.db) else { - throw Abort(.notFound) + + func delete(req: Request) async throws -> Response { + guard let obj = try? await mdlState.ObjQuery(req: req).first() else { + throw Abort(.notFound, reason: "State's ID was not supplied. State will not be deleted") } - try await obj.delete(on: req.db) - return .noContent + guard let _ = try? await obj.delete(on: req.db) else{ + throw Abort(.conflict, reason: "There was a conflict deleting State. Make sure it has not children.") + } + guard let response = try? await obj.encodeResponse(status: .ok, for: req) else{ + throw Abort(.noContent, reason: "Something went wrong decoding data. State has been deleted.") + } + return response } } + diff --git a/Sources/App/Controllers/BaseControllers/cntrlStatus.swift b/Sources/App/Controllers/BaseControllers/cntrlStatus.swift index acdcfe4..0e6e9b4 100644 --- a/Sources/App/Controllers/BaseControllers/cntrlStatus.swift +++ b/Sources/App/Controllers/BaseControllers/cntrlStatus.swift @@ -2,11 +2,14 @@ // cntrlStatus.swift // // -// Created by Michiel Carman on 4/1/24. +// Created by Michiel Carman on 4/5/2024. // import Fluent import Vapor +import Crypto + + struct cntrlStatus: RouteCollection { func boot(routes: RoutesBuilder) throws { @@ -14,34 +17,62 @@ struct cntrlStatus: RouteCollection { rts.get(use: index) rts.get(":x", use: read) rts.post(use: create) - rts.group(":x") { rt in - rts.delete(use: delete) - } - ///Add custom routes here + rts.delete(":x", use: delete) + //Additional routs added here } - func index(req: Request) async throws -> [mdlStatus] { - try await mdlStatus.ObjQuery(req: req).all() + func index(req: Request) async throws -> Response { + guard let obj = try? await mdlStatus.ObjQuery(req: req).all() else { + throw Abort(.badGateway, reason: "Something went wrong with this api.") + } + guard let response = try? await obj.encodeResponse(status: .ok, for: req) else{ + throw Abort(.noContent, reason: "Something went wrong decoding data. Array could not be shown.") + } + return response } - func read(req:Request) async throws -> mdlStatus{ - return try await mdlStatus.ObjQuery(req: req).first()! + func read(req:Request) async throws -> Response{ + guard let obj = try? await mdlStatus.ObjQuery(req: req).first() else{ + throw Abort(.notFound, reason: "Status was not found.") + } + guard let response = try? await obj.encodeResponse(status: .ok, for: req) else { + throw Abort(.noContent, reason: "Something went wrong decoding object.") + } + return response } - func create(req: Request) async throws -> mdlStatus { - let obj = try req.content.decode(mdlStatus.self) + func create(req: Request) async throws -> Response { + + guard let obj = try? req.content.decode(mdlStatus.self) else { + throw Abort(.notAcceptable, reason: "Status json not formatted correctly or is missing parents.") + } + var exists = false if(obj.id != nil){ obj._$idExists = true + exists = true + } + guard let _ = try? await obj.save(on: req.db) else{ + throw Abort(.conflict, reason: "Something conflicts in the database. Status cannot be saved.") } - try await obj.save(on: req.db) - return obj + + guard let response = try? await obj.encodeResponse(status: exists ? .accepted : .created, for: req) else{ + throw Abort(.noContent, reason: "Something went wrong decoding data. Status has been saved.") + } + return response } - func delete(req: Request) async throws -> HTTPStatus { - guard let obj = try await mdlStatus.find(req.parameters.get("id"), on: req.db) else { - throw Abort(.notFound) + + func delete(req: Request) async throws -> Response { + guard let obj = try? await mdlStatus.ObjQuery(req: req).first() else { + throw Abort(.notFound, reason: "Status's ID was not supplied. Status will not be deleted") } - try await obj.delete(on: req.db) - return .noContent + guard let _ = try? await obj.delete(on: req.db) else{ + throw Abort(.conflict, reason: "There was a conflict deleting Status. Make sure it has not children.") + } + guard let response = try? await obj.encodeResponse(status: .ok, for: req) else{ + throw Abort(.noContent, reason: "Something went wrong decoding data. Status has been deleted.") + } + return response } } + diff --git a/Sources/App/Controllers/BaseControllers/cntrlValueType.swift b/Sources/App/Controllers/BaseControllers/cntrlValueType.swift index c7bad2f..3b37e13 100644 --- a/Sources/App/Controllers/BaseControllers/cntrlValueType.swift +++ b/Sources/App/Controllers/BaseControllers/cntrlValueType.swift @@ -1,5 +1,15 @@ +// +// cntrlValueType.swift +// +// +// Created by Michiel Carman on 4/5/2024. +// + import Fluent import Vapor +import Crypto + + struct cntrlValueType: RouteCollection { func boot(routes: RoutesBuilder) throws { @@ -7,34 +17,62 @@ struct cntrlValueType: RouteCollection { rts.get(use: index) rts.get(":x", use: read) rts.post(use: create) - rts.group(":x") { rt in - rts.delete(use: delete) - } - ///Add custom routes here + rts.delete(":x", use: delete) + //Additional routs added here } - - func index(req: Request) async throws -> [mdlValueType] { - try await mdlValueType.ObjQuery(req: req).all() + + func index(req: Request) async throws -> Response { + guard let obj = try? await mdlValueType.ObjQuery(req: req).all() else { + throw Abort(.badGateway, reason: "Something went wrong with this api.") + } + guard let response = try? await obj.encodeResponse(status: .ok, for: req) else{ + throw Abort(.noContent, reason: "Something went wrong decoding data. Array could not be shown.") + } + return response } - func read(req:Request) async throws -> mdlValueType{ - return try await mdlValueType.ObjQuery(req: req).first()! + func read(req:Request) async throws -> Response{ + guard let obj = try? await mdlValueType.ObjQuery(req: req).first() else{ + throw Abort(.notFound, reason: "ValueType was not found.") + } + guard let response = try? await obj.encodeResponse(status: .ok, for: req) else { + throw Abort(.noContent, reason: "Something went wrong decoding object.") + } + return response } - - func create(req: Request) async throws -> mdlValueType { - let obj = try req.content.decode(mdlValueType.self) + + func create(req: Request) async throws -> Response { + + guard let obj = try? req.content.decode(mdlValueType.self) else { + throw Abort(.notAcceptable, reason: "ValueType json not formatted correctly or is missing parents.") + } + var exists = false if(obj.id != nil){ obj._$idExists = true + exists = true + } + guard let _ = try? await obj.save(on: req.db) else{ + throw Abort(.conflict, reason: "Something conflicts in the database. ValueType cannot be saved.") } - try await obj.save(on: req.db) - return obj + + guard let response = try? await obj.encodeResponse(status: exists ? .accepted : .created, for: req) else{ + throw Abort(.noContent, reason: "Something went wrong decoding data. ValueType has been saved.") + } + return response } - - func delete(req: Request) async throws -> HTTPStatus { - guard let obj = try await mdlValueType.find(req.parameters.get("id"), on: req.db) else { - throw Abort(.notFound) + + + func delete(req: Request) async throws -> Response { + guard let obj = try? await mdlValueType.ObjQuery(req: req).first() else { + throw Abort(.notFound, reason: "ValueType's ID was not supplied. ValueType will not be deleted") + } + guard let _ = try? await obj.delete(on: req.db) else{ + throw Abort(.conflict, reason: "There was a conflict deleting ValueType. Make sure it has not children.") } - try await obj.delete(on: req.db) - return .noContent + guard let response = try? await obj.encodeResponse(status: .ok, for: req) else{ + throw Abort(.noContent, reason: "Something went wrong decoding data. ValueType has been deleted.") + } + return response } } + diff --git a/Sources/App/Controllers/BaseControllers/cntrlViscosity.swift b/Sources/App/Controllers/BaseControllers/cntrlViscosity.swift index 0dcebc1..b7ab19f 100644 --- a/Sources/App/Controllers/BaseControllers/cntrlViscosity.swift +++ b/Sources/App/Controllers/BaseControllers/cntrlViscosity.swift @@ -2,11 +2,14 @@ // cntrlViscosity.swift // // -// Created by Michiel Carman on 4/1/24. +// Created by Michiel Carman on 4/5/2024. // import Fluent import Vapor +import Crypto + + struct cntrlViscosity: RouteCollection { func boot(routes: RoutesBuilder) throws { @@ -14,44 +17,62 @@ struct cntrlViscosity: RouteCollection { rts.get(use: index) rts.get(":x", use: read) rts.post(use: create) - rts.group(":x") { rt in - rts.delete(use: delete) - } - ///Add custom routes here + rts.delete(":x", use: delete) + //Additional routs added here } - func index(req: Request) async throws -> [mdlViscosity] { - try await mdlViscosity.ObjQuery(req: req).all() + func index(req: Request) async throws -> Response { + guard let obj = try? await mdlViscosity.ObjQuery(req: req).all() else { + throw Abort(.badGateway, reason: "Something went wrong with this api.") + } + guard let response = try? await obj.encodeResponse(status: .ok, for: req) else{ + throw Abort(.noContent, reason: "Something went wrong decoding data. Array could not be shown.") + } + return response } - func read(req:Request) async throws -> mdlViscosity{ - return try await mdlViscosity.ObjQuery(req: req).first()! + func read(req:Request) async throws -> Response{ + guard let obj = try? await mdlViscosity.ObjQuery(req: req).first() else{ + throw Abort(.notFound, reason: "Viscosity was not found.") + } + guard let response = try? await obj.encodeResponse(status: .ok, for: req) else { + throw Abort(.noContent, reason: "Something went wrong decoding object.") + } + return response } - func create(req: Request) async throws -> mdlViscosity { - let obj = try req.content.decode(mdlViscosity.self) + func create(req: Request) async throws -> Response { + + guard let obj = try? req.content.decode(mdlViscosity.self) else { + throw Abort(.notAcceptable, reason: "Viscosity json not formatted correctly or is missing parents.") + } + var exists = false if(obj.id != nil){ obj._$idExists = true + exists = true + } + guard let _ = try? await obj.save(on: req.db) else{ + throw Abort(.conflict, reason: "Something conflicts in the database. Viscosity cannot be saved.") } - try await obj.save(on: req.db) - return obj - } - func update(req: Request) async throws -> mdlViscosity{ - let obj = try req.content.decode(mdlViscosity.self) - let dbobj = try await mdlViscosity.query(on: req.db).filter(\.$id == obj.id!).first()! - - dbobj.updatedate = Date() - dbobj.updateuserid = -1 - try await dbobj.update(on: req.db) - return dbobj + guard let response = try? await obj.encodeResponse(status: exists ? .accepted : .created, for: req) else{ + throw Abort(.noContent, reason: "Something went wrong decoding data. Viscosity has been saved.") + } + return response } - func delete(req: Request) async throws -> HTTPStatus { - guard let obj = try await mdlViscosity.find(req.parameters.get("id"), on: req.db) else { - throw Abort(.notFound) + + func delete(req: Request) async throws -> Response { + guard let obj = try? await mdlViscosity.ObjQuery(req: req).first() else { + throw Abort(.notFound, reason: "Viscosity's ID was not supplied. Viscosity will not be deleted") + } + guard let _ = try? await obj.delete(on: req.db) else{ + throw Abort(.conflict, reason: "There was a conflict deleting Viscosity. Make sure it has not children.") } - try await obj.delete(on: req.db) - return .noContent + guard let response = try? await obj.encodeResponse(status: .ok, for: req) else{ + throw Abort(.noContent, reason: "Something went wrong decoding data. Viscosity has been deleted.") + } + return response } } + diff --git a/Sources/App/Controllers/CompartmentControllers/cntrlCompartment.swift b/Sources/App/Controllers/CompartmentControllers/cntrlCompartment.swift index fa7fc81..3710a0d 100644 --- a/Sources/App/Controllers/CompartmentControllers/cntrlCompartment.swift +++ b/Sources/App/Controllers/CompartmentControllers/cntrlCompartment.swift @@ -2,11 +2,14 @@ // cntrlCompartment.swift // // -// Created by Michiel Carman on 4/1/24. +// Created by Michiel Carman on 4/5/2024. // import Fluent import Vapor +import Crypto + + struct cntrlCompartment: RouteCollection { func boot(routes: RoutesBuilder) throws { @@ -14,34 +17,62 @@ struct cntrlCompartment: RouteCollection { rts.get(use: index) rts.get(":x", use: read) rts.post(use: create) - rts.group(":x") { rt in - rts.delete(use: delete) - } - ///Add custom routes here + rts.delete(":x", use: delete) + //Additional routs added here } - func index(req: Request) async throws -> [mdlCompartment] { - try await mdlCompartment.ObjQuery(req: req).all() + func index(req: Request) async throws -> Response { + guard let obj = try? await mdlCompartment.ObjQuery(req: req).all() else { + throw Abort(.badGateway, reason: "Something went wrong with this api.") + } + guard let response = try? await obj.encodeResponse(status: .ok, for: req) else{ + throw Abort(.noContent, reason: "Something went wrong decoding data. Array could not be shown.") + } + return response } - func read(req:Request) async throws -> mdlCompartment{ - return try await mdlCompartment.ObjQuery(req: req).first()! + func read(req:Request) async throws -> Response{ + guard let obj = try? await mdlCompartment.ObjQuery(req: req).first() else{ + throw Abort(.notFound, reason: "Compartment was not found.") + } + guard let response = try? await obj.encodeResponse(status: .ok, for: req) else { + throw Abort(.noContent, reason: "Something went wrong decoding object.") + } + return response } - func create(req: Request) async throws -> mdlCompartment { - let obj = try req.content.decode(mdlCompartment.self) + func create(req: Request) async throws -> Response { + + guard let obj = try? req.content.decode(mdlCompartment.self) else { + throw Abort(.notAcceptable, reason: "Compartment json not formatted correctly or is missing parents.") + } + var exists = false if(obj.id != nil){ obj._$idExists = true + exists = true + } + guard let _ = try? await obj.save(on: req.db) else{ + throw Abort(.conflict, reason: "Something conflicts in the database. Compartment cannot be saved.") } - try await obj.save(on: req.db) - return obj + + guard let response = try? await obj.encodeResponse(status: exists ? .accepted : .created, for: req) else{ + throw Abort(.noContent, reason: "Something went wrong decoding data. Compartment has been saved.") + } + return response } - func delete(req: Request) async throws -> HTTPStatus { - guard let obj = try await mdlCompartment.find(req.parameters.get("id"), on: req.db) else { - throw Abort(.notFound) + + func delete(req: Request) async throws -> Response { + guard let obj = try? await mdlCompartment.ObjQuery(req: req).first() else { + throw Abort(.notFound, reason: "Compartment's ID was not supplied. Compartment will not be deleted") } - try await obj.delete(on: req.db) - return .noContent + guard let _ = try? await obj.delete(on: req.db) else{ + throw Abort(.conflict, reason: "There was a conflict deleting Compartment. Make sure it has not children.") + } + guard let response = try? await obj.encodeResponse(status: .ok, for: req) else{ + throw Abort(.noContent, reason: "Something went wrong decoding data. Compartment has been deleted.") + } + return response } } + diff --git a/Sources/App/Controllers/CompartmentControllers/cntrlCompartmentLocation.swift b/Sources/App/Controllers/CompartmentControllers/cntrlCompartmentLocation.swift index 93b5271..6fd8fb4 100644 --- a/Sources/App/Controllers/CompartmentControllers/cntrlCompartmentLocation.swift +++ b/Sources/App/Controllers/CompartmentControllers/cntrlCompartmentLocation.swift @@ -2,11 +2,14 @@ // cntrlCompartmentLocation.swift // // -// Created by Michiel Carman on 4/1/24. +// Created by Michiel Carman on 4/5/2024. // import Fluent import Vapor +import Crypto + + struct cntrlCompartmentLocation: RouteCollection { func boot(routes: RoutesBuilder) throws { @@ -14,44 +17,62 @@ struct cntrlCompartmentLocation: RouteCollection { rts.get(use: index) rts.get(":x", use: read) rts.post(use: create) - rts.group(":x") { rt in - rts.delete(use: delete) - } - ///Add custom routes here + rts.delete(":x", use: delete) + //Additional routs added here } - func index(req: Request) async throws -> [mdlCompartmentLocation] { - try await mdlCompartmentLocation.ObjQuery(req: req).all() + func index(req: Request) async throws -> Response { + guard let obj = try? await mdlCompartmentLocation.ObjQuery(req: req).all() else { + throw Abort(.badGateway, reason: "Something went wrong with this api.") + } + guard let response = try? await obj.encodeResponse(status: .ok, for: req) else{ + throw Abort(.noContent, reason: "Something went wrong decoding data. Array could not be shown.") + } + return response } - func read(req:Request) async throws -> mdlCompartmentLocation{ - return try await mdlCompartmentLocation.ObjQuery(req: req).first()! + func read(req:Request) async throws -> Response{ + guard let obj = try? await mdlCompartmentLocation.ObjQuery(req: req).first() else{ + throw Abort(.notFound, reason: "CompartmentLocation was not found.") + } + guard let response = try? await obj.encodeResponse(status: .ok, for: req) else { + throw Abort(.noContent, reason: "Something went wrong decoding object.") + } + return response } - func create(req: Request) async throws -> mdlCompartmentLocation { - let obj = try req.content.decode(mdlCompartmentLocation.self) + func create(req: Request) async throws -> Response { + + guard let obj = try? req.content.decode(mdlCompartmentLocation.self) else { + throw Abort(.notAcceptable, reason: "CompartmentLocation json not formatted correctly or is missing parents.") + } + var exists = false if(obj.id != nil){ obj._$idExists = true + exists = true + } + guard let _ = try? await obj.save(on: req.db) else{ + throw Abort(.conflict, reason: "Something conflicts in the database. CompartmentLocation cannot be saved.") } - try await obj.save(on: req.db) - return obj - } - func update(req: Request) async throws -> mdlCompartmentLocation{ - let obj = try req.content.decode(mdlCompartmentLocation.self) - let dbobj = try await mdlCompartmentLocation.query(on: req.db).filter(\.$id == obj.id!).first()! - - dbobj.updatedate = Date() - dbobj.updateuserid = -1 - try await dbobj.update(on: req.db) - return dbobj + guard let response = try? await obj.encodeResponse(status: exists ? .accepted : .created, for: req) else{ + throw Abort(.noContent, reason: "Something went wrong decoding data. CompartmentLocation has been saved.") + } + return response } - func delete(req: Request) async throws -> HTTPStatus { - guard let obj = try await mdlCompartmentLocation.find(req.parameters.get("id"), on: req.db) else { - throw Abort(.notFound) + + func delete(req: Request) async throws -> Response { + guard let obj = try? await mdlCompartmentLocation.ObjQuery(req: req).first() else { + throw Abort(.notFound, reason: "CompartmentLocation's ID was not supplied. CompartmentLocation will not be deleted") + } + guard let _ = try? await obj.delete(on: req.db) else{ + throw Abort(.conflict, reason: "There was a conflict deleting CompartmentLocation. Make sure it has not children.") } - try await obj.delete(on: req.db) - return .noContent + guard let response = try? await obj.encodeResponse(status: .ok, for: req) else{ + throw Abort(.noContent, reason: "Something went wrong decoding data. CompartmentLocation has been deleted.") + } + return response } } + diff --git a/Sources/App/Controllers/CustomerControllers/cntrlContact.swift b/Sources/App/Controllers/CustomerControllers/cntrlContact.swift index ab94825..bfac625 100644 --- a/Sources/App/Controllers/CustomerControllers/cntrlContact.swift +++ b/Sources/App/Controllers/CustomerControllers/cntrlContact.swift @@ -2,11 +2,14 @@ // cntrlContact.swift // // -// Created by Michiel Carman on 4/1/24. +// Created by Michiel Carman on 4/5/2024. // import Fluent import Vapor +import Crypto + + struct cntrlContact: RouteCollection { func boot(routes: RoutesBuilder) throws { @@ -14,34 +17,62 @@ struct cntrlContact: RouteCollection { rts.get(use: index) rts.get(":x", use: read) rts.post(use: create) - rts.group(":x") { rt in - rts.delete(use: delete) - } - ///Add custom routes here + rts.delete(":x", use: delete) + //Additional routs added here } - func index(req: Request) async throws -> [mdlContact] { - try await mdlContact.ObjQuery(req: req).all() + func index(req: Request) async throws -> Response { + guard let obj = try? await mdlContact.ObjQuery(req: req).all() else { + throw Abort(.badGateway, reason: "Something went wrong with this api.") + } + guard let response = try? await obj.encodeResponse(status: .ok, for: req) else{ + throw Abort(.noContent, reason: "Something went wrong decoding data. Array could not be shown.") + } + return response } - func read(req:Request) async throws -> mdlContact{ - return try await mdlContact.ObjQuery(req: req).first()! + func read(req:Request) async throws -> Response{ + guard let obj = try? await mdlContact.ObjQuery(req: req).first() else{ + throw Abort(.notFound, reason: "Contact was not found.") + } + guard let response = try? await obj.encodeResponse(status: .ok, for: req) else { + throw Abort(.noContent, reason: "Something went wrong decoding object.") + } + return response } - func create(req: Request) async throws -> mdlContact { - let obj = try req.content.decode(mdlContact.self) + func create(req: Request) async throws -> Response { + + guard let obj = try? req.content.decode(mdlContact.self) else { + throw Abort(.notAcceptable, reason: "Contact json not formatted correctly or is missing parents.") + } + var exists = false if(obj.id != nil){ obj._$idExists = true + exists = true + } + guard let _ = try? await obj.save(on: req.db) else{ + throw Abort(.conflict, reason: "Something conflicts in the database. Contact cannot be saved.") } - try await obj.save(on: req.db) - return obj + + guard let response = try? await obj.encodeResponse(status: exists ? .accepted : .created, for: req) else{ + throw Abort(.noContent, reason: "Something went wrong decoding data. Contact has been saved.") + } + return response } - func delete(req: Request) async throws -> HTTPStatus { - guard let obj = try await mdlContact.find(req.parameters.get("id"), on: req.db) else { - throw Abort(.notFound) + + func delete(req: Request) async throws -> Response { + guard let obj = try? await mdlContact.ObjQuery(req: req).first() else { + throw Abort(.notFound, reason: "Contact's ID was not supplied. Contact will not be deleted") } - try await obj.delete(on: req.db) - return .noContent + guard let _ = try? await obj.delete(on: req.db) else{ + throw Abort(.conflict, reason: "There was a conflict deleting Contact. Make sure it has not children.") + } + guard let response = try? await obj.encodeResponse(status: .ok, for: req) else{ + throw Abort(.noContent, reason: "Something went wrong decoding data. Contact has been deleted.") + } + return response } } + diff --git a/Sources/App/Controllers/CustomerControllers/cntrlContactInfo.swift b/Sources/App/Controllers/CustomerControllers/cntrlContactInfo.swift index 1709b03..4a76f03 100644 --- a/Sources/App/Controllers/CustomerControllers/cntrlContactInfo.swift +++ b/Sources/App/Controllers/CustomerControllers/cntrlContactInfo.swift @@ -2,11 +2,14 @@ // cntrlContactInfo.swift // // -// Created by Michiel Carman on 4/1/24. +// Created by Michiel Carman on 4/5/2024. // import Fluent import Vapor +import Crypto + + struct cntrlContactInfo: RouteCollection { func boot(routes: RoutesBuilder) throws { @@ -14,34 +17,62 @@ struct cntrlContactInfo: RouteCollection { rts.get(use: index) rts.get(":x", use: read) rts.post(use: create) - rts.group(":x") { rt in - rts.delete(use: delete) - } - ///Add custom routes here + rts.delete(":x", use: delete) + //Additional routs added here } - func index(req: Request) async throws -> [mdlContactInfo] { - try await mdlContactInfo.ObjQuery(req: req).all() + func index(req: Request) async throws -> Response { + guard let obj = try? await mdlContactInfo.ObjQuery(req: req).all() else { + throw Abort(.badGateway, reason: "Something went wrong with this api.") + } + guard let response = try? await obj.encodeResponse(status: .ok, for: req) else{ + throw Abort(.noContent, reason: "Something went wrong decoding data. Array could not be shown.") + } + return response } - func read(req:Request) async throws -> mdlContactInfo{ - return try await mdlContactInfo.ObjQuery(req: req).first()! + func read(req:Request) async throws -> Response{ + guard let obj = try? await mdlContactInfo.ObjQuery(req: req).first() else{ + throw Abort(.notFound, reason: "ContactInfo was not found.") + } + guard let response = try? await obj.encodeResponse(status: .ok, for: req) else { + throw Abort(.noContent, reason: "Something went wrong decoding object.") + } + return response } - func create(req: Request) async throws -> mdlContactInfo { - let obj = try req.content.decode(mdlContactInfo.self) + func create(req: Request) async throws -> Response { + + guard let obj = try? req.content.decode(mdlContactInfo.self) else { + throw Abort(.notAcceptable, reason: "ContactInfo json not formatted correctly or is missing parents.") + } + var exists = false if(obj.id != nil){ obj._$idExists = true + exists = true + } + guard let _ = try? await obj.save(on: req.db) else{ + throw Abort(.conflict, reason: "Something conflicts in the database. ContactInfo cannot be saved.") } - try await obj.save(on: req.db) - return obj + + guard let response = try? await obj.encodeResponse(status: exists ? .accepted : .created, for: req) else{ + throw Abort(.noContent, reason: "Something went wrong decoding data. ContactInfo has been saved.") + } + return response } - func delete(req: Request) async throws -> HTTPStatus { - guard let obj = try await mdlContactInfo.find(req.parameters.get("id"), on: req.db) else { - throw Abort(.notFound) + + func delete(req: Request) async throws -> Response { + guard let obj = try? await mdlContactInfo.ObjQuery(req: req).first() else { + throw Abort(.notFound, reason: "ContactInfo's ID was not supplied. ContactInfo will not be deleted") } - try await obj.delete(on: req.db) - return .noContent + guard let _ = try? await obj.delete(on: req.db) else{ + throw Abort(.conflict, reason: "There was a conflict deleting ContactInfo. Make sure it has not children.") + } + guard let response = try? await obj.encodeResponse(status: .ok, for: req) else{ + throw Abort(.noContent, reason: "Something went wrong decoding data. ContactInfo has been deleted.") + } + return response } } + diff --git a/Sources/App/Controllers/CustomerControllers/cntrlCustomer.swift b/Sources/App/Controllers/CustomerControllers/cntrlCustomer.swift index 31d02f2..bf303fc 100644 --- a/Sources/App/Controllers/CustomerControllers/cntrlCustomer.swift +++ b/Sources/App/Controllers/CustomerControllers/cntrlCustomer.swift @@ -2,11 +2,14 @@ // cntrlCustomer.swift // // -// Created by Michiel Carman on 4/1/24. +// Created by Michiel Carman on 4/5/2024. // import Fluent import Vapor +import Crypto + + struct cntrlCustomer: RouteCollection { func boot(routes: RoutesBuilder) throws { @@ -14,34 +17,62 @@ struct cntrlCustomer: RouteCollection { rts.get(use: index) rts.get(":x", use: read) rts.post(use: create) - rts.group(":x") { rt in - rts.delete(use: delete) - } - ///Add custom routes here + rts.delete(":x", use: delete) + //Additional routs added here } - func index(req: Request) async throws -> [mdlCustomer] { - try await mdlCustomer.ObjQuery(req: req).all() + func index(req: Request) async throws -> Response { + guard let obj = try? await mdlCustomer.ObjQuery(req: req).all() else { + throw Abort(.badGateway, reason: "Something went wrong with this api.") + } + guard let response = try? await obj.encodeResponse(status: .ok, for: req) else{ + throw Abort(.noContent, reason: "Something went wrong decoding data. Array could not be shown.") + } + return response } - func read(req:Request) async throws -> mdlCustomer{ - return try await mdlCustomer.ObjQuery(req: req).first()! + func read(req:Request) async throws -> Response{ + guard let obj = try? await mdlCustomer.ObjQuery(req: req).first() else{ + throw Abort(.notFound, reason: "Customer was not found.") + } + guard let response = try? await obj.encodeResponse(status: .ok, for: req) else { + throw Abort(.noContent, reason: "Something went wrong decoding object.") + } + return response } - func create(req: Request) async throws -> mdlCustomer { - let obj = try req.content.decode(mdlCustomer.self) + func create(req: Request) async throws -> Response { + + guard let obj = try? req.content.decode(mdlCustomer.self) else { + throw Abort(.notAcceptable, reason: "Customer json not formatted correctly or is missing parents.") + } + var exists = false if(obj.id != nil){ obj._$idExists = true + exists = true + } + guard let _ = try? await obj.save(on: req.db) else{ + throw Abort(.conflict, reason: "Something conflicts in the database. Customer cannot be saved.") } - try await obj.save(on: req.db) - return obj + + guard let response = try? await obj.encodeResponse(status: exists ? .accepted : .created, for: req) else{ + throw Abort(.noContent, reason: "Something went wrong decoding data. Customer has been saved.") + } + return response } - func delete(req: Request) async throws -> HTTPStatus { - guard let obj = try await mdlCustomer.find(req.parameters.get("id"), on: req.db) else { - throw Abort(.notFound) + + func delete(req: Request) async throws -> Response { + guard let obj = try? await mdlCustomer.ObjQuery(req: req).first() else { + throw Abort(.notFound, reason: "Customer's ID was not supplied. Customer will not be deleted") } - try await obj.delete(on: req.db) - return .noContent + guard let _ = try? await obj.delete(on: req.db) else{ + throw Abort(.conflict, reason: "There was a conflict deleting Customer. Make sure it has not children.") + } + guard let response = try? await obj.encodeResponse(status: .ok, for: req) else{ + throw Abort(.noContent, reason: "Something went wrong decoding data. Customer has been deleted.") + } + return response } } + diff --git a/Sources/App/Controllers/CustomerControllers/cntrlEquipment.swift b/Sources/App/Controllers/CustomerControllers/cntrlEquipment.swift index fbc4747..95974ec 100644 --- a/Sources/App/Controllers/CustomerControllers/cntrlEquipment.swift +++ b/Sources/App/Controllers/CustomerControllers/cntrlEquipment.swift @@ -2,11 +2,14 @@ // cntrlEquipment.swift // // -// Created by Michiel Carman on 4/1/24. +// Created by Michiel Carman on 4/5/2024. // import Fluent import Vapor +import Crypto + + struct cntrlEquipment: RouteCollection { func boot(routes: RoutesBuilder) throws { @@ -14,34 +17,62 @@ struct cntrlEquipment: RouteCollection { rts.get(use: index) rts.get(":x", use: read) rts.post(use: create) - rts.group(":x") { rt in - rts.delete(use: delete) - } - ///Add custom routes here + rts.delete(":x", use: delete) + //Additional routs added here } - func index(req: Request) async throws -> [mdlEquipment] { - try await mdlEquipment.ObjQuery(req: req).all() + func index(req: Request) async throws -> Response { + guard let obj = try? await mdlEquipment.ObjQuery(req: req).all() else { + throw Abort(.badGateway, reason: "Something went wrong with this api.") + } + guard let response = try? await obj.encodeResponse(status: .ok, for: req) else{ + throw Abort(.noContent, reason: "Something went wrong decoding data. Array could not be shown.") + } + return response } - func read(req:Request) async throws -> mdlEquipment{ - return try await mdlEquipment.ObjQuery(req: req).first()! + func read(req:Request) async throws -> Response{ + guard let obj = try? await mdlEquipment.ObjQuery(req: req).first() else{ + throw Abort(.notFound, reason: "Equipment was not found.") + } + guard let response = try? await obj.encodeResponse(status: .ok, for: req) else { + throw Abort(.noContent, reason: "Something went wrong decoding object.") + } + return response } - func create(req: Request) async throws -> mdlEquipment { - let obj = try req.content.decode(mdlEquipment.self) + func create(req: Request) async throws -> Response { + + guard let obj = try? req.content.decode(mdlEquipment.self) else { + throw Abort(.notAcceptable, reason: "Equipment json not formatted correctly or is missing parents.") + } + var exists = false if(obj.id != nil){ obj._$idExists = true + exists = true + } + guard let _ = try? await obj.save(on: req.db) else{ + throw Abort(.conflict, reason: "Something conflicts in the database. Equipment cannot be saved.") } - try await obj.save(on: req.db) - return obj + + guard let response = try? await obj.encodeResponse(status: exists ? .accepted : .created, for: req) else{ + throw Abort(.noContent, reason: "Something went wrong decoding data. Equipment has been saved.") + } + return response } - func delete(req: Request) async throws -> HTTPStatus { - guard let obj = try await mdlEquipment.find(req.parameters.get("id"), on: req.db) else { - throw Abort(.notFound) + + func delete(req: Request) async throws -> Response { + guard let obj = try? await mdlEquipment.ObjQuery(req: req).first() else { + throw Abort(.notFound, reason: "Equipment's ID was not supplied. Equipment will not be deleted") } - try await obj.delete(on: req.db) - return .noContent + guard let _ = try? await obj.delete(on: req.db) else{ + throw Abort(.conflict, reason: "There was a conflict deleting Equipment. Make sure it has not children.") + } + guard let response = try? await obj.encodeResponse(status: .ok, for: req) else{ + throw Abort(.noContent, reason: "Something went wrong decoding data. Equipment has been deleted.") + } + return response } } + diff --git a/Sources/App/Controllers/CustomerControllers/cntrlMake.swift b/Sources/App/Controllers/CustomerControllers/cntrlMake.swift index 4067786..c705f49 100644 --- a/Sources/App/Controllers/CustomerControllers/cntrlMake.swift +++ b/Sources/App/Controllers/CustomerControllers/cntrlMake.swift @@ -2,11 +2,14 @@ // cntrlMake.swift // // -// Created by Michiel Carman on 4/1/24. +// Created by Michiel Carman on 4/5/2024. // import Fluent import Vapor +import Crypto + + struct cntrlMake: RouteCollection { func boot(routes: RoutesBuilder) throws { @@ -14,34 +17,62 @@ struct cntrlMake: RouteCollection { rts.get(use: index) rts.get(":x", use: read) rts.post(use: create) - rts.group(":x") { rt in - rts.delete(use: delete) - } - ///Add custom routes here + rts.delete(":x", use: delete) + //Additional routs added here } - func index(req: Request) async throws -> [mdlMake] { - try await mdlMake.ObjQuery(req: req).all() + func index(req: Request) async throws -> Response { + guard let obj = try? await mdlMake.ObjQuery(req: req).all() else { + throw Abort(.badGateway, reason: "Something went wrong with this api.") + } + guard let response = try? await obj.encodeResponse(status: .ok, for: req) else{ + throw Abort(.noContent, reason: "Something went wrong decoding data. Array could not be shown.") + } + return response } - func read(req:Request) async throws -> mdlMake{ - return try await mdlMake.ObjQuery(req: req).first()! + func read(req:Request) async throws -> Response{ + guard let obj = try? await mdlMake.ObjQuery(req: req).first() else{ + throw Abort(.notFound, reason: "Make was not found.") + } + guard let response = try? await obj.encodeResponse(status: .ok, for: req) else { + throw Abort(.noContent, reason: "Something went wrong decoding object.") + } + return response } - func create(req: Request) async throws -> mdlMake { - let obj = try req.content.decode(mdlMake.self) + func create(req: Request) async throws -> Response { + + guard let obj = try? req.content.decode(mdlMake.self) else { + throw Abort(.notAcceptable, reason: "Make json not formatted correctly or is missing parents.") + } + var exists = false if(obj.id != nil){ obj._$idExists = true + exists = true + } + guard let _ = try? await obj.save(on: req.db) else{ + throw Abort(.conflict, reason: "Something conflicts in the database. Make cannot be saved.") } - try await obj.save(on: req.db) - return obj + + guard let response = try? await obj.encodeResponse(status: exists ? .accepted : .created, for: req) else{ + throw Abort(.noContent, reason: "Something went wrong decoding data. Make has been saved.") + } + return response } - func delete(req: Request) async throws -> HTTPStatus { - guard let obj = try await mdlMake.find(req.parameters.get("id"), on: req.db) else { - throw Abort(.notFound) + + func delete(req: Request) async throws -> Response { + guard let obj = try? await mdlMake.ObjQuery(req: req).first() else { + throw Abort(.notFound, reason: "Make's ID was not supplied. Make will not be deleted") } - try await obj.delete(on: req.db) - return .noContent + guard let _ = try? await obj.delete(on: req.db) else{ + throw Abort(.conflict, reason: "There was a conflict deleting Make. Make sure it has not children.") + } + guard let response = try? await obj.encodeResponse(status: .ok, for: req) else{ + throw Abort(.noContent, reason: "Something went wrong decoding data. Make has been deleted.") + } + return response } } + diff --git a/Sources/App/Controllers/CustomerControllers/cntrlModel.swift b/Sources/App/Controllers/CustomerControllers/cntrlModel.swift index bd39da7..81f56a1 100644 --- a/Sources/App/Controllers/CustomerControllers/cntrlModel.swift +++ b/Sources/App/Controllers/CustomerControllers/cntrlModel.swift @@ -2,11 +2,14 @@ // cntrlModel.swift // // -// Created by Michiel Carman on 4/1/24. +// Created by Michiel Carman on 4/5/2024. // import Fluent import Vapor +import Crypto + + struct cntrlModel: RouteCollection { func boot(routes: RoutesBuilder) throws { @@ -14,34 +17,62 @@ struct cntrlModel: RouteCollection { rts.get(use: index) rts.get(":x", use: read) rts.post(use: create) - rts.group(":x") { rt in - rts.delete(use: delete) - } - ///Add custom routes here + rts.delete(":x", use: delete) + //Additional routs added here } - func index(req: Request) async throws -> [mdlModel] { - try await mdlModel.ObjQuery(req: req).all() + func index(req: Request) async throws -> Response { + guard let obj = try? await mdlModel.ObjQuery(req: req).all() else { + throw Abort(.badGateway, reason: "Something went wrong with this api.") + } + guard let response = try? await obj.encodeResponse(status: .ok, for: req) else{ + throw Abort(.noContent, reason: "Something went wrong decoding data. Array could not be shown.") + } + return response } - func read(req:Request) async throws -> mdlModel{ - return try await mdlModel.ObjQuery(req: req).first()! + func read(req:Request) async throws -> Response{ + guard let obj = try? await mdlModel.ObjQuery(req: req).first() else{ + throw Abort(.notFound, reason: "Model was not found.") + } + guard let response = try? await obj.encodeResponse(status: .ok, for: req) else { + throw Abort(.noContent, reason: "Something went wrong decoding object.") + } + return response } - func create(req: Request) async throws -> mdlModel { - let obj = try req.content.decode(mdlModel.self) + func create(req: Request) async throws -> Response { + + guard let obj = try? req.content.decode(mdlModel.self) else { + throw Abort(.notAcceptable, reason: "Model json not formatted correctly or is missing parents.") + } + var exists = false if(obj.id != nil){ obj._$idExists = true + exists = true + } + guard let _ = try? await obj.save(on: req.db) else{ + throw Abort(.conflict, reason: "Something conflicts in the database. Model cannot be saved.") } - try await obj.save(on: req.db) - return obj + + guard let response = try? await obj.encodeResponse(status: exists ? .accepted : .created, for: req) else{ + throw Abort(.noContent, reason: "Something went wrong decoding data. Model has been saved.") + } + return response } - func delete(req: Request) async throws -> HTTPStatus { - guard let obj = try await mdlModel.find(req.parameters.get("id"), on: req.db) else { - throw Abort(.notFound) + + func delete(req: Request) async throws -> Response { + guard let obj = try? await mdlModel.ObjQuery(req: req).first() else { + throw Abort(.notFound, reason: "Model's ID was not supplied. Model will not be deleted") } - try await obj.delete(on: req.db) - return .noContent + guard let _ = try? await obj.delete(on: req.db) else{ + throw Abort(.conflict, reason: "There was a conflict deleting Model. Make sure it has not children.") + } + guard let response = try? await obj.encodeResponse(status: .ok, for: req) else{ + throw Abort(.noContent, reason: "Something went wrong decoding data. Model has been deleted.") + } + return response } } + diff --git a/Sources/App/Controllers/CustomerControllers/cntrlObjectProperty.swift b/Sources/App/Controllers/CustomerControllers/cntrlObjectProperty.swift index ef92bdf..e7ae55c 100644 --- a/Sources/App/Controllers/CustomerControllers/cntrlObjectProperty.swift +++ b/Sources/App/Controllers/CustomerControllers/cntrlObjectProperty.swift @@ -1,47 +1,78 @@ // -// cntrlObjectProperty.swift +// cntrlObjProperty.swift // // -// Created by Michiel Carman on 4/1/24. +// Created by Michiel Carman on 4/5/2024. // import Fluent import Vapor +import Crypto + + struct cntrlObjProperty: RouteCollection { func boot(routes: RoutesBuilder) throws { - let rts = routes.grouped("objproperties") + let rts = routes.grouped("objectproperties") rts.get(use: index) rts.get(":x", use: read) rts.post(use: create) - rts.group(":x") { rt in - rts.delete(use: delete) - } - ///Add custom routes here + rts.delete(":x", use: delete) + //Additional routs added here } - func index(req: Request) async throws -> [mdlObjProperty] { - try await mdlObjProperty.ObjQuery(req: req).all() + func index(req: Request) async throws -> Response { + guard let obj = try? await mdlObjProperty.ObjQuery(req: req).all() else { + throw Abort(.badGateway, reason: "Something went wrong with this api.") + } + guard let response = try? await obj.encodeResponse(status: .ok, for: req) else{ + throw Abort(.noContent, reason: "Something went wrong decoding data. Array could not be shown.") + } + return response } - func read(req:Request) async throws -> mdlObjProperty{ - return try await mdlObjProperty.ObjQuery(req: req).first()! + func read(req:Request) async throws -> Response{ + guard let obj = try? await mdlObjProperty.ObjQuery(req: req).first() else{ + throw Abort(.notFound, reason: "ObjProperty was not found.") + } + guard let response = try? await obj.encodeResponse(status: .ok, for: req) else { + throw Abort(.noContent, reason: "Something went wrong decoding object.") + } + return response } - func create(req: Request) async throws -> mdlObjProperty { - let obj = try req.content.decode(mdlObjProperty.self) + func create(req: Request) async throws -> Response { + + guard let obj = try? req.content.decode(mdlObjProperty.self) else { + throw Abort(.notAcceptable, reason: "ObjProperty json not formatted correctly or is missing parents.") + } + var exists = false if(obj.id != nil){ obj._$idExists = true + exists = true + } + guard let _ = try? await obj.save(on: req.db) else{ + throw Abort(.conflict, reason: "Something conflicts in the database. ObjProperty cannot be saved.") } - try await obj.save(on: req.db) - return obj + + guard let response = try? await obj.encodeResponse(status: exists ? .accepted : .created, for: req) else{ + throw Abort(.noContent, reason: "Something went wrong decoding data. ObjProperty has been saved.") + } + return response } - func delete(req: Request) async throws -> HTTPStatus { - guard let obj = try await mdlObjProperty.find(req.parameters.get("id"), on: req.db) else { - throw Abort(.notFound) + + func delete(req: Request) async throws -> Response { + guard let obj = try? await mdlObjProperty.ObjQuery(req: req).first() else { + throw Abort(.notFound, reason: "ObjProperty's ID was not supplied. ObjProperty will not be deleted") } - try await obj.delete(on: req.db) - return .noContent + guard let _ = try? await obj.delete(on: req.db) else{ + throw Abort(.conflict, reason: "There was a conflict deleting ObjProperty. Make sure it has not children.") + } + guard let response = try? await obj.encodeResponse(status: .ok, for: req) else{ + throw Abort(.noContent, reason: "Something went wrong decoding data. ObjProperty has been deleted.") + } + return response } } + diff --git a/Sources/App/Controllers/CustomerControllers/cntrlProduct.swift b/Sources/App/Controllers/CustomerControllers/cntrlProduct.swift index fe7ff9a..eb80c41 100644 --- a/Sources/App/Controllers/CustomerControllers/cntrlProduct.swift +++ b/Sources/App/Controllers/CustomerControllers/cntrlProduct.swift @@ -2,11 +2,14 @@ // cntrlProduct.swift // // -// Created by Michiel Carman on 4/1/24. +// Created by Michiel Carman on 4/5/2024. // import Fluent import Vapor +import Crypto + + struct cntrlProduct: RouteCollection { func boot(routes: RoutesBuilder) throws { @@ -14,34 +17,62 @@ struct cntrlProduct: RouteCollection { rts.get(use: index) rts.get(":x", use: read) rts.post(use: create) - rts.group(":x") { rt in - rts.delete(use: delete) - } - ///Add custom routes here + rts.delete(":x", use: delete) + //Additional routs added here } - func index(req: Request) async throws -> [mdlProduct] { - try await mdlProduct.ObjQuery(req: req).all() + func index(req: Request) async throws -> Response { + guard let obj = try? await mdlProduct.ObjQuery(req: req).all() else { + throw Abort(.badGateway, reason: "Something went wrong with this api.") + } + guard let response = try? await obj.encodeResponse(status: .ok, for: req) else{ + throw Abort(.noContent, reason: "Something went wrong decoding data. Array could not be shown.") + } + return response } - func read(req:Request) async throws -> mdlProduct{ - return try await mdlProduct.ObjQuery(req: req).first()! + func read(req:Request) async throws -> Response{ + guard let obj = try? await mdlProduct.ObjQuery(req: req).first() else{ + throw Abort(.notFound, reason: "Product was not found.") + } + guard let response = try? await obj.encodeResponse(status: .ok, for: req) else { + throw Abort(.noContent, reason: "Something went wrong decoding object.") + } + return response } - func create(req: Request) async throws -> mdlProduct { - let obj = try req.content.decode(mdlProduct.self) + func create(req: Request) async throws -> Response { + + guard let obj = try? req.content.decode(mdlProduct.self) else { + throw Abort(.notAcceptable, reason: "Product json not formatted correctly or is missing parents.") + } + var exists = false if(obj.id != nil){ obj._$idExists = true + exists = true + } + guard let _ = try? await obj.save(on: req.db) else{ + throw Abort(.conflict, reason: "Something conflicts in the database. Product cannot be saved.") } - try await obj.save(on: req.db) - return obj + + guard let response = try? await obj.encodeResponse(status: exists ? .accepted : .created, for: req) else{ + throw Abort(.noContent, reason: "Something went wrong decoding data. Product has been saved.") + } + return response } - func delete(req: Request) async throws -> HTTPStatus { - guard let obj = try await mdlProduct.find(req.parameters.get("id"), on: req.db) else { - throw Abort(.notFound) + + func delete(req: Request) async throws -> Response { + guard let obj = try? await mdlProduct.ObjQuery(req: req).first() else { + throw Abort(.notFound, reason: "Product's ID was not supplied. Product will not be deleted") } - try await obj.delete(on: req.db) - return .noContent + guard let _ = try? await obj.delete(on: req.db) else{ + throw Abort(.conflict, reason: "There was a conflict deleting Product. Make sure it has not children.") + } + guard let response = try? await obj.encodeResponse(status: .ok, for: req) else{ + throw Abort(.noContent, reason: "Something went wrong decoding data. Product has been deleted.") + } + return response } } + diff --git a/Sources/App/Controllers/GlobalFunctions/EncryptionController.swift b/Sources/App/Controllers/GlobalFunctions/EncryptionController.swift index dbd70d8..99a1002 100644 --- a/Sources/App/Controllers/GlobalFunctions/EncryptionController.swift +++ b/Sources/App/Controllers/GlobalFunctions/EncryptionController.swift @@ -30,6 +30,23 @@ struct CustomCrypto{ } return decryptedString } + + public static func EncryptString(input: String) throws -> String{ + let apikey = Environment.get("API_KEY") ?? "rv0ywT7Ygwh01jP1dQWBP39ogjq5ladO+EoNUGcBVq0=" + print(apikey) + guard let keyData = Data(base64Encoded: apikey) else{ + fatalError("Invalid Base64 key data") + } + let encryptionKey = SymmetricKey(data: keyData) + guard let datatoEncrypt = input.data(using: .utf8) else{ fatalError("Invalid Data")} + do{ + let sealedBox = try AES.GCM.seal(datatoEncrypt, using: encryptionKey) + guard let combined = sealedBox.combined else{ fatalError("Can't combine")} + let base64EncodedString = combined.base64EncodedString() + let urlEncodedString = base64EncodedString.addingPercentEncoding(withAllowedCharacters: .urlHostAllowed) + return urlEncodedString! + } + } } diff --git a/Sources/App/Controllers/ItemControllers/cntrlItem.swift b/Sources/App/Controllers/ItemControllers/cntrlItem.swift index 574822d..3d09c94 100644 --- a/Sources/App/Controllers/ItemControllers/cntrlItem.swift +++ b/Sources/App/Controllers/ItemControllers/cntrlItem.swift @@ -2,11 +2,14 @@ // cntrlItem.swift // // -// Created by Michiel Carman on 4/1/24. +// Created by Michiel Carman on 4/5/2024. // import Fluent import Vapor +import Crypto + + struct cntrlItem: RouteCollection { func boot(routes: RoutesBuilder) throws { @@ -14,34 +17,62 @@ struct cntrlItem: RouteCollection { rts.get(use: index) rts.get(":x", use: read) rts.post(use: create) - rts.group(":x") { rt in - rts.delete(use: delete) - } - ///Add custom routes here + rts.delete(":x", use: delete) + //Additional routs added here } - func index(req: Request) async throws -> [mdlItem] { - try await mdlItem.ObjQuery(req: req).all() + func index(req: Request) async throws -> Response { + guard let obj = try? await mdlItem.ObjQuery(req: req).all() else { + throw Abort(.badGateway, reason: "Something went wrong with this api.") + } + guard let response = try? await obj.encodeResponse(status: .ok, for: req) else{ + throw Abort(.noContent, reason: "Something went wrong decoding data. Array could not be shown.") + } + return response } - func read(req:Request) async throws -> mdlItem{ - return try await mdlItem.ObjQuery(req: req).first()! + func read(req:Request) async throws -> Response{ + guard let obj = try? await mdlItem.ObjQuery(req: req).first() else{ + throw Abort(.notFound, reason: "Item was not found.") + } + guard let response = try? await obj.encodeResponse(status: .ok, for: req) else { + throw Abort(.noContent, reason: "Something went wrong decoding object.") + } + return response } - func create(req: Request) async throws -> mdlItem { - let obj = try req.content.decode(mdlItem.self) + func create(req: Request) async throws -> Response { + + guard let obj = try? req.content.decode(mdlItem.self) else { + throw Abort(.notAcceptable, reason: "Item json not formatted correctly or is missing parents.") + } + var exists = false if(obj.id != nil){ obj._$idExists = true + exists = true + } + guard let _ = try? await obj.save(on: req.db) else{ + throw Abort(.conflict, reason: "Something conflicts in the database. Item cannot be saved.") } - try await obj.save(on: req.db) - return obj + + guard let response = try? await obj.encodeResponse(status: exists ? .accepted : .created, for: req) else{ + throw Abort(.noContent, reason: "Something went wrong decoding data. Item has been saved.") + } + return response } - func delete(req: Request) async throws -> HTTPStatus { - guard let obj = try await mdlItem.find(req.parameters.get("id"), on: req.db) else { - throw Abort(.notFound) + + func delete(req: Request) async throws -> Response { + guard let obj = try? await mdlItem.ObjQuery(req: req).first() else { + throw Abort(.notFound, reason: "Item's ID was not supplied. Item will not be deleted") } - try await obj.delete(on: req.db) - return .noContent + guard let _ = try? await obj.delete(on: req.db) else{ + throw Abort(.conflict, reason: "There was a conflict deleting Item. Make sure it has not children.") + } + guard let response = try? await obj.encodeResponse(status: .ok, for: req) else{ + throw Abort(.noContent, reason: "Something went wrong decoding data. Item has been deleted.") + } + return response } } + diff --git a/Sources/App/Controllers/ItemControllers/cntrlItemResponseType.swift b/Sources/App/Controllers/ItemControllers/cntrlItemResponseType.swift index f3d1ae5..713414b 100644 --- a/Sources/App/Controllers/ItemControllers/cntrlItemResponseType.swift +++ b/Sources/App/Controllers/ItemControllers/cntrlItemResponseType.swift @@ -2,11 +2,14 @@ // cntrlItemResponseType.swift // // -// Created by Michiel Carman on 4/1/24. +// Created by Michiel Carman on 4/5/2024. // import Fluent import Vapor +import Crypto + + struct cntrlItemResponseType: RouteCollection { func boot(routes: RoutesBuilder) throws { @@ -14,34 +17,62 @@ struct cntrlItemResponseType: RouteCollection { rts.get(use: index) rts.get(":x", use: read) rts.post(use: create) - rts.group(":x") { rt in - rts.delete(use: delete) - } - ///Add custom routes here + rts.delete(":x", use: delete) + //Additional routs added here } - func index(req: Request) async throws -> [mdlItemResponseType] { - try await mdlItemResponseType.ObjQuery(req: req).all() + func index(req: Request) async throws -> Response { + guard let obj = try? await mdlItemResponseType.ObjQuery(req: req).all() else { + throw Abort(.badGateway, reason: "Something went wrong with this api.") + } + guard let response = try? await obj.encodeResponse(status: .ok, for: req) else{ + throw Abort(.noContent, reason: "Something went wrong decoding data. Array could not be shown.") + } + return response } - func read(req:Request) async throws -> mdlItemResponseType{ - return try await mdlItemResponseType.ObjQuery(req: req).first()! + func read(req:Request) async throws -> Response{ + guard let obj = try? await mdlItemResponseType.ObjQuery(req: req).first() else{ + throw Abort(.notFound, reason: "ItemResponseType was not found.") + } + guard let response = try? await obj.encodeResponse(status: .ok, for: req) else { + throw Abort(.noContent, reason: "Something went wrong decoding object.") + } + return response } - func create(req: Request) async throws -> mdlItemResponseType { - let obj = try req.content.decode(mdlItemResponseType.self) + func create(req: Request) async throws -> Response { + + guard let obj = try? req.content.decode(mdlItemResponseType.self) else { + throw Abort(.notAcceptable, reason: "ItemResponseType json not formatted correctly or is missing parents.") + } + var exists = false if(obj.id != nil){ obj._$idExists = true + exists = true + } + guard let _ = try? await obj.save(on: req.db) else{ + throw Abort(.conflict, reason: "Something conflicts in the database. ItemResponseType cannot be saved.") } - try await obj.save(on: req.db) - return obj + + guard let response = try? await obj.encodeResponse(status: exists ? .accepted : .created, for: req) else{ + throw Abort(.noContent, reason: "Something went wrong decoding data. ItemResponseType has been saved.") + } + return response } - func delete(req: Request) async throws -> HTTPStatus { - guard let obj = try await mdlItemResponseType.find(req.parameters.get("id"), on: req.db) else { - throw Abort(.notFound) + + func delete(req: Request) async throws -> Response { + guard let obj = try? await mdlItemResponseType.ObjQuery(req: req).first() else { + throw Abort(.notFound, reason: "ItemResponseType's ID was not supplied. ItemResponseType will not be deleted") } - try await obj.delete(on: req.db) - return .noContent + guard let _ = try? await obj.delete(on: req.db) else{ + throw Abort(.conflict, reason: "There was a conflict deleting ItemResponseType. Make sure it has not children.") + } + guard let response = try? await obj.encodeResponse(status: .ok, for: req) else{ + throw Abort(.noContent, reason: "Something went wrong decoding data. ItemResponseType has been deleted.") + } + return response } } + diff --git a/Sources/App/Controllers/LabControllers/cntrlLabSetting.swift b/Sources/App/Controllers/LabControllers/cntrlLabSetting.swift index 83a8481..71d14fc 100644 --- a/Sources/App/Controllers/LabControllers/cntrlLabSetting.swift +++ b/Sources/App/Controllers/LabControllers/cntrlLabSetting.swift @@ -2,11 +2,14 @@ // cntrlLabSetting.swift // // -// Created by Michiel Carman on 4/1/24. +// Created by Michiel Carman on 4/5/2024. // import Fluent import Vapor +import Crypto + + struct cntrlLabSetting: RouteCollection { func boot(routes: RoutesBuilder) throws { @@ -14,34 +17,62 @@ struct cntrlLabSetting: RouteCollection { rts.get(use: index) rts.get(":x", use: read) rts.post(use: create) - rts.group(":x") { rt in - rts.delete(use: delete) - } - ///Add custom routes here + rts.delete(":x", use: delete) + //Additional routs added here } - func index(req: Request) async throws -> [mdlLabSetting] { - try await mdlLabSetting.ObjQuery(req: req).all() + func index(req: Request) async throws -> Response { + guard let obj = try? await mdlLabSetting.ObjQuery(req: req).all() else { + throw Abort(.badGateway, reason: "Something went wrong with this api.") + } + guard let response = try? await obj.encodeResponse(status: .ok, for: req) else{ + throw Abort(.noContent, reason: "Something went wrong decoding data. Array could not be shown.") + } + return response } - func read(req:Request) async throws -> mdlLabSetting{ - return try await mdlLabSetting.ObjQuery(req: req).first()! + func read(req:Request) async throws -> Response{ + guard let obj = try? await mdlLabSetting.ObjQuery(req: req).first() else{ + throw Abort(.notFound, reason: "LabSetting was not found.") + } + guard let response = try? await obj.encodeResponse(status: .ok, for: req) else { + throw Abort(.noContent, reason: "Something went wrong decoding object.") + } + return response } - func create(req: Request) async throws -> mdlLabSetting { - let obj = try req.content.decode(mdlLabSetting.self) + func create(req: Request) async throws -> Response { + + guard let obj = try? req.content.decode(mdlLabSetting.self) else { + throw Abort(.notAcceptable, reason: "LabSetting json not formatted correctly or is missing parents.") + } + var exists = false if(obj.id != nil){ obj._$idExists = true + exists = true + } + guard let _ = try? await obj.save(on: req.db) else{ + throw Abort(.conflict, reason: "Something conflicts in the database. LabSetting cannot be saved.") } - try await obj.save(on: req.db) - return obj + + guard let response = try? await obj.encodeResponse(status: exists ? .accepted : .created, for: req) else{ + throw Abort(.noContent, reason: "Something went wrong decoding data. LabSetting has been saved.") + } + return response } - func delete(req: Request) async throws -> HTTPStatus { - guard let obj = try await mdlLabSetting.find(req.parameters.get("id"), on: req.db) else { - throw Abort(.notFound) + + func delete(req: Request) async throws -> Response { + guard let obj = try? await mdlLabSetting.ObjQuery(req: req).first() else { + throw Abort(.notFound, reason: "LabSetting's ID was not supplied. LabSetting will not be deleted") } - try await obj.delete(on: req.db) - return .noContent + guard let _ = try? await obj.delete(on: req.db) else{ + throw Abort(.conflict, reason: "There was a conflict deleting LabSetting. Make sure it has not children.") + } + guard let response = try? await obj.encodeResponse(status: .ok, for: req) else{ + throw Abort(.noContent, reason: "Something went wrong decoding data. LabSetting has been deleted.") + } + return response } } + diff --git a/Sources/App/Controllers/LinkingControllers/cntrlXFluidViscosity.swift b/Sources/App/Controllers/LinkingControllers/cntrlXFluidViscosity.swift index 1104af0..7437e8c 100644 --- a/Sources/App/Controllers/LinkingControllers/cntrlXFluidViscosity.swift +++ b/Sources/App/Controllers/LinkingControllers/cntrlXFluidViscosity.swift @@ -2,11 +2,14 @@ // cntrlXFluidViscosity.swift // // -// Created by Michiel Carman on 4/1/24. +// Created by Michiel Carman on 4/5/2024. // import Fluent import Vapor +import Crypto + + struct cntrlXFluidViscosity: RouteCollection { func boot(routes: RoutesBuilder) throws { @@ -14,34 +17,62 @@ struct cntrlXFluidViscosity: RouteCollection { rts.get(use: index) rts.get(":x", use: read) rts.post(use: create) - rts.group(":x") { rt in - rts.delete(use: delete) - } - ///Add custom routes here + rts.delete(":x", use: delete) + //Additional routs added here } - func index(req: Request) async throws -> [mdlXFluidViscosity] { - try await mdlXFluidViscosity.ObjQuery(req: req).all() + func index(req: Request) async throws -> Response { + guard let obj = try? await mdlXFluidViscosity.ObjQuery(req: req).all() else { + throw Abort(.badGateway, reason: "Something went wrong with this api.") + } + guard let response = try? await obj.encodeResponse(status: .ok, for: req) else{ + throw Abort(.noContent, reason: "Something went wrong decoding data. Array could not be shown.") + } + return response } - func read(req:Request) async throws -> mdlXFluidViscosity{ - return try await mdlXFluidViscosity.ObjQuery(req: req).first()! + func read(req:Request) async throws -> Response{ + guard let obj = try? await mdlXFluidViscosity.ObjQuery(req: req).first() else{ + throw Abort(.notFound, reason: "XFluidViscosity was not found.") + } + guard let response = try? await obj.encodeResponse(status: .ok, for: req) else { + throw Abort(.noContent, reason: "Something went wrong decoding object.") + } + return response } - func create(req: Request) async throws -> mdlXFluidViscosity { - let obj = try req.content.decode(mdlXFluidViscosity.self) + func create(req: Request) async throws -> Response { + + guard let obj = try? req.content.decode(mdlXFluidViscosity.self) else { + throw Abort(.notAcceptable, reason: "XFluidViscosity json not formatted correctly or is missing parents.") + } + var exists = false if(obj.id != nil){ obj._$idExists = true + exists = true + } + guard let _ = try? await obj.save(on: req.db) else{ + throw Abort(.conflict, reason: "Something conflicts in the database. XFluidViscosity cannot be saved.") } - try await obj.save(on: req.db) - return obj + + guard let response = try? await obj.encodeResponse(status: exists ? .accepted : .created, for: req) else{ + throw Abort(.noContent, reason: "Something went wrong decoding data. XFluidViscosity has been saved.") + } + return response } - func delete(req: Request) async throws -> HTTPStatus { - guard let obj = try await mdlXFluidViscosity.find(req.parameters.get("id"), on: req.db) else { - throw Abort(.notFound) + + func delete(req: Request) async throws -> Response { + guard let obj = try? await mdlXFluidViscosity.ObjQuery(req: req).first() else { + throw Abort(.notFound, reason: "XFluidViscosity's ID was not supplied. XFluidViscosity will not be deleted") } - try await obj.delete(on: req.db) - return .noContent + guard let _ = try? await obj.delete(on: req.db) else{ + throw Abort(.conflict, reason: "There was a conflict deleting XFluidViscosity. Make sure it has not children.") + } + guard let response = try? await obj.encodeResponse(status: .ok, for: req) else{ + throw Abort(.noContent, reason: "Something went wrong decoding data. XFluidViscosity has been deleted.") + } + return response } } + diff --git a/Sources/App/Controllers/LinkingControllers/cntrlXUserRole.swift b/Sources/App/Controllers/LinkingControllers/cntrlXUserRole.swift index 4b144a0..1043f56 100644 --- a/Sources/App/Controllers/LinkingControllers/cntrlXUserRole.swift +++ b/Sources/App/Controllers/LinkingControllers/cntrlXUserRole.swift @@ -2,11 +2,14 @@ // cntrlXUserRole.swift // // -// Created by Michiel Carman on 4/1/24. +// Created by Michiel Carman on 4/5/2024. // import Fluent import Vapor +import Crypto + + struct cntrlXUserRole: RouteCollection { func boot(routes: RoutesBuilder) throws { @@ -14,34 +17,62 @@ struct cntrlXUserRole: RouteCollection { rts.get(use: index) rts.get(":x", use: read) rts.post(use: create) - rts.group(":x") { rt in - rts.delete(use: delete) - } - ///Add custom routes here + rts.delete(":x", use: delete) + //Additional routs added here } - func index(req: Request) async throws -> [mdlXUserRole] { - try await mdlXUserRole.ObjQuery(req: req).all() + func index(req: Request) async throws -> Response { + guard let obj = try? await mdlXUserRole.ObjQuery(req: req).all() else { + throw Abort(.badGateway, reason: "Something went wrong with this api.") + } + guard let response = try? await obj.encodeResponse(status: .ok, for: req) else{ + throw Abort(.noContent, reason: "Something went wrong decoding data. Array could not be shown.") + } + return response } - func read(req:Request) async throws -> mdlXUserRole{ - return try await mdlXUserRole.ObjQuery(req: req).first()! + func read(req:Request) async throws -> Response{ + guard let obj = try? await mdlXUserRole.ObjQuery(req: req).first() else{ + throw Abort(.notFound, reason: "XUserRole was not found.") + } + guard let response = try? await obj.encodeResponse(status: .ok, for: req) else { + throw Abort(.noContent, reason: "Something went wrong decoding object.") + } + return response } - func create(req: Request) async throws -> mdlXUserRole { - let obj = try req.content.decode(mdlXUserRole.self) + func create(req: Request) async throws -> Response { + + guard let obj = try? req.content.decode(mdlXUserRole.self) else { + throw Abort(.notAcceptable, reason: "XUserRole json not formatted correctly or is missing parents.") + } + var exists = false if(obj.id != nil){ obj._$idExists = true + exists = true + } + guard let _ = try? await obj.save(on: req.db) else{ + throw Abort(.conflict, reason: "Something conflicts in the database. XUserRole cannot be saved.") } - try await obj.save(on: req.db) - return obj + + guard let response = try? await obj.encodeResponse(status: exists ? .accepted : .created, for: req) else{ + throw Abort(.noContent, reason: "Something went wrong decoding data. XUserRole has been saved.") + } + return response } - func delete(req: Request) async throws -> HTTPStatus { - guard let obj = try await mdlXUserRole.find(req.parameters.get("id"), on: req.db) else { - throw Abort(.notFound) + + func delete(req: Request) async throws -> Response { + guard let obj = try? await mdlXUserRole.ObjQuery(req: req).first() else { + throw Abort(.notFound, reason: "XUserRole's ID was not supplied. XUserRole will not be deleted") } - try await obj.delete(on: req.db) - return .noContent + guard let _ = try? await obj.delete(on: req.db) else{ + throw Abort(.conflict, reason: "There was a conflict deleting XUserRole. Make sure it has not children.") + } + guard let response = try? await obj.encodeResponse(status: .ok, for: req) else{ + throw Abort(.noContent, reason: "Something went wrong decoding data. XUserRole has been deleted.") + } + return response } } + diff --git a/Sources/App/Controllers/OilSampleControllers/cntrlOilSample.swift b/Sources/App/Controllers/OilSampleControllers/cntrlOilSample.swift index 5e46382..d31fa7c 100644 --- a/Sources/App/Controllers/OilSampleControllers/cntrlOilSample.swift +++ b/Sources/App/Controllers/OilSampleControllers/cntrlOilSample.swift @@ -2,11 +2,14 @@ // cntrlOilSample.swift // // -// Created by Michiel Carman on 4/1/24. +// Created by Michiel Carman on 4/5/2024. // import Fluent import Vapor +import Crypto + + struct cntrlOilSample: RouteCollection { func boot(routes: RoutesBuilder) throws { @@ -14,34 +17,62 @@ struct cntrlOilSample: RouteCollection { rts.get(use: index) rts.get(":x", use: read) rts.post(use: create) - rts.group(":x") { rt in - rts.delete(use: delete) - } - ///Add custom routes here + rts.delete(":x", use: delete) + //Additional routs added here } - func index(req: Request) async throws -> [mdlOilSample] { - try await mdlOilSample.ObjQuery(req: req).all() + func index(req: Request) async throws -> Response { + guard let obj = try? await mdlOilSample.ObjQuery(req: req).all() else { + throw Abort(.badGateway, reason: "Something went wrong with this api.") + } + guard let response = try? await obj.encodeResponse(status: .ok, for: req) else{ + throw Abort(.noContent, reason: "Something went wrong decoding data. Array could not be shown.") + } + return response } - func read(req:Request) async throws -> mdlOilSample{ - return try await mdlOilSample.ObjQuery(req: req).first()! + func read(req:Request) async throws -> Response{ + guard let obj = try? await mdlOilSample.ObjQuery(req: req).first() else{ + throw Abort(.notFound, reason: "OilSample was not found.") + } + guard let response = try? await obj.encodeResponse(status: .ok, for: req) else { + throw Abort(.noContent, reason: "Something went wrong decoding object.") + } + return response } - func create(req: Request) async throws -> mdlOilSample { - let obj = try req.content.decode(mdlOilSample.self) + func create(req: Request) async throws -> Response { + + guard let obj = try? req.content.decode(mdlOilSample.self) else { + throw Abort(.notAcceptable, reason: "OilSample json not formatted correctly or is missing parents.") + } + var exists = false if(obj.id != nil){ obj._$idExists = true + exists = true + } + guard let _ = try? await obj.save(on: req.db) else{ + throw Abort(.conflict, reason: "Something conflicts in the database. OilSample cannot be saved.") } - try await obj.save(on: req.db) - return obj + + guard let response = try? await obj.encodeResponse(status: exists ? .accepted : .created, for: req) else{ + throw Abort(.noContent, reason: "Something went wrong decoding data. OilSample has been saved.") + } + return response } - func delete(req: Request) async throws -> HTTPStatus { - guard let obj = try await mdlOilSample.find(req.parameters.get("id"), on: req.db) else { - throw Abort(.notFound) + + func delete(req: Request) async throws -> Response { + guard let obj = try? await mdlOilSample.ObjQuery(req: req).first() else { + throw Abort(.notFound, reason: "OilSample's ID was not supplied. OilSample will not be deleted") } - try await obj.delete(on: req.db) - return .noContent + guard let _ = try? await obj.delete(on: req.db) else{ + throw Abort(.conflict, reason: "There was a conflict deleting OilSample. Make sure it has not children.") + } + guard let response = try? await obj.encodeResponse(status: .ok, for: req) else{ + throw Abort(.noContent, reason: "Something went wrong decoding data. OilSample has been deleted.") + } + return response } } + diff --git a/Sources/App/Controllers/SpecControllers/cntrlSpec.swift b/Sources/App/Controllers/SpecControllers/cntrlSpec.swift index 4ff134b..506b114 100644 --- a/Sources/App/Controllers/SpecControllers/cntrlSpec.swift +++ b/Sources/App/Controllers/SpecControllers/cntrlSpec.swift @@ -2,11 +2,14 @@ // cntrlSpec.swift // // -// Created by Michiel Carman on 4/1/24. +// Created by Michiel Carman on 4/5/2024. // import Fluent import Vapor +import Crypto + + struct cntrlSpec: RouteCollection { func boot(routes: RoutesBuilder) throws { @@ -14,34 +17,62 @@ struct cntrlSpec: RouteCollection { rts.get(use: index) rts.get(":x", use: read) rts.post(use: create) - rts.group(":x") { rt in - rts.delete(use: delete) - } - ///Add custom routes here + rts.delete(":x", use: delete) + //Additional routs added here } - func index(req: Request) async throws -> [mdlSpec] { - try await mdlSpec.ObjQuery(req: req).all() + func index(req: Request) async throws -> Response { + guard let obj = try? await mdlSpec.ObjQuery(req: req).all() else { + throw Abort(.badGateway, reason: "Something went wrong with this api.") + } + guard let response = try? await obj.encodeResponse(status: .ok, for: req) else{ + throw Abort(.noContent, reason: "Something went wrong decoding data. Array could not be shown.") + } + return response } - func read(req:Request) async throws -> mdlSpec{ - return try await mdlSpec.ObjQuery(req: req).first()! + func read(req:Request) async throws -> Response{ + guard let obj = try? await mdlSpec.ObjQuery(req: req).first() else{ + throw Abort(.notFound, reason: "Spec was not found.") + } + guard let response = try? await obj.encodeResponse(status: .ok, for: req) else { + throw Abort(.noContent, reason: "Something went wrong decoding object.") + } + return response } - func create(req: Request) async throws -> mdlSpec { - let obj = try req.content.decode(mdlSpec.self) + func create(req: Request) async throws -> Response { + + guard let obj = try? req.content.decode(mdlSpec.self) else { + throw Abort(.notAcceptable, reason: "Spec json not formatted correctly or is missing parents.") + } + var exists = false if(obj.id != nil){ obj._$idExists = true + exists = true + } + guard let _ = try? await obj.save(on: req.db) else{ + throw Abort(.conflict, reason: "Something conflicts in the database. Spec cannot be saved.") } - try await obj.save(on: req.db) - return obj + + guard let response = try? await obj.encodeResponse(status: exists ? .accepted : .created, for: req) else{ + throw Abort(.noContent, reason: "Something went wrong decoding data. Spec has been saved.") + } + return response } - func delete(req: Request) async throws -> HTTPStatus { - guard let obj = try await mdlSpec.find(req.parameters.get("id"), on: req.db) else { - throw Abort(.notFound) + + func delete(req: Request) async throws -> Response { + guard let obj = try? await mdlSpec.ObjQuery(req: req).first() else { + throw Abort(.notFound, reason: "Spec's ID was not supplied. Spec will not be deleted") } - try await obj.delete(on: req.db) - return .noContent + guard let _ = try? await obj.delete(on: req.db) else{ + throw Abort(.conflict, reason: "There was a conflict deleting Spec. Make sure it has not children.") + } + guard let response = try? await obj.encodeResponse(status: .ok, for: req) else{ + throw Abort(.noContent, reason: "Something went wrong decoding data. Spec has been deleted.") + } + return response } } + diff --git a/Sources/App/Controllers/SpecControllers/cntrlSpecItem.swift b/Sources/App/Controllers/SpecControllers/cntrlSpecItem.swift index 65725af..cb1bf1e 100644 --- a/Sources/App/Controllers/SpecControllers/cntrlSpecItem.swift +++ b/Sources/App/Controllers/SpecControllers/cntrlSpecItem.swift @@ -2,11 +2,14 @@ // cntrlSpecItem.swift // // -// Created by Michiel Carman on 4/1/24. +// Created by Michiel Carman on 4/5/2024. // import Fluent import Vapor +import Crypto + + struct cntrlSpecItem: RouteCollection { func boot(routes: RoutesBuilder) throws { @@ -14,34 +17,62 @@ struct cntrlSpecItem: RouteCollection { rts.get(use: index) rts.get(":x", use: read) rts.post(use: create) - rts.group(":x") { rt in - rts.delete(use: delete) - } - ///Add custom routes here + rts.delete(":x", use: delete) + //Additional routs added here } - func index(req: Request) async throws -> [mdlSpecItem] { - try await mdlSpecItem.ObjQuery(req: req).all() + func index(req: Request) async throws -> Response { + guard let obj = try? await mdlSpecItem.ObjQuery(req: req).all() else { + throw Abort(.badGateway, reason: "Something went wrong with this api.") + } + guard let response = try? await obj.encodeResponse(status: .ok, for: req) else{ + throw Abort(.noContent, reason: "Something went wrong decoding data. Array could not be shown.") + } + return response } - func read(req:Request) async throws -> mdlSpecItem{ - return try await mdlSpecItem.ObjQuery(req: req).first()! + func read(req:Request) async throws -> Response{ + guard let obj = try? await mdlSpecItem.ObjQuery(req: req).first() else{ + throw Abort(.notFound, reason: "SpecItem was not found.") + } + guard let response = try? await obj.encodeResponse(status: .ok, for: req) else { + throw Abort(.noContent, reason: "Something went wrong decoding object.") + } + return response } - func create(req: Request) async throws -> mdlSpecItem { - let obj = try req.content.decode(mdlSpecItem.self) + func create(req: Request) async throws -> Response { + + guard let obj = try? req.content.decode(mdlSpecItem.self) else { + throw Abort(.notAcceptable, reason: "SpecItem json not formatted correctly or is missing parents.") + } + var exists = false if(obj.id != nil){ obj._$idExists = true + exists = true + } + guard let _ = try? await obj.save(on: req.db) else{ + throw Abort(.conflict, reason: "Something conflicts in the database. SpecItem cannot be saved.") } - try await obj.save(on: req.db) - return obj + + guard let response = try? await obj.encodeResponse(status: exists ? .accepted : .created, for: req) else{ + throw Abort(.noContent, reason: "Something went wrong decoding data. SpecItem has been saved.") + } + return response } - func delete(req: Request) async throws -> HTTPStatus { - guard let obj = try await mdlSpecItem.find(req.parameters.get("id"), on: req.db) else { - throw Abort(.notFound) + + func delete(req: Request) async throws -> Response { + guard let obj = try? await mdlSpecItem.ObjQuery(req: req).first() else { + throw Abort(.notFound, reason: "SpecItem's ID was not supplied. SpecItem will not be deleted") } - try await obj.delete(on: req.db) - return .noContent + guard let _ = try? await obj.delete(on: req.db) else{ + throw Abort(.conflict, reason: "There was a conflict deleting SpecItem. Make sure it has not children.") + } + guard let response = try? await obj.encodeResponse(status: .ok, for: req) else{ + throw Abort(.noContent, reason: "Something went wrong decoding data. SpecItem has been deleted.") + } + return response } } + diff --git a/Sources/App/Controllers/SpecControllers/cntrlSpecItemValue.swift b/Sources/App/Controllers/SpecControllers/cntrlSpecItemValue.swift index 81255ab..9d679ae 100644 --- a/Sources/App/Controllers/SpecControllers/cntrlSpecItemValue.swift +++ b/Sources/App/Controllers/SpecControllers/cntrlSpecItemValue.swift @@ -2,11 +2,14 @@ // cntrlSpecItemValue.swift // // -// Created by Michiel Carman on 4/1/24. +// Created by Michiel Carman on 4/5/2024. // import Fluent import Vapor +import Crypto + + struct cntrlSpecItemValue: RouteCollection { func boot(routes: RoutesBuilder) throws { @@ -14,34 +17,62 @@ struct cntrlSpecItemValue: RouteCollection { rts.get(use: index) rts.get(":x", use: read) rts.post(use: create) - rts.group(":x") { rt in - rts.delete(use: delete) - } - ///Add custom routes here + rts.delete(":x", use: delete) + //Additional routs added here } - func index(req: Request) async throws -> [mdlSpecItemValue] { - try await mdlSpecItemValue.ObjQuery(req: req).all() + func index(req: Request) async throws -> Response { + guard let obj = try? await mdlSpecItemValue.ObjQuery(req: req).all() else { + throw Abort(.badGateway, reason: "Something went wrong with this api.") + } + guard let response = try? await obj.encodeResponse(status: .ok, for: req) else{ + throw Abort(.noContent, reason: "Something went wrong decoding data. Array could not be shown.") + } + return response } - func read(req:Request) async throws -> mdlSpecItemValue{ - return try await mdlSpecItemValue.ObjQuery(req: req).first()! + func read(req:Request) async throws -> Response{ + guard let obj = try? await mdlSpecItemValue.ObjQuery(req: req).first() else{ + throw Abort(.notFound, reason: "SpecItemValue was not found.") + } + guard let response = try? await obj.encodeResponse(status: .ok, for: req) else { + throw Abort(.noContent, reason: "Something went wrong decoding object.") + } + return response } - func create(req: Request) async throws -> mdlSpecItemValue { - let obj = try req.content.decode(mdlSpecItemValue.self) + func create(req: Request) async throws -> Response { + + guard let obj = try? req.content.decode(mdlSpecItemValue.self) else { + throw Abort(.notAcceptable, reason: "SpecItemValue json not formatted correctly or is missing parents.") + } + var exists = false if(obj.id != nil){ obj._$idExists = true + exists = true + } + guard let _ = try? await obj.save(on: req.db) else{ + throw Abort(.conflict, reason: "Something conflicts in the database. SpecItemValue cannot be saved.") } - try await obj.save(on: req.db) - return obj + + guard let response = try? await obj.encodeResponse(status: exists ? .accepted : .created, for: req) else{ + throw Abort(.noContent, reason: "Something went wrong decoding data. SpecItemValue has been saved.") + } + return response } - func delete(req: Request) async throws -> HTTPStatus { - guard let obj = try await mdlSpecItemValue.find(req.parameters.get("id"), on: req.db) else { - throw Abort(.notFound) + + func delete(req: Request) async throws -> Response { + guard let obj = try? await mdlSpecItemValue.ObjQuery(req: req).first() else { + throw Abort(.notFound, reason: "SpecItemValue's ID was not supplied. SpecItemValue will not be deleted") } - try await obj.delete(on: req.db) - return .noContent + guard let _ = try? await obj.delete(on: req.db) else{ + throw Abort(.conflict, reason: "There was a conflict deleting SpecItemValue. Make sure it has not children.") + } + guard let response = try? await obj.encodeResponse(status: .ok, for: req) else{ + throw Abort(.noContent, reason: "Something went wrong decoding data. SpecItemValue has been deleted.") + } + return response } } + diff --git a/Sources/App/Controllers/TemplateControllers/cntrlTemplate.swift b/Sources/App/Controllers/TemplateControllers/cntrlTemplate.swift index fe65079..c46b64d 100644 --- a/Sources/App/Controllers/TemplateControllers/cntrlTemplate.swift +++ b/Sources/App/Controllers/TemplateControllers/cntrlTemplate.swift @@ -2,11 +2,14 @@ // cntrlTemplate.swift // // -// Created by Michiel Carman on 4/1/24. +// Created by Michiel Carman on 4/5/2024. // import Fluent import Vapor +import Crypto + + struct cntrlTemplate: RouteCollection { func boot(routes: RoutesBuilder) throws { @@ -14,34 +17,62 @@ struct cntrlTemplate: RouteCollection { rts.get(use: index) rts.get(":x", use: read) rts.post(use: create) - rts.group(":x") { rt in - rts.delete(use: delete) - } - ///Add custom routes here + rts.delete(":x", use: delete) + //Additional routs added here } - func index(req: Request) async throws -> [mdlTemplate] { - try await mdlTemplate.ObjQuery(req: req).all() + func index(req: Request) async throws -> Response { + guard let obj = try? await mdlTemplate.ObjQuery(req: req).all() else { + throw Abort(.badGateway, reason: "Something went wrong with this api.") + } + guard let response = try? await obj.encodeResponse(status: .ok, for: req) else{ + throw Abort(.noContent, reason: "Something went wrong decoding data. Array could not be shown.") + } + return response } - func read(req:Request) async throws -> mdlTemplate{ - return try await mdlTemplate.ObjQuery(req: req).first()! + func read(req:Request) async throws -> Response{ + guard let obj = try? await mdlTemplate.ObjQuery(req: req).first() else{ + throw Abort(.notFound, reason: "Template was not found.") + } + guard let response = try? await obj.encodeResponse(status: .ok, for: req) else { + throw Abort(.noContent, reason: "Something went wrong decoding object.") + } + return response } - func create(req: Request) async throws -> mdlTemplate { - let obj = try req.content.decode(mdlTemplate.self) + func create(req: Request) async throws -> Response { + + guard let obj = try? req.content.decode(mdlTemplate.self) else { + throw Abort(.notAcceptable, reason: "Template json not formatted correctly or is missing parents.") + } + var exists = false if(obj.id != nil){ obj._$idExists = true + exists = true + } + guard let _ = try? await obj.save(on: req.db) else{ + throw Abort(.conflict, reason: "Something conflicts in the database. Template cannot be saved.") } - try await obj.save(on: req.db) - return obj + + guard let response = try? await obj.encodeResponse(status: exists ? .accepted : .created, for: req) else{ + throw Abort(.noContent, reason: "Something went wrong decoding data. Template has been saved.") + } + return response } - func delete(req: Request) async throws -> HTTPStatus { - guard let obj = try await mdlTemplate.find(req.parameters.get("id"), on: req.db) else { - throw Abort(.notFound) + + func delete(req: Request) async throws -> Response { + guard let obj = try? await mdlTemplate.ObjQuery(req: req).first() else { + throw Abort(.notFound, reason: "Template's ID was not supplied. Template will not be deleted") } - try await obj.delete(on: req.db) - return .noContent + guard let _ = try? await obj.delete(on: req.db) else{ + throw Abort(.conflict, reason: "There was a conflict deleting Template. Make sure it has not children.") + } + guard let response = try? await obj.encodeResponse(status: .ok, for: req) else{ + throw Abort(.noContent, reason: "Something went wrong decoding data. Template has been deleted.") + } + return response } } + diff --git a/Sources/App/Controllers/TemplateControllers/cntrlTemplateCategory.swift b/Sources/App/Controllers/TemplateControllers/cntrlTemplateCategory.swift index d62882f..4ab3a2e 100644 --- a/Sources/App/Controllers/TemplateControllers/cntrlTemplateCategory.swift +++ b/Sources/App/Controllers/TemplateControllers/cntrlTemplateCategory.swift @@ -2,11 +2,14 @@ // cntrlTemplateCategory.swift // // -// Created by Michiel Carman on 4/1/24. +// Created by Michiel Carman on 4/5/2024. // import Fluent import Vapor +import Crypto + + struct cntrlTemplateCategory: RouteCollection { func boot(routes: RoutesBuilder) throws { @@ -14,34 +17,62 @@ struct cntrlTemplateCategory: RouteCollection { rts.get(use: index) rts.get(":x", use: read) rts.post(use: create) - rts.group(":x") { rt in - rts.delete(use: delete) - } - ///Add custom routes here + rts.delete(":x", use: delete) + //Additional routs added here } - func index(req: Request) async throws -> [mdlTemplateCategory] { - try await mdlTemplateCategory.ObjQuery(req: req).all() + func index(req: Request) async throws -> Response { + guard let obj = try? await mdlTemplateCategory.ObjQuery(req: req).all() else { + throw Abort(.badGateway, reason: "Something went wrong with this api.") + } + guard let response = try? await obj.encodeResponse(status: .ok, for: req) else{ + throw Abort(.noContent, reason: "Something went wrong decoding data. Array could not be shown.") + } + return response } - func read(req:Request) async throws -> mdlTemplateCategory{ - return try await mdlTemplateCategory.ObjQuery(req: req).first()! + func read(req:Request) async throws -> Response{ + guard let obj = try? await mdlTemplateCategory.ObjQuery(req: req).first() else{ + throw Abort(.notFound, reason: "TemplateCategory was not found.") + } + guard let response = try? await obj.encodeResponse(status: .ok, for: req) else { + throw Abort(.noContent, reason: "Something went wrong decoding object.") + } + return response } - func create(req: Request) async throws -> mdlTemplateCategory { - let obj = try req.content.decode(mdlTemplateCategory.self) + func create(req: Request) async throws -> Response { + + guard let obj = try? req.content.decode(mdlTemplateCategory.self) else { + throw Abort(.notAcceptable, reason: "TemplateCategory json not formatted correctly or is missing parents.") + } + var exists = false if(obj.id != nil){ obj._$idExists = true + exists = true + } + guard let _ = try? await obj.save(on: req.db) else{ + throw Abort(.conflict, reason: "Something conflicts in the database. TemplateCategory cannot be saved.") } - try await obj.save(on: req.db) - return obj + + guard let response = try? await obj.encodeResponse(status: exists ? .accepted : .created, for: req) else{ + throw Abort(.noContent, reason: "Something went wrong decoding data. TemplateCategory has been saved.") + } + return response } - func delete(req: Request) async throws -> HTTPStatus { - guard let obj = try await mdlTemplateCategory.find(req.parameters.get("id"), on: req.db) else { - throw Abort(.notFound) + + func delete(req: Request) async throws -> Response { + guard let obj = try? await mdlTemplateCategory.ObjQuery(req: req).first() else { + throw Abort(.notFound, reason: "TemplateCategory's ID was not supplied. TemplateCategory will not be deleted") } - try await obj.delete(on: req.db) - return .noContent + guard let _ = try? await obj.delete(on: req.db) else{ + throw Abort(.conflict, reason: "There was a conflict deleting TemplateCategory. Make sure it has not children.") + } + guard let response = try? await obj.encodeResponse(status: .ok, for: req) else{ + throw Abort(.noContent, reason: "Something went wrong decoding data. TemplateCategory has been deleted.") + } + return response } } + diff --git a/Sources/App/Controllers/TemplateControllers/cntrlTemplateItemSpec.swift b/Sources/App/Controllers/TemplateControllers/cntrlTemplateItemSpec.swift index dee5363..1d88473 100644 --- a/Sources/App/Controllers/TemplateControllers/cntrlTemplateItemSpec.swift +++ b/Sources/App/Controllers/TemplateControllers/cntrlTemplateItemSpec.swift @@ -2,11 +2,14 @@ // cntrlTemplateItemSpec.swift // // -// Created by Michiel Carman on 4/1/24. +// Created by Michiel Carman on 4/5/2024. // import Fluent import Vapor +import Crypto + + struct cntrlTemplateItemSpec: RouteCollection { func boot(routes: RoutesBuilder) throws { @@ -14,34 +17,62 @@ struct cntrlTemplateItemSpec: RouteCollection { rts.get(use: index) rts.get(":x", use: read) rts.post(use: create) - rts.group(":x") { rt in - rts.delete(use: delete) - } - ///Add custom routes here + rts.delete(":x", use: delete) + //Additional routs added here } - func index(req: Request) async throws -> [mdlTemplateItemSpec] { - try await mdlTemplateItemSpec.ObjQuery(req: req).all() + func index(req: Request) async throws -> Response { + guard let obj = try? await mdlTemplateItemSpec.ObjQuery(req: req).all() else { + throw Abort(.badGateway, reason: "Something went wrong with this api.") + } + guard let response = try? await obj.encodeResponse(status: .ok, for: req) else{ + throw Abort(.noContent, reason: "Something went wrong decoding data. Array could not be shown.") + } + return response } - func read(req:Request) async throws -> mdlTemplateItemSpec{ - return try await mdlTemplateItemSpec.ObjQuery(req: req).first()! + func read(req:Request) async throws -> Response{ + guard let obj = try? await mdlTemplateItemSpec.ObjQuery(req: req).first() else{ + throw Abort(.notFound, reason: "TemplateItemSpec was not found.") + } + guard let response = try? await obj.encodeResponse(status: .ok, for: req) else { + throw Abort(.noContent, reason: "Something went wrong decoding object.") + } + return response } - func create(req: Request) async throws -> mdlTemplateItemSpec { - let obj = try req.content.decode(mdlTemplateItemSpec.self) + func create(req: Request) async throws -> Response { + + guard let obj = try? req.content.decode(mdlTemplateItemSpec.self) else { + throw Abort(.notAcceptable, reason: "TemplateItemSpec json not formatted correctly or is missing parents.") + } + var exists = false if(obj.id != nil){ obj._$idExists = true + exists = true + } + guard let _ = try? await obj.save(on: req.db) else{ + throw Abort(.conflict, reason: "Something conflicts in the database. TemplateItemSpec cannot be saved.") } - try await obj.save(on: req.db) - return obj + + guard let response = try? await obj.encodeResponse(status: exists ? .accepted : .created, for: req) else{ + throw Abort(.noContent, reason: "Something went wrong decoding data. TemplateItemSpec has been saved.") + } + return response } - func delete(req: Request) async throws -> HTTPStatus { - guard let obj = try await mdlTemplateItemSpec.find(req.parameters.get("id"), on: req.db) else { - throw Abort(.notFound) + + func delete(req: Request) async throws -> Response { + guard let obj = try? await mdlTemplateItemSpec.ObjQuery(req: req).first() else { + throw Abort(.notFound, reason: "TemplateItemSpec's ID was not supplied. TemplateItemSpec will not be deleted") } - try await obj.delete(on: req.db) - return .noContent + guard let _ = try? await obj.delete(on: req.db) else{ + throw Abort(.conflict, reason: "There was a conflict deleting TemplateItemSpec. Make sure it has not children.") + } + guard let response = try? await obj.encodeResponse(status: .ok, for: req) else{ + throw Abort(.noContent, reason: "Something went wrong decoding data. TemplateItemSpec has been deleted.") + } + return response } } + diff --git a/Sources/App/Controllers/WorkOrderControllers/Attachments/cntrlWorkOrderAttachment.swift b/Sources/App/Controllers/WorkOrderControllers/Attachments/cntrlWorkOrderAttachment.swift index 5894edf..a16466f 100644 --- a/Sources/App/Controllers/WorkOrderControllers/Attachments/cntrlWorkOrderAttachment.swift +++ b/Sources/App/Controllers/WorkOrderControllers/Attachments/cntrlWorkOrderAttachment.swift @@ -2,11 +2,14 @@ // cntrlWorkOrderAttachment.swift // // -// Created by Michiel Carman on 4/1/24. +// Created by Michiel Carman on 4/5/2024. // import Fluent import Vapor +import Crypto + + struct cntrlWorkOrderAttachment: RouteCollection { func boot(routes: RoutesBuilder) throws { @@ -14,34 +17,62 @@ struct cntrlWorkOrderAttachment: RouteCollection { rts.get(use: index) rts.get(":x", use: read) rts.post(use: create) - rts.group(":x") { rt in - rts.delete(use: delete) - } - ///Add custom routes here + rts.delete(":x", use: delete) + //Additional routs added here } - func index(req: Request) async throws -> [mdlWorkOrderAttachment] { - try await mdlWorkOrderAttachment.ObjQuery(req: req).all() + func index(req: Request) async throws -> Response { + guard let obj = try? await mdlWorkOrderAttachment.ObjQuery(req: req).all() else { + throw Abort(.badGateway, reason: "Something went wrong with this api.") + } + guard let response = try? await obj.encodeResponse(status: .ok, for: req) else{ + throw Abort(.noContent, reason: "Something went wrong decoding data. Array could not be shown.") + } + return response } - func read(req:Request) async throws -> mdlWorkOrderAttachment{ - return try await mdlWorkOrderAttachment.ObjQuery(req: req).first()! + func read(req:Request) async throws -> Response{ + guard let obj = try? await mdlWorkOrderAttachment.ObjQuery(req: req).first() else{ + throw Abort(.notFound, reason: "WorkOrderAttachment was not found.") + } + guard let response = try? await obj.encodeResponse(status: .ok, for: req) else { + throw Abort(.noContent, reason: "Something went wrong decoding object.") + } + return response } - func create(req: Request) async throws -> mdlWorkOrderAttachment { - let obj = try req.content.decode(mdlWorkOrderAttachment.self) + func create(req: Request) async throws -> Response { + + guard let obj = try? req.content.decode(mdlWorkOrderAttachment.self) else { + throw Abort(.notAcceptable, reason: "WorkOrderAttachment json not formatted correctly or is missing parents.") + } + var exists = false if(obj.id != nil){ obj._$idExists = true + exists = true + } + guard let _ = try? await obj.save(on: req.db) else{ + throw Abort(.conflict, reason: "Something conflicts in the database. WorkOrderAttachment cannot be saved.") } - try await obj.save(on: req.db) - return obj + + guard let response = try? await obj.encodeResponse(status: exists ? .accepted : .created, for: req) else{ + throw Abort(.noContent, reason: "Something went wrong decoding data. WorkOrderAttachment has been saved.") + } + return response } - func delete(req: Request) async throws -> HTTPStatus { - guard let obj = try await mdlWorkOrderAttachment.find(req.parameters.get("id"), on: req.db) else { - throw Abort(.notFound) + + func delete(req: Request) async throws -> Response { + guard let obj = try? await mdlWorkOrderAttachment.ObjQuery(req: req).first() else { + throw Abort(.notFound, reason: "WorkOrderAttachment's ID was not supplied. WorkOrderAttachment will not be deleted") } - try await obj.delete(on: req.db) - return .noContent + guard let _ = try? await obj.delete(on: req.db) else{ + throw Abort(.conflict, reason: "There was a conflict deleting WorkOrderAttachment. Make sure it has not children.") + } + guard let response = try? await obj.encodeResponse(status: .ok, for: req) else{ + throw Abort(.noContent, reason: "Something went wrong decoding data. WorkOrderAttachment has been deleted.") + } + return response } } + diff --git a/Sources/App/Controllers/WorkOrderControllers/Consumptions/cntrlConsumption.swift b/Sources/App/Controllers/WorkOrderControllers/Consumptions/cntrlConsumption.swift index 652c11d..3fd91ee 100644 --- a/Sources/App/Controllers/WorkOrderControllers/Consumptions/cntrlConsumption.swift +++ b/Sources/App/Controllers/WorkOrderControllers/Consumptions/cntrlConsumption.swift @@ -2,11 +2,14 @@ // cntrlConsumption.swift // // -// Created by Michiel Carman on 4/1/24. +// Created by Michiel Carman on 4/5/2024. // import Fluent import Vapor +import Crypto + + struct cntrlConsumption: RouteCollection { func boot(routes: RoutesBuilder) throws { @@ -14,34 +17,62 @@ struct cntrlConsumption: RouteCollection { rts.get(use: index) rts.get(":x", use: read) rts.post(use: create) - rts.group(":x") { rt in - rts.delete(use: delete) - } - ///Add custom routes here + rts.delete(":x", use: delete) + //Additional routs added here } - func index(req: Request) async throws -> [mdlConsumption] { - try await mdlConsumption.ObjQuery(req: req).all() + func index(req: Request) async throws -> Response { + guard let obj = try? await mdlConsumption.ObjQuery(req: req).all() else { + throw Abort(.badGateway, reason: "Something went wrong with this api.") + } + guard let response = try? await obj.encodeResponse(status: .ok, for: req) else{ + throw Abort(.noContent, reason: "Something went wrong decoding data. Array could not be shown.") + } + return response } - func read(req:Request) async throws -> mdlConsumption{ - return try await mdlConsumption.ObjQuery(req: req).first()! + func read(req:Request) async throws -> Response{ + guard let obj = try? await mdlConsumption.ObjQuery(req: req).first() else{ + throw Abort(.notFound, reason: "Consumption was not found.") + } + guard let response = try? await obj.encodeResponse(status: .ok, for: req) else { + throw Abort(.noContent, reason: "Something went wrong decoding object.") + } + return response } - func create(req: Request) async throws -> mdlConsumption { - let obj = try req.content.decode(mdlConsumption.self) + func create(req: Request) async throws -> Response { + + guard let obj = try? req.content.decode(mdlConsumption.self) else { + throw Abort(.notAcceptable, reason: "Consumption json not formatted correctly or is missing parents.") + } + var exists = false if(obj.id != nil){ obj._$idExists = true + exists = true + } + guard let _ = try? await obj.save(on: req.db) else{ + throw Abort(.conflict, reason: "Something conflicts in the database. Consumption cannot be saved.") } - try await obj.save(on: req.db) - return obj + + guard let response = try? await obj.encodeResponse(status: exists ? .accepted : .created, for: req) else{ + throw Abort(.noContent, reason: "Something went wrong decoding data. Consumption has been saved.") + } + return response } - func delete(req: Request) async throws -> HTTPStatus { - guard let obj = try await mdlConsumption.find(req.parameters.get("id"), on: req.db) else { - throw Abort(.notFound) + + func delete(req: Request) async throws -> Response { + guard let obj = try? await mdlConsumption.ObjQuery(req: req).first() else { + throw Abort(.notFound, reason: "Consumption's ID was not supplied. Consumption will not be deleted") } - try await obj.delete(on: req.db) - return .noContent + guard let _ = try? await obj.delete(on: req.db) else{ + throw Abort(.conflict, reason: "There was a conflict deleting Consumption. Make sure it has not children.") + } + guard let response = try? await obj.encodeResponse(status: .ok, for: req) else{ + throw Abort(.noContent, reason: "Something went wrong decoding data. Consumption has been deleted.") + } + return response } } + diff --git a/Sources/App/Controllers/WorkOrderControllers/Inspections/cntrlInspection.swift b/Sources/App/Controllers/WorkOrderControllers/Inspections/cntrlInspection.swift index c04777f..eb71f95 100644 --- a/Sources/App/Controllers/WorkOrderControllers/Inspections/cntrlInspection.swift +++ b/Sources/App/Controllers/WorkOrderControllers/Inspections/cntrlInspection.swift @@ -2,11 +2,14 @@ // cntrlInspection.swift // // -// Created by Michiel Carman on 4/1/24. +// Created by Michiel Carman on 4/5/2024. // import Fluent import Vapor +import Crypto + + struct cntrlInspection: RouteCollection { func boot(routes: RoutesBuilder) throws { @@ -14,34 +17,62 @@ struct cntrlInspection: RouteCollection { rts.get(use: index) rts.get(":x", use: read) rts.post(use: create) - rts.group(":x") { rt in - rts.delete(use: delete) - } - ///Add custom routes here + rts.delete(":x", use: delete) + //Additional routs added here } - func index(req: Request) async throws -> [mdlInspection] { - try await mdlInspection.ObjQuery(req: req).all() + func index(req: Request) async throws -> Response { + guard let obj = try? await mdlInspection.ObjQuery(req: req).all() else { + throw Abort(.badGateway, reason: "Something went wrong with this api.") + } + guard let response = try? await obj.encodeResponse(status: .ok, for: req) else{ + throw Abort(.noContent, reason: "Something went wrong decoding data. Array could not be shown.") + } + return response } - func read(req:Request) async throws -> mdlInspection{ - return try await mdlInspection.ObjQuery(req: req).first()! + func read(req:Request) async throws -> Response{ + guard let obj = try? await mdlInspection.ObjQuery(req: req).first() else{ + throw Abort(.notFound, reason: "Inspection was not found.") + } + guard let response = try? await obj.encodeResponse(status: .ok, for: req) else { + throw Abort(.noContent, reason: "Something went wrong decoding object.") + } + return response } - func create(req: Request) async throws -> mdlInspection { - let obj = try req.content.decode(mdlInspection.self) + func create(req: Request) async throws -> Response { + + guard let obj = try? req.content.decode(mdlInspection.self) else { + throw Abort(.notAcceptable, reason: "Inspection json not formatted correctly or is missing parents.") + } + var exists = false if(obj.id != nil){ obj._$idExists = true + exists = true + } + guard let _ = try? await obj.save(on: req.db) else{ + throw Abort(.conflict, reason: "Something conflicts in the database. Inspection cannot be saved.") } - try await obj.save(on: req.db) - return obj + + guard let response = try? await obj.encodeResponse(status: exists ? .accepted : .created, for: req) else{ + throw Abort(.noContent, reason: "Something went wrong decoding data. Inspection has been saved.") + } + return response } - func delete(req: Request) async throws -> HTTPStatus { - guard let obj = try await mdlInspection.find(req.parameters.get("id"), on: req.db) else { - throw Abort(.notFound) + + func delete(req: Request) async throws -> Response { + guard let obj = try? await mdlInspection.ObjQuery(req: req).first() else { + throw Abort(.notFound, reason: "Inspection's ID was not supplied. Inspection will not be deleted") } - try await obj.delete(on: req.db) - return .noContent + guard let _ = try? await obj.delete(on: req.db) else{ + throw Abort(.conflict, reason: "There was a conflict deleting Inspection. Make sure it has not children.") + } + guard let response = try? await obj.encodeResponse(status: .ok, for: req) else{ + throw Abort(.noContent, reason: "Something went wrong decoding data. Inspection has been deleted.") + } + return response } } + diff --git a/Sources/App/Controllers/WorkOrderControllers/Inspections/cntrlInspectionCategory.swift b/Sources/App/Controllers/WorkOrderControllers/Inspections/cntrlInspectionCategory.swift index 672585b..6b4451c 100644 --- a/Sources/App/Controllers/WorkOrderControllers/Inspections/cntrlInspectionCategory.swift +++ b/Sources/App/Controllers/WorkOrderControllers/Inspections/cntrlInspectionCategory.swift @@ -2,11 +2,14 @@ // cntrlInspectionCategory.swift // // -// Created by Michiel Carman on 4/1/24. +// Created by Michiel Carman on 4/5/2024. // import Fluent import Vapor +import Crypto + + struct cntrlInspectionCategory: RouteCollection { func boot(routes: RoutesBuilder) throws { @@ -14,34 +17,62 @@ struct cntrlInspectionCategory: RouteCollection { rts.get(use: index) rts.get(":x", use: read) rts.post(use: create) - rts.group(":x") { rt in - rts.delete(use: delete) - } - ///Add custom routes here + rts.delete(":x", use: delete) + //Additional routs added here } - func index(req: Request) async throws -> [mdlInspectionCategory] { - return try await mdlInspectionCategory.ObjQuery(req: req).all() + func index(req: Request) async throws -> Response { + guard let obj = try? await mdlInspectionCategory.ObjQuery(req: req).all() else { + throw Abort(.badGateway, reason: "Something went wrong with this api.") + } + guard let response = try? await obj.encodeResponse(status: .ok, for: req) else{ + throw Abort(.noContent, reason: "Something went wrong decoding data. Array could not be shown.") + } + return response } - func read(req:Request) async throws -> mdlInspectionCategory{ - return try await mdlInspectionCategory.ObjQuery(req: req).first()! + func read(req:Request) async throws -> Response{ + guard let obj = try? await mdlInspectionCategory.ObjQuery(req: req).first() else{ + throw Abort(.notFound, reason: "InspectionCategory was not found.") + } + guard let response = try? await obj.encodeResponse(status: .ok, for: req) else { + throw Abort(.noContent, reason: "Something went wrong decoding object.") + } + return response } - func create(req: Request) async throws -> mdlInspectionCategory { - let obj = try req.content.decode(mdlInspectionCategory.self) + func create(req: Request) async throws -> Response { + + guard let obj = try? req.content.decode(mdlInspectionCategory.self) else { + throw Abort(.notAcceptable, reason: "InspectionCategory json not formatted correctly or is missing parents.") + } + var exists = false if(obj.id != nil){ obj._$idExists = true + exists = true + } + guard let _ = try? await obj.save(on: req.db) else{ + throw Abort(.conflict, reason: "Something conflicts in the database. InspectionCategory cannot be saved.") } - try await obj.save(on: req.db) - return obj + + guard let response = try? await obj.encodeResponse(status: exists ? .accepted : .created, for: req) else{ + throw Abort(.noContent, reason: "Something went wrong decoding data. InspectionCategory has been saved.") + } + return response } - func delete(req: Request) async throws -> HTTPStatus { - guard let obj = try await mdlInspectionCategory.find(req.parameters.get("id"), on: req.db) else { - throw Abort(.notFound) + + func delete(req: Request) async throws -> Response { + guard let obj = try? await mdlInspectionCategory.ObjQuery(req: req).first() else { + throw Abort(.notFound, reason: "InspectionCategory's ID was not supplied. <#Name#> will not be deleted") } - try await obj.delete(on: req.db) - return .noContent + guard let _ = try? await obj.delete(on: req.db) else{ + throw Abort(.conflict, reason: "There was a conflict deleting InspectionCategory. Make sure it has not children.") + } + guard let response = try? await obj.encodeResponse(status: .ok, for: req) else{ + throw Abort(.noContent, reason: "Something went wrong decoding data. InspectionCategory has been deleted.") + } + return response } } + diff --git a/Sources/App/Controllers/WorkOrderControllers/Inspections/cntrlInspectionItem.swift b/Sources/App/Controllers/WorkOrderControllers/Inspections/cntrlInspectionItem.swift index 12f3b59..5d09fab 100644 --- a/Sources/App/Controllers/WorkOrderControllers/Inspections/cntrlInspectionItem.swift +++ b/Sources/App/Controllers/WorkOrderControllers/Inspections/cntrlInspectionItem.swift @@ -2,11 +2,14 @@ // cntrlInspectionItem.swift // // -// Created by Michiel Carman on 4/1/24. +// Created by Michiel Carman on 4/5/2024. // import Fluent import Vapor +import Crypto + + struct cntrlInspectionItem: RouteCollection { func boot(routes: RoutesBuilder) throws { @@ -14,34 +17,62 @@ struct cntrlInspectionItem: RouteCollection { rts.get(use: index) rts.get(":x", use: read) rts.post(use: create) - rts.group(":x") { rt in - rts.delete(use: delete) - } - ///Add custom routes here + rts.delete(":x", use: delete) + //Additional routs added here } - func index(req: Request) async throws -> [mdlInspectionItem] { - try await mdlInspectionItem.ObjQuery(req: req).all() + func index(req: Request) async throws -> Response { + guard let obj = try? await mdlInspectionItem.ObjQuery(req: req).all() else { + throw Abort(.badGateway, reason: "Something went wrong with this api.") + } + guard let response = try? await obj.encodeResponse(status: .ok, for: req) else{ + throw Abort(.noContent, reason: "Something went wrong decoding data. Array could not be shown.") + } + return response } - func read(req:Request) async throws -> mdlInspectionItem{ - return try await mdlInspectionItem.ObjQuery(req: req).first()! + func read(req:Request) async throws -> Response{ + guard let obj = try? await mdlInspectionItem.ObjQuery(req: req).first() else{ + throw Abort(.notFound, reason: "InspectionItem was not found.") + } + guard let response = try? await obj.encodeResponse(status: .ok, for: req) else { + throw Abort(.noContent, reason: "Something went wrong decoding object.") + } + return response } - func create(req: Request) async throws -> mdlInspectionItem { - let obj = try req.content.decode(mdlInspectionItem.self) + func create(req: Request) async throws -> Response { + + guard let obj = try? req.content.decode(mdlInspectionItem.self) else { + throw Abort(.notAcceptable, reason: "InspectionItem json not formatted correctly or is missing parents.") + } + var exists = false if(obj.id != nil){ obj._$idExists = true + exists = true + } + guard let _ = try? await obj.save(on: req.db) else{ + throw Abort(.conflict, reason: "Something conflicts in the database. InspectionItem cannot be saved.") } - try await obj.save(on: req.db) - return obj + + guard let response = try? await obj.encodeResponse(status: exists ? .accepted : .created, for: req) else{ + throw Abort(.noContent, reason: "Something went wrong decoding data. InspectionItem has been saved.") + } + return response } - func delete(req: Request) async throws -> HTTPStatus { - guard let obj = try await mdlInspectionItem.find(req.parameters.get("id"), on: req.db) else { - throw Abort(.notFound) + + func delete(req: Request) async throws -> Response { + guard let obj = try? await mdlInspectionItem.ObjQuery(req: req).first() else { + throw Abort(.notFound, reason: "InspectionItem's ID was not supplied. InspectionItem will not be deleted") } - try await obj.delete(on: req.db) - return .noContent + guard let _ = try? await obj.delete(on: req.db) else{ + throw Abort(.conflict, reason: "There was a conflict deleting InspectionItem. Make sure it has not children.") + } + guard let response = try? await obj.encodeResponse(status: .ok, for: req) else{ + throw Abort(.noContent, reason: "Something went wrong decoding data. InspectionItem has been deleted.") + } + return response } } + diff --git a/Sources/App/Controllers/WorkOrderControllers/Inspections/cntrlInspectionItemResponse.swift b/Sources/App/Controllers/WorkOrderControllers/Inspections/cntrlInspectionItemResponse.swift index e1a01e3..7e566cc 100644 --- a/Sources/App/Controllers/WorkOrderControllers/Inspections/cntrlInspectionItemResponse.swift +++ b/Sources/App/Controllers/WorkOrderControllers/Inspections/cntrlInspectionItemResponse.swift @@ -2,11 +2,14 @@ // cntrlInspectionItemResponse.swift // // -// Created by Michiel Carman on 4/1/24. +// Created by Michiel Carman on 4/5/2024. // import Fluent import Vapor +import Crypto + + struct cntrlInspectionItemResponse: RouteCollection { func boot(routes: RoutesBuilder) throws { @@ -14,34 +17,62 @@ struct cntrlInspectionItemResponse: RouteCollection { rts.get(use: index) rts.get(":x", use: read) rts.post(use: create) - rts.group(":x") { rt in - rts.delete(use: delete) - } - ///Add custom routes here + rts.delete(":x", use: delete) + //Additional routs added here } - func index(req: Request) async throws -> [mdlInspectionItemResponse] { - try await mdlInspectionItemResponse.ObjQuery(req: req).all() + func index(req: Request) async throws -> Response { + guard let obj = try? await mdlInspectionItemResponse.ObjQuery(req: req).all() else { + throw Abort(.badGateway, reason: "Something went wrong with this api.") + } + guard let response = try? await obj.encodeResponse(status: .ok, for: req) else{ + throw Abort(.noContent, reason: "Something went wrong decoding data. Array could not be shown.") + } + return response } - func read(req:Request) async throws -> mdlInspectionItemResponse{ - return try await mdlInspectionItemResponse.ObjQuery(req: req).first()! + func read(req:Request) async throws -> Response{ + guard let obj = try? await mdlInspectionItemResponse.ObjQuery(req: req).first() else{ + throw Abort(.notFound, reason: "InspectionItemResponse was not found.") + } + guard let response = try? await obj.encodeResponse(status: .ok, for: req) else { + throw Abort(.noContent, reason: "Something went wrong decoding object.") + } + return response } - func create(req: Request) async throws -> mdlInspectionItemResponse { - let obj = try req.content.decode(mdlInspectionItemResponse.self) + func create(req: Request) async throws -> Response { + + guard let obj = try? req.content.decode(mdlInspectionItemResponse.self) else { + throw Abort(.notAcceptable, reason: "InspectionItemResponse json not formatted correctly or is missing parents.") + } + var exists = false if(obj.id != nil){ obj._$idExists = true + exists = true + } + guard let _ = try? await obj.save(on: req.db) else{ + throw Abort(.conflict, reason: "Something conflicts in the database. InspectionItemResponse cannot be saved.") } - try await obj.save(on: req.db) - return obj + + guard let response = try? await obj.encodeResponse(status: exists ? .accepted : .created, for: req) else{ + throw Abort(.noContent, reason: "Something went wrong decoding data. InspectionItemResponse has been saved.") + } + return response } - func delete(req: Request) async throws -> HTTPStatus { - guard let obj = try await mdlInspectionItemResponse.find(req.parameters.get("id"), on: req.db) else { - throw Abort(.notFound) + + func delete(req: Request) async throws -> Response { + guard let obj = try? await mdlInspectionItemResponse.ObjQuery(req: req).first() else { + throw Abort(.notFound, reason: "InspectionItemResponse's ID was not supplied. InspectionItemResponse will not be deleted") } - try await obj.delete(on: req.db) - return .noContent + guard let _ = try? await obj.delete(on: req.db) else{ + throw Abort(.conflict, reason: "There was a conflict deleting InspectionItemResponse. Make sure it has not children.") + } + guard let response = try? await obj.encodeResponse(status: .ok, for: req) else{ + throw Abort(.noContent, reason: "Something went wrong decoding data. InspectionItemResponse has been deleted.") + } + return response } } + diff --git a/Sources/App/Controllers/WorkOrderControllers/Inspections/cntrlInspectionItemSpec.swift b/Sources/App/Controllers/WorkOrderControllers/Inspections/cntrlInspectionItemSpec.swift index b52addc..304a5ea 100644 --- a/Sources/App/Controllers/WorkOrderControllers/Inspections/cntrlInspectionItemSpec.swift +++ b/Sources/App/Controllers/WorkOrderControllers/Inspections/cntrlInspectionItemSpec.swift @@ -2,11 +2,14 @@ // cntrlInspectionItemSpec.swift // // -// Created by Michiel Carman on 4/1/24. +// Created by Michiel Carman on 4/5/2024. // import Fluent import Vapor +import Crypto + + struct cntrlInspectionItemSpec: RouteCollection { func boot(routes: RoutesBuilder) throws { @@ -14,34 +17,62 @@ struct cntrlInspectionItemSpec: RouteCollection { rts.get(use: index) rts.get(":x", use: read) rts.post(use: create) - rts.group(":x") { rt in - rts.delete(use: delete) - } - ///Add custom routes here + rts.delete(":x", use: delete) + //Additional routs added here } - func index(req: Request) async throws -> [mdlInspectionItemSpec] { - try await mdlInspectionItemSpec.ObjQuery(req: req).all() + func index(req: Request) async throws -> Response { + guard let obj = try? await mdlInspectionItemSpec.ObjQuery(req: req).all() else { + throw Abort(.badGateway, reason: "Something went wrong with this api.") + } + guard let response = try? await obj.encodeResponse(status: .ok, for: req) else{ + throw Abort(.noContent, reason: "Something went wrong decoding data. Array could not be shown.") + } + return response } - func read(req:Request) async throws -> mdlInspectionItemSpec{ - return try await mdlInspectionItemSpec.ObjQuery(req: req).first()! + func read(req:Request) async throws -> Response{ + guard let obj = try? await mdlInspectionItemSpec.ObjQuery(req: req).first() else{ + throw Abort(.notFound, reason: "InspectionItemSpec was not found.") + } + guard let response = try? await obj.encodeResponse(status: .ok, for: req) else { + throw Abort(.noContent, reason: "Something went wrong decoding object.") + } + return response } - func create(req: Request) async throws -> mdlInspectionItemSpec { - let obj = try req.content.decode(mdlInspectionItemSpec.self) + func create(req: Request) async throws -> Response { + + guard let obj = try? req.content.decode(mdlInspectionItemSpec.self) else { + throw Abort(.notAcceptable, reason: "InspectionItemSpec json not formatted correctly or is missing parents.") + } + var exists = false if(obj.id != nil){ obj._$idExists = true + exists = true + } + guard let _ = try? await obj.save(on: req.db) else{ + throw Abort(.conflict, reason: "Something conflicts in the database. InspectionItemSpec cannot be saved.") } - try await obj.save(on: req.db) - return obj + + guard let response = try? await obj.encodeResponse(status: exists ? .accepted : .created, for: req) else{ + throw Abort(.noContent, reason: "Something went wrong decoding data. InspectionItemSpec has been saved.") + } + return response } - func delete(req: Request) async throws -> HTTPStatus { - guard let obj = try await mdlInspectionItemSpec.find(req.parameters.get("id"), on: req.db) else { - throw Abort(.notFound) + + func delete(req: Request) async throws -> Response { + guard let obj = try? await mdlInspectionItemSpec.ObjQuery(req: req).first() else { + throw Abort(.notFound, reason: "InspectionItemSpec's ID was not supplied. InspectionItemSpec will not be deleted") } - try await obj.delete(on: req.db) - return .noContent + guard let _ = try? await obj.delete(on: req.db) else{ + throw Abort(.conflict, reason: "There was a conflict deleting InspectionItemSpec. Make sure it has not children.") + } + guard let response = try? await obj.encodeResponse(status: .ok, for: req) else{ + throw Abort(.noContent, reason: "Something went wrong decoding data. InspectionItemSpec has been deleted.") + } + return response } } + diff --git a/Sources/App/Controllers/WorkOrderControllers/Inspections/cntrlInspectionItemSpecItem.swift b/Sources/App/Controllers/WorkOrderControllers/Inspections/cntrlInspectionItemSpecItem.swift index 2354746..3d666be 100644 --- a/Sources/App/Controllers/WorkOrderControllers/Inspections/cntrlInspectionItemSpecItem.swift +++ b/Sources/App/Controllers/WorkOrderControllers/Inspections/cntrlInspectionItemSpecItem.swift @@ -2,11 +2,14 @@ // cntrlInspectionItemSpecItem.swift // // -// Created by Michiel Carman on 4/1/24. +// Created by Michiel Carman on 4/5/2024. // import Fluent import Vapor +import Crypto + + struct cntrlInspectionItemSpecItem: RouteCollection { func boot(routes: RoutesBuilder) throws { @@ -14,34 +17,62 @@ struct cntrlInspectionItemSpecItem: RouteCollection { rts.get(use: index) rts.get(":x", use: read) rts.post(use: create) - rts.group(":x") { rt in - rts.delete(use: delete) - } - ///Add custom routes here + rts.delete(":x", use: delete) + //Additional routs added here } - func index(req: Request) async throws -> [mdlInspectionItemSpecItem] { - try await mdlInspectionItemSpecItem.ObjQuery(req: req).all() + func index(req: Request) async throws -> Response { + guard let obj = try? await mdlInspectionItemSpecItem.ObjQuery(req: req).all() else { + throw Abort(.badGateway, reason: "Something went wrong with this api.") + } + guard let response = try? await obj.encodeResponse(status: .ok, for: req) else{ + throw Abort(.noContent, reason: "Something went wrong decoding data. Array could not be shown.") + } + return response } - func read(req:Request) async throws -> mdlInspectionItemSpecItem{ - return try await mdlInspectionItemSpecItem.ObjQuery(req: req).first()! + func read(req:Request) async throws -> Response{ + guard let obj = try? await mdlInspectionItemSpecItem.ObjQuery(req: req).first() else{ + throw Abort(.notFound, reason: "InspectionItemSpecItem was not found.") + } + guard let response = try? await obj.encodeResponse(status: .ok, for: req) else { + throw Abort(.noContent, reason: "Something went wrong decoding object.") + } + return response } - func create(req: Request) async throws -> mdlInspectionItemSpecItem { - let obj = try req.content.decode(mdlInspectionItemSpecItem.self) + func create(req: Request) async throws -> Response { + + guard let obj = try? req.content.decode(mdlInspectionItemSpecItem.self) else { + throw Abort(.notAcceptable, reason: "InspectionItemSpecItem json not formatted correctly or is missing parents.") + } + var exists = false if(obj.id != nil){ obj._$idExists = true + exists = true + } + guard let _ = try? await obj.save(on: req.db) else{ + throw Abort(.conflict, reason: "Something conflicts in the database. InspectionItemSpecItem cannot be saved.") } - try await obj.save(on: req.db) - return obj + + guard let response = try? await obj.encodeResponse(status: exists ? .accepted : .created, for: req) else{ + throw Abort(.noContent, reason: "Something went wrong decoding data. InspectionItemSpecItem has been saved.") + } + return response } - func delete(req: Request) async throws -> HTTPStatus { - guard let obj = try await mdlInspectionItemSpecItem.find(req.parameters.get("id"), on: req.db) else { - throw Abort(.notFound) + + func delete(req: Request) async throws -> Response { + guard let obj = try? await mdlInspectionItemSpecItem.ObjQuery(req: req).first() else { + throw Abort(.notFound, reason: "InspectionItemSpecItem's ID was not supplied. InspectionItemSpecItem will not be deleted") } - try await obj.delete(on: req.db) - return .noContent + guard let _ = try? await obj.delete(on: req.db) else{ + throw Abort(.conflict, reason: "There was a conflict deleting InspectionItemSpecItem. Make sure it has not children.") + } + guard let response = try? await obj.encodeResponse(status: .ok, for: req) else{ + throw Abort(.noContent, reason: "Something went wrong decoding data. InspectionItemSpecItem has been deleted.") + } + return response } } + diff --git a/Sources/App/Controllers/WorkOrderControllers/Inspections/cntrlInspectionItemSpecItemTest.swift b/Sources/App/Controllers/WorkOrderControllers/Inspections/cntrlInspectionItemSpecItemTest.swift index 41239b8..f6563e8 100644 --- a/Sources/App/Controllers/WorkOrderControllers/Inspections/cntrlInspectionItemSpecItemTest.swift +++ b/Sources/App/Controllers/WorkOrderControllers/Inspections/cntrlInspectionItemSpecItemTest.swift @@ -2,11 +2,14 @@ // cntrlInspectionItemSpecItemTest.swift // // -// Created by Michiel Carman on 4/1/24. +// Created by Michiel Carman on 4/5/2024. // import Fluent import Vapor +import Crypto + + struct cntrlInspectionItemSpecItemTest: RouteCollection { func boot(routes: RoutesBuilder) throws { @@ -14,34 +17,62 @@ struct cntrlInspectionItemSpecItemTest: RouteCollection { rts.get(use: index) rts.get(":x", use: read) rts.post(use: create) - rts.group(":x") { rt in - rts.delete(use: delete) - } - ///Add custom routes here + rts.delete(":x", use: delete) + //Additional routs added here } - func index(req: Request) async throws -> [mdlInspectionItemSpecItemTest] { - try await mdlInspectionItemSpecItemTest.ObjQuery(req: req).all() + func index(req: Request) async throws -> Response { + guard let obj = try? await mdlInspectionItemSpecItemTest.ObjQuery(req: req).all() else { + throw Abort(.badGateway, reason: "Something went wrong with this api.") + } + guard let response = try? await obj.encodeResponse(status: .ok, for: req) else{ + throw Abort(.noContent, reason: "Something went wrong decoding data. Array could not be shown.") + } + return response } - func read(req:Request) async throws -> mdlInspectionItemSpecItemTest{ - return try await mdlInspectionItemSpecItemTest.ObjQuery(req: req).first()! + func read(req:Request) async throws -> Response{ + guard let obj = try? await mdlInspectionItemSpecItemTest.ObjQuery(req: req).first() else{ + throw Abort(.notFound, reason: "InspectionItemSpecItemTest was not found.") + } + guard let response = try? await obj.encodeResponse(status: .ok, for: req) else { + throw Abort(.noContent, reason: "Something went wrong decoding object.") + } + return response } - func create(req: Request) async throws -> mdlInspectionItemSpecItemTest { - let obj = try req.content.decode(mdlInspectionItemSpecItemTest.self) + func create(req: Request) async throws -> Response { + + guard let obj = try? req.content.decode(mdlInspectionItemSpecItemTest.self) else { + throw Abort(.notAcceptable, reason: "InspectionItemSpecItemTest json not formatted correctly or is missing parents.") + } + var exists = false if(obj.id != nil){ obj._$idExists = true + exists = true + } + guard let _ = try? await obj.save(on: req.db) else{ + throw Abort(.conflict, reason: "Something conflicts in the database. InspectionItemSpecItemTest cannot be saved.") } - try await obj.save(on: req.db) - return obj + + guard let response = try? await obj.encodeResponse(status: exists ? .accepted : .created, for: req) else{ + throw Abort(.noContent, reason: "Something went wrong decoding data. InspectionItemSpecItemTest has been saved.") + } + return response } - func delete(req: Request) async throws -> HTTPStatus { - guard let obj = try await mdlInspectionItemSpecItemTest.find(req.parameters.get("id"), on: req.db) else { - throw Abort(.notFound) + + func delete(req: Request) async throws -> Response { + guard let obj = try? await mdlInspectionItemSpecItemTest.ObjQuery(req: req).first() else { + throw Abort(.notFound, reason: "InspectionItemSpecItemTest's ID was not supplied. InspectionItemSpecItemTest will not be deleted") } - try await obj.delete(on: req.db) - return .noContent + guard let _ = try? await obj.delete(on: req.db) else{ + throw Abort(.conflict, reason: "There was a conflict deleting InspectionItemSpecItemTest. Make sure it has not children.") + } + guard let response = try? await obj.encodeResponse(status: .ok, for: req) else{ + throw Abort(.noContent, reason: "Something went wrong decoding data. InspectionItemSpecItemTest has been deleted.") + } + return response } } + diff --git a/Sources/App/Controllers/WorkOrderControllers/Inspections/cntrlInspectionItemSpecItemValue.swift b/Sources/App/Controllers/WorkOrderControllers/Inspections/cntrlInspectionItemSpecItemValue.swift index 26312ba..87a72dd 100644 --- a/Sources/App/Controllers/WorkOrderControllers/Inspections/cntrlInspectionItemSpecItemValue.swift +++ b/Sources/App/Controllers/WorkOrderControllers/Inspections/cntrlInspectionItemSpecItemValue.swift @@ -2,11 +2,14 @@ // cntrlInspectionItemSpecItemValue.swift // // -// Created by Michiel Carman on 4/1/24. +// Created by Michiel Carman on 4/5/2024. // import Fluent import Vapor +import Crypto + + struct cntrlInspectionItemSpecItemValue: RouteCollection { func boot(routes: RoutesBuilder) throws { @@ -14,34 +17,62 @@ struct cntrlInspectionItemSpecItemValue: RouteCollection { rts.get(use: index) rts.get(":x", use: read) rts.post(use: create) - rts.group(":x") { rt in - rts.delete(use: delete) - } - ///Add custom routes here + rts.delete(":x", use: delete) + //Additional routs added here } - func index(req: Request) async throws -> [mdlInspectionItemSpecItemValue] { - try await mdlInspectionItemSpecItemValue.ObjQuery(req: req).all() + func index(req: Request) async throws -> Response { + guard let obj = try? await mdlInspectionItemSpecItemValue.ObjQuery(req: req).all() else { + throw Abort(.badGateway, reason: "Something went wrong with this api.") + } + guard let response = try? await obj.encodeResponse(status: .ok, for: req) else{ + throw Abort(.noContent, reason: "Something went wrong decoding data. Array could not be shown.") + } + return response } - func read(req:Request) async throws -> mdlInspectionItemSpecItemValue{ - return try await mdlInspectionItemSpecItemValue.ObjQuery(req: req).first()! + func read(req:Request) async throws -> Response{ + guard let obj = try? await mdlInspectionItemSpecItemValue.ObjQuery(req: req).first() else{ + throw Abort(.notFound, reason: "InspectionItemSpecItemValue was not found.") + } + guard let response = try? await obj.encodeResponse(status: .ok, for: req) else { + throw Abort(.noContent, reason: "Something went wrong decoding object.") + } + return response } - func create(req: Request) async throws -> mdlInspectionItemSpecItemValue { - let obj = try req.content.decode(mdlInspectionItemSpecItemValue.self) + func create(req: Request) async throws -> Response { + + guard let obj = try? req.content.decode(mdlInspectionItemSpecItemValue.self) else { + throw Abort(.notAcceptable, reason: "InspectionItemSpecItemValue json not formatted correctly or is missing parents.") + } + var exists = false if(obj.id != nil){ obj._$idExists = true + exists = true + } + guard let _ = try? await obj.save(on: req.db) else{ + throw Abort(.conflict, reason: "Something conflicts in the database. InspectionItemSpecItemValue cannot be saved.") } - try await obj.save(on: req.db) - return obj + + guard let response = try? await obj.encodeResponse(status: exists ? .accepted : .created, for: req) else{ + throw Abort(.noContent, reason: "Something went wrong decoding data. InspectionItemSpecItemValue has been saved.") + } + return response } - func delete(req: Request) async throws -> HTTPStatus { - guard let obj = try await mdlInspectionItemSpecItemValue.find(req.parameters.get("id"), on: req.db) else { - throw Abort(.notFound) + + func delete(req: Request) async throws -> Response { + guard let obj = try? await mdlInspectionItemSpecItemValue.ObjQuery(req: req).first() else { + throw Abort(.notFound, reason: "InspectionItemSpecItemValue's ID was not supplied. InspectionItemSpecItemValue will not be deleted") } - try await obj.delete(on: req.db) - return .noContent + guard let _ = try? await obj.delete(on: req.db) else{ + throw Abort(.conflict, reason: "There was a conflict deleting InspectionItemSpecItemValue. Make sure it has not children.") + } + guard let response = try? await obj.encodeResponse(status: .ok, for: req) else{ + throw Abort(.noContent, reason: "Something went wrong decoding data. InspectionItemSpecItemValue has been deleted.") + } + return response } } + diff --git a/Sources/App/Controllers/WorkOrderControllers/Inspections/cntrlInspectionLevel.swift b/Sources/App/Controllers/WorkOrderControllers/Inspections/cntrlInspectionLevel.swift index bd5af64..93f51bf 100644 --- a/Sources/App/Controllers/WorkOrderControllers/Inspections/cntrlInspectionLevel.swift +++ b/Sources/App/Controllers/WorkOrderControllers/Inspections/cntrlInspectionLevel.swift @@ -2,11 +2,14 @@ // cntrlInspectionLevel.swift // // -// Created by Michiel Carman on 4/1/24. +// Created by Michiel Carman on 4/5/2024. // import Fluent import Vapor +import Crypto + + struct cntrlInspectionLevel: RouteCollection { func boot(routes: RoutesBuilder) throws { @@ -14,34 +17,62 @@ struct cntrlInspectionLevel: RouteCollection { rts.get(use: index) rts.get(":x", use: read) rts.post(use: create) - rts.group(":x") { rt in - rts.delete(use: delete) - } - ///Add custom routes here + rts.delete(":x", use: delete) + //Additional routs added here } - func index(req: Request) async throws -> [mdlInspectionLevel] { - try await mdlInspectionLevel.ObjQuery(req: req).all() + func index(req: Request) async throws -> Response { + guard let obj = try? await mdlInspectionLevel.ObjQuery(req: req).all() else { + throw Abort(.badGateway, reason: "Something went wrong with this api.") + } + guard let response = try? await obj.encodeResponse(status: .ok, for: req) else{ + throw Abort(.noContent, reason: "Something went wrong decoding data. Array could not be shown.") + } + return response } - func read(req:Request) async throws -> mdlInspectionLevel{ - return try await mdlInspectionLevel.ObjQuery(req: req).first()! + func read(req:Request) async throws -> Response{ + guard let obj = try? await mdlInspectionLevel.ObjQuery(req: req).first() else{ + throw Abort(.notFound, reason: "InspectionLevel was not found.") + } + guard let response = try? await obj.encodeResponse(status: .ok, for: req) else { + throw Abort(.noContent, reason: "Something went wrong decoding object.") + } + return response } - func create(req: Request) async throws -> mdlInspectionLevel { - let obj = try req.content.decode(mdlInspectionLevel.self) + func create(req: Request) async throws -> Response { + + guard let obj = try? req.content.decode(mdlInspectionLevel.self) else { + throw Abort(.notAcceptable, reason: "InspectionLevel json not formatted correctly or is missing parents.") + } + var exists = false if(obj.id != nil){ obj._$idExists = true + exists = true + } + guard let _ = try? await obj.save(on: req.db) else{ + throw Abort(.conflict, reason: "Something conflicts in the database. InspectionLevel cannot be saved.") } - try await obj.save(on: req.db) - return obj + + guard let response = try? await obj.encodeResponse(status: exists ? .accepted : .created, for: req) else{ + throw Abort(.noContent, reason: "Something went wrong decoding data. InspectionLevel has been saved.") + } + return response } - func delete(req: Request) async throws -> HTTPStatus { - guard let obj = try await mdlInspectionLevel.find(req.parameters.get("id"), on: req.db) else { - throw Abort(.notFound) + + func delete(req: Request) async throws -> Response { + guard let obj = try? await mdlInspectionLevel.ObjQuery(req: req).first() else { + throw Abort(.notFound, reason: "InspectionLevel's ID was not supplied. InspectionLevel will not be deleted") } - try await obj.delete(on: req.db) - return .noContent + guard let _ = try? await obj.delete(on: req.db) else{ + throw Abort(.conflict, reason: "There was a conflict deleting InspectionLevel. Make sure it has not children.") + } + guard let response = try? await obj.encodeResponse(status: .ok, for: req) else{ + throw Abort(.noContent, reason: "Something went wrong decoding data. InspectionLevel has been deleted.") + } + return response } } + diff --git a/Sources/App/Controllers/WorkOrderControllers/Inspections/cntrlInspectionType.swift b/Sources/App/Controllers/WorkOrderControllers/Inspections/cntrlInspectionType.swift index a3ffa27..09f3c47 100644 --- a/Sources/App/Controllers/WorkOrderControllers/Inspections/cntrlInspectionType.swift +++ b/Sources/App/Controllers/WorkOrderControllers/Inspections/cntrlInspectionType.swift @@ -2,11 +2,14 @@ // cntrlInspectionType.swift // // -// Created by Michiel Carman on 4/1/24. +// Created by Michiel Carman on 4/5/2024. // import Fluent import Vapor +import Crypto + + struct cntrlInspectionType: RouteCollection { func boot(routes: RoutesBuilder) throws { @@ -14,34 +17,62 @@ struct cntrlInspectionType: RouteCollection { rts.get(use: index) rts.get(":x", use: read) rts.post(use: create) - rts.group(":x") { rt in - rts.delete(use: delete) - } - ///Add custom routes here + rts.delete(":x", use: delete) + //Additional routs added here } - func index(req: Request) async throws -> [mdlInspectionType] { - try await mdlInspectionType.ObjQuery(req: req).all() + func index(req: Request) async throws -> Response { + guard let obj = try? await mdlInspectionType.ObjQuery(req: req).all() else { + throw Abort(.badGateway, reason: "Something went wrong with this api.") + } + guard let response = try? await obj.encodeResponse(status: .ok, for: req) else{ + throw Abort(.noContent, reason: "Something went wrong decoding data. Array could not be shown.") + } + return response } - func read(req:Request) async throws -> mdlInspectionType{ - return try await mdlInspectionType.ObjQuery(req: req).first()! + func read(req:Request) async throws -> Response{ + guard let obj = try? await mdlInspectionType.ObjQuery(req: req).first() else{ + throw Abort(.notFound, reason: "InspectionType was not found.") + } + guard let response = try? await obj.encodeResponse(status: .ok, for: req) else { + throw Abort(.noContent, reason: "Something went wrong decoding object.") + } + return response } - func create(req: Request) async throws -> mdlInspectionType { - let obj = try req.content.decode(mdlInspectionType.self) + func create(req: Request) async throws -> Response { + + guard let obj = try? req.content.decode(mdlInspectionType.self) else { + throw Abort(.notAcceptable, reason: "InspectionType json not formatted correctly or is missing parents.") + } + var exists = false if(obj.id != nil){ obj._$idExists = true + exists = true + } + guard let _ = try? await obj.save(on: req.db) else{ + throw Abort(.conflict, reason: "Something conflicts in the database. InspectionType cannot be saved.") } - try await obj.save(on: req.db) - return obj + + guard let response = try? await obj.encodeResponse(status: exists ? .accepted : .created, for: req) else{ + throw Abort(.noContent, reason: "Something went wrong decoding data. InspectionType has been saved.") + } + return response } - func delete(req: Request) async throws -> HTTPStatus { - guard let obj = try await mdlInspectionType.find(req.parameters.get("id"), on: req.db) else { - throw Abort(.notFound) + + func delete(req: Request) async throws -> Response { + guard let obj = try? await mdlInspectionType.ObjQuery(req: req).first() else { + throw Abort(.notFound, reason: "InspectionType's ID was not supplied. InspectionType will not be deleted") } - try await obj.delete(on: req.db) - return .noContent + guard let _ = try? await obj.delete(on: req.db) else{ + throw Abort(.conflict, reason: "There was a conflict deleting InspectionType. Make sure it has not children.") + } + guard let response = try? await obj.encodeResponse(status: .ok, for: req) else{ + throw Abort(.noContent, reason: "Something went wrong decoding data. InspectionType has been deleted.") + } + return response } } + diff --git a/Sources/App/Controllers/WorkOrderControllers/Notes/cntrlNote.swift b/Sources/App/Controllers/WorkOrderControllers/Notes/cntrlNote.swift index 66549e5..807a6b4 100644 --- a/Sources/App/Controllers/WorkOrderControllers/Notes/cntrlNote.swift +++ b/Sources/App/Controllers/WorkOrderControllers/Notes/cntrlNote.swift @@ -2,11 +2,14 @@ // cntrlNote.swift // // -// Created by Michiel Carman on 4/1/24. +// Created by Michiel Carman on 4/5/2024. // import Fluent import Vapor +import Crypto + + struct cntrlNote: RouteCollection { func boot(routes: RoutesBuilder) throws { @@ -14,34 +17,62 @@ struct cntrlNote: RouteCollection { rts.get(use: index) rts.get(":x", use: read) rts.post(use: create) - rts.group(":x") { rt in - rts.delete(use: delete) - } - ///Add custom routes here + rts.delete(":x", use: delete) + //Additional routs added here } - func index(req: Request) async throws -> [mdlNote] { - try await mdlNote.ObjQuery(req: req).all() + func index(req: Request) async throws -> Response { + guard let obj = try? await mdlNote.ObjQuery(req: req).all() else { + throw Abort(.badGateway, reason: "Something went wrong with this api.") + } + guard let response = try? await obj.encodeResponse(status: .ok, for: req) else{ + throw Abort(.noContent, reason: "Something went wrong decoding data. Array could not be shown.") + } + return response } - func read(req:Request) async throws -> mdlNote{ - return try await mdlNote.ObjQuery(req: req).first()! + func read(req:Request) async throws -> Response{ + guard let obj = try? await mdlNote.ObjQuery(req: req).first() else{ + throw Abort(.notFound, reason: "Note was not found.") + } + guard let response = try? await obj.encodeResponse(status: .ok, for: req) else { + throw Abort(.noContent, reason: "Something went wrong decoding object.") + } + return response } - func create(req: Request) async throws -> mdlNote { - let obj = try req.content.decode(mdlNote.self) + func create(req: Request) async throws -> Response { + + guard let obj = try? req.content.decode(mdlNote.self) else { + throw Abort(.notAcceptable, reason: "Note json not formatted correctly or is missing parents.") + } + var exists = false if(obj.id != nil){ obj._$idExists = true + exists = true + } + guard let _ = try? await obj.save(on: req.db) else{ + throw Abort(.conflict, reason: "Something conflicts in the database. Note cannot be saved.") } - try await obj.save(on: req.db) - return obj + + guard let response = try? await obj.encodeResponse(status: exists ? .accepted : .created, for: req) else{ + throw Abort(.noContent, reason: "Something went wrong decoding data. Note has been saved.") + } + return response } - func delete(req: Request) async throws -> HTTPStatus { - guard let obj = try await mdlNote.find(req.parameters.get("id"), on: req.db) else { - throw Abort(.notFound) + + func delete(req: Request) async throws -> Response { + guard let obj = try? await mdlNote.ObjQuery(req: req).first() else { + throw Abort(.notFound, reason: "Note's ID was not supplied. Note will not be deleted") } - try await obj.delete(on: req.db) - return .noContent + guard let _ = try? await obj.delete(on: req.db) else{ + throw Abort(.conflict, reason: "There was a conflict deleting Note. Make sure it has not children.") + } + guard let response = try? await obj.encodeResponse(status: .ok, for: req) else{ + throw Abort(.noContent, reason: "Something went wrong decoding data. Note has been deleted.") + } + return response } } + diff --git a/Sources/App/Controllers/WorkOrderControllers/Parts/cntrlWorkOrderPart.swift b/Sources/App/Controllers/WorkOrderControllers/Parts/cntrlWorkOrderPart.swift index 02138b8..9666106 100644 --- a/Sources/App/Controllers/WorkOrderControllers/Parts/cntrlWorkOrderPart.swift +++ b/Sources/App/Controllers/WorkOrderControllers/Parts/cntrlWorkOrderPart.swift @@ -2,11 +2,14 @@ // cntrlWorkOrderPart.swift // // -// Created by Michiel Carman on 4/1/24. +// Created by Michiel Carman on 4/5/2024. // import Fluent import Vapor +import Crypto + + struct cntrlWorkOrderPart: RouteCollection { func boot(routes: RoutesBuilder) throws { @@ -14,34 +17,62 @@ struct cntrlWorkOrderPart: RouteCollection { rts.get(use: index) rts.get(":x", use: read) rts.post(use: create) - rts.group(":x") { rt in - rts.delete(use: delete) - } - ///Add custom routes here + rts.delete(":x", use: delete) + //Additional routs added here } - func index(req: Request) async throws -> [mdlWorkOrderPart] { - try await mdlWorkOrderPart.ObjQuery(req: req).all() + func index(req: Request) async throws -> Response { + guard let obj = try? await mdlWorkOrderPart.ObjQuery(req: req).all() else { + throw Abort(.badGateway, reason: "Something went wrong with this api.") + } + guard let response = try? await obj.encodeResponse(status: .ok, for: req) else{ + throw Abort(.noContent, reason: "Something went wrong decoding data. Array could not be shown.") + } + return response } - func read(req:Request) async throws -> mdlWorkOrderPart{ - return try await mdlWorkOrderPart.ObjQuery(req: req).first()! + func read(req:Request) async throws -> Response{ + guard let obj = try? await mdlWorkOrderPart.ObjQuery(req: req).first() else{ + throw Abort(.notFound, reason: "WorkOrderPart was not found.") + } + guard let response = try? await obj.encodeResponse(status: .ok, for: req) else { + throw Abort(.noContent, reason: "Something went wrong decoding object.") + } + return response } - func create(req: Request) async throws -> mdlWorkOrderPart { - let obj = try req.content.decode(mdlWorkOrderPart.self) + func create(req: Request) async throws -> Response { + + guard let obj = try? req.content.decode(mdlWorkOrderPart.self) else { + throw Abort(.notAcceptable, reason: "WorkOrderPart json not formatted correctly or is missing parents.") + } + var exists = false if(obj.id != nil){ obj._$idExists = true + exists = true + } + guard let _ = try? await obj.save(on: req.db) else{ + throw Abort(.conflict, reason: "Something conflicts in the database. WorkOrderPart cannot be saved.") } - try await obj.save(on: req.db) - return obj + + guard let response = try? await obj.encodeResponse(status: exists ? .accepted : .created, for: req) else{ + throw Abort(.noContent, reason: "Something went wrong decoding data. WorkOrderPart has been saved.") + } + return response } - func delete(req: Request) async throws -> HTTPStatus { - guard let obj = try await mdlWorkOrderPart.find(req.parameters.get("id"), on: req.db) else { - throw Abort(.notFound) + + func delete(req: Request) async throws -> Response { + guard let obj = try? await mdlWorkOrderPart.ObjQuery(req: req).first() else { + throw Abort(.notFound, reason: "WorkOrderPart's ID was not supplied. WorkOrderPart will not be deleted") } - try await obj.delete(on: req.db) - return .noContent + guard let _ = try? await obj.delete(on: req.db) else{ + throw Abort(.conflict, reason: "There was a conflict deleting WorkOrderPart. Make sure it has not children.") + } + guard let response = try? await obj.encodeResponse(status: .ok, for: req) else{ + throw Abort(.noContent, reason: "Something went wrong decoding data. WorkOrderPart has been deleted.") + } + return response } } + diff --git a/Sources/App/Controllers/WorkOrderControllers/SignatureControllers/cntrlSignature.swift b/Sources/App/Controllers/WorkOrderControllers/SignatureControllers/cntrlSignature.swift index b42b41e..2cb53f5 100644 --- a/Sources/App/Controllers/WorkOrderControllers/SignatureControllers/cntrlSignature.swift +++ b/Sources/App/Controllers/WorkOrderControllers/SignatureControllers/cntrlSignature.swift @@ -2,11 +2,14 @@ // cntrlSignature.swift // // -// Created by Michiel Carman on 4/1/24. +// Created by Michiel Carman on 4/5/2024. // import Fluent import Vapor +import Crypto + + struct cntrlSignature: RouteCollection { func boot(routes: RoutesBuilder) throws { @@ -14,34 +17,62 @@ struct cntrlSignature: RouteCollection { rts.get(use: index) rts.get(":x", use: read) rts.post(use: create) - rts.group(":x") { rt in - rts.delete(use: delete) - } - ///Add custom routes here + rts.delete(":x", use: delete) + //Additional routs added here } - func index(req: Request) async throws -> [mdlSignature] { - try await mdlSignature.ObjQuery(req: req).all() + func index(req: Request) async throws -> Response { + guard let obj = try? await mdlSignature.ObjQuery(req: req).all() else { + throw Abort(.badGateway, reason: "Something went wrong with this api.") + } + guard let response = try? await obj.encodeResponse(status: .ok, for: req) else{ + throw Abort(.noContent, reason: "Something went wrong decoding data. Array could not be shown.") + } + return response } - func read(req:Request) async throws -> mdlSignature{ - return try await mdlSignature.ObjQuery(req: req).first()! + func read(req:Request) async throws -> Response{ + guard let obj = try? await mdlSignature.ObjQuery(req: req).first() else{ + throw Abort(.notFound, reason: "Signature was not found.") + } + guard let response = try? await obj.encodeResponse(status: .ok, for: req) else { + throw Abort(.noContent, reason: "Something went wrong decoding object.") + } + return response } - func create(req: Request) async throws -> mdlSignature { - let obj = try req.content.decode(mdlSignature.self) + func create(req: Request) async throws -> Response { + + guard let obj = try? req.content.decode(mdlSignature.self) else { + throw Abort(.notAcceptable, reason: "Signature json not formatted correctly or is missing parents.") + } + var exists = false if(obj.id != nil){ obj._$idExists = true + exists = true + } + guard let _ = try? await obj.save(on: req.db) else{ + throw Abort(.conflict, reason: "Something conflicts in the database. Signature cannot be saved.") } - try await obj.save(on: req.db) - return obj + + guard let response = try? await obj.encodeResponse(status: exists ? .accepted : .created, for: req) else{ + throw Abort(.noContent, reason: "Something went wrong decoding data. Signature has been saved.") + } + return response } - func delete(req: Request) async throws -> HTTPStatus { - guard let obj = try await mdlSignature.find(req.parameters.get("id"), on: req.db) else { - throw Abort(.notFound) + + func delete(req: Request) async throws -> Response { + guard let obj = try? await mdlSignature.ObjQuery(req: req).first() else { + throw Abort(.notFound, reason: "Signature's ID was not supplied. Signature will not be deleted") } - try await obj.delete(on: req.db) - return .noContent + guard let _ = try? await obj.delete(on: req.db) else{ + throw Abort(.conflict, reason: "There was a conflict deleting Signature. Make sure it has not children.") + } + guard let response = try? await obj.encodeResponse(status: .ok, for: req) else{ + throw Abort(.noContent, reason: "Something went wrong decoding data. Signature has been deleted.") + } + return response } } + diff --git a/Sources/App/Controllers/WorkOrderControllers/cntrlWorkOrder.swift b/Sources/App/Controllers/WorkOrderControllers/cntrlWorkOrder.swift index e5d1cd4..46f2a93 100644 --- a/Sources/App/Controllers/WorkOrderControllers/cntrlWorkOrder.swift +++ b/Sources/App/Controllers/WorkOrderControllers/cntrlWorkOrder.swift @@ -2,11 +2,14 @@ // cntrlWorkOrder.swift // // -// Created by Michiel Carman on 4/1/24. +// Created by Michiel Carman on 4/5/2024. // import Fluent import Vapor +import Crypto + + struct cntrlWorkOrder: RouteCollection { func boot(routes: RoutesBuilder) throws { @@ -14,34 +17,62 @@ struct cntrlWorkOrder: RouteCollection { rts.get(use: index) rts.get(":x", use: read) rts.post(use: create) - rts.group(":x") { rt in - rts.delete(use: delete) - } - ///Add custom routes here + rts.delete(":x", use: delete) + //Additional routs added here } - func index(req: Request) async throws -> [mdlWorkOrder] { - try await mdlWorkOrder.ObjQuery(req: req).all() + func index(req: Request) async throws -> Response { + guard let obj = try? await mdlWorkOrder.ObjQuery(req: req).all() else { + throw Abort(.badGateway, reason: "Something went wrong with this api.") + } + guard let response = try? await obj.encodeResponse(status: .ok, for: req) else{ + throw Abort(.noContent, reason: "Something went wrong decoding data. Array could not be shown.") + } + return response } - func read(req:Request) async throws -> mdlWorkOrder{ - return try await mdlWorkOrder.ObjQuery(req: req).first()! + func read(req:Request) async throws -> Response{ + guard let obj = try? await mdlWorkOrder.ObjQuery(req: req).first() else{ + throw Abort(.notFound, reason: "WorkOrder was not found.") + } + guard let response = try? await obj.encodeResponse(status: .ok, for: req) else { + throw Abort(.noContent, reason: "Something went wrong decoding object.") + } + return response } - func create(req: Request) async throws -> mdlWorkOrder { - let obj = try req.content.decode(mdlWorkOrder.self) + func create(req: Request) async throws -> Response { + + guard let obj = try? req.content.decode(mdlWorkOrder.self) else { + throw Abort(.notAcceptable, reason: "WorkOrder json not formatted correctly or is missing parents.") + } + var exists = false if(obj.id != nil){ obj._$idExists = true + exists = true + } + guard let _ = try? await obj.save(on: req.db) else{ + throw Abort(.conflict, reason: "Something conflicts in the database. WorkOrder cannot be saved.") } - try await obj.save(on: req.db) - return obj + + guard let response = try? await obj.encodeResponse(status: exists ? .accepted : .created, for: req) else{ + throw Abort(.noContent, reason: "Something went wrong decoding data. WorkOrder has been saved.") + } + return response } - func delete(req: Request) async throws -> HTTPStatus { - guard let obj = try await mdlWorkOrder.find(req.parameters.get("id"), on: req.db) else { - throw Abort(.notFound) + + func delete(req: Request) async throws -> Response { + guard let obj = try? await mdlWorkOrder.ObjQuery(req: req).first() else { + throw Abort(.notFound, reason: "WorkOrder's ID was not supplied. WorkOrder will not be deleted") } - try await obj.delete(on: req.db) - return .noContent + guard let _ = try? await obj.delete(on: req.db) else{ + throw Abort(.conflict, reason: "There was a conflict deleting WorkOrder. Make sure it has not children.") + } + guard let response = try? await obj.encodeResponse(status: .ok, for: req) else{ + throw Abort(.noContent, reason: "Something went wrong decoding data. WorkOrder has been deleted.") + } + return response } } + diff --git a/Sources/App/Controllers/WorkOrderControllers/cntrlWorkOrderFlow.swift b/Sources/App/Controllers/WorkOrderControllers/cntrlWorkOrderFlow.swift index 760c884..f916e50 100644 --- a/Sources/App/Controllers/WorkOrderControllers/cntrlWorkOrderFlow.swift +++ b/Sources/App/Controllers/WorkOrderControllers/cntrlWorkOrderFlow.swift @@ -2,46 +2,77 @@ // cntrlWorkOrderFlow.swift // // -// Created by Michiel Carman on 4/1/24. +// Created by Michiel Carman on 4/5/2024. // import Fluent import Vapor +import Crypto -struct cntrlWorkorderFlow: RouteCollection { + + +struct cntrlWorkOrderFlow: RouteCollection { func boot(routes: RoutesBuilder) throws { let rts = routes.grouped("workorderflows") rts.get(use: index) rts.get(":x", use: read) rts.post(use: create) - rts.group(":x") { rt in - rts.delete(use: delete) - } - ///Add custom routes here + rts.delete(":x", use: delete) + //Additional routs added here } - func index(req: Request) async throws -> [mdlWorkOrderFlow] { - try await mdlWorkOrderFlow.ObjQuery(req: req).all() + func index(req: Request) async throws -> Response { + guard let obj = try? await mdlWorkOrderFlow.ObjQuery(req: req).all() else { + throw Abort(.badGateway, reason: "Something went wrong with this api.") + } + guard let response = try? await obj.encodeResponse(status: .ok, for: req) else{ + throw Abort(.noContent, reason: "Something went wrong decoding data. Array could not be shown.") + } + return response } - func read(req:Request) async throws -> mdlWorkOrderFlow{ - return try await mdlWorkOrderFlow.ObjQuery(req: req).first()! + func read(req:Request) async throws -> Response{ + guard let obj = try? await mdlWorkOrderFlow.ObjQuery(req: req).first() else{ + throw Abort(.notFound, reason: "WorkOrderFlow was not found.") + } + guard let response = try? await obj.encodeResponse(status: .ok, for: req) else { + throw Abort(.noContent, reason: "Something went wrong decoding object.") + } + return response } - func create(req: Request) async throws -> mdlWorkOrderFlow { - let obj = try req.content.decode(mdlWorkOrderFlow.self) + func create(req: Request) async throws -> Response { + + guard let obj = try? req.content.decode(mdlWorkOrderFlow.self) else { + throw Abort(.notAcceptable, reason: "WorkOrderFlow json not formatted correctly or is missing parents.") + } + var exists = false if(obj.id != nil){ obj._$idExists = true + exists = true + } + guard let _ = try? await obj.save(on: req.db) else{ + throw Abort(.conflict, reason: "Something conflicts in the database. WorkOrderFlow cannot be saved.") } - try await obj.save(on: req.db) - return obj + + guard let response = try? await obj.encodeResponse(status: exists ? .accepted : .created, for: req) else{ + throw Abort(.noContent, reason: "Something went wrong decoding data. WorkOrderFlow has been saved.") + } + return response } - func delete(req: Request) async throws -> HTTPStatus { - guard let obj = try await mdlWorkOrderFlow.find(req.parameters.get("id"), on: req.db) else { - throw Abort(.notFound) + + func delete(req: Request) async throws -> Response { + guard let obj = try? await mdlWorkOrderFlow.ObjQuery(req: req).first() else { + throw Abort(.notFound, reason: "WorkOrderFlow's ID was not supplied. WorkOrderFlow will not be deleted") } - try await obj.delete(on: req.db) - return .noContent + guard let _ = try? await obj.delete(on: req.db) else{ + throw Abort(.conflict, reason: "There was a conflict deleting WorkOrderFlow. Make sure it has not children.") + } + guard let response = try? await obj.encodeResponse(status: .ok, for: req) else{ + throw Abort(.noContent, reason: "Something went wrong decoding data. WorkOrderFlow has been deleted.") + } + return response } } + diff --git a/Sources/App/Controllers/WorkOrderControllers/cntrlWorkOrderSegment.swift b/Sources/App/Controllers/WorkOrderControllers/cntrlWorkOrderSegment.swift index d50a229..50e02b7 100644 --- a/Sources/App/Controllers/WorkOrderControllers/cntrlWorkOrderSegment.swift +++ b/Sources/App/Controllers/WorkOrderControllers/cntrlWorkOrderSegment.swift @@ -2,11 +2,14 @@ // cntrlWorkOrderSegment.swift // // -// Created by Michiel Carman on 4/1/24. +// Created by Michiel Carman on 4/5/2024. // import Fluent import Vapor +import Crypto + + struct cntrlWorkOrderSegment: RouteCollection { func boot(routes: RoutesBuilder) throws { @@ -14,34 +17,62 @@ struct cntrlWorkOrderSegment: RouteCollection { rts.get(use: index) rts.get(":x", use: read) rts.post(use: create) - rts.group(":x") { rt in - rts.delete(use: delete) - } - ///Add custom routes here + rts.delete(":x", use: delete) + //Additional routs added here } - func index(req: Request) async throws -> [mdlWorkOrderSegment] { - try await mdlWorkOrderSegment.ObjQuery(req: req).all() + func index(req: Request) async throws -> Response { + guard let obj = try? await mdlWorkOrderSegment.ObjQuery(req: req).all() else { + throw Abort(.badGateway, reason: "Something went wrong with this api.") + } + guard let response = try? await obj.encodeResponse(status: .ok, for: req) else{ + throw Abort(.noContent, reason: "Something went wrong decoding data. Array could not be shown.") + } + return response } - func read(req:Request) async throws -> mdlWorkOrderSegment{ - return try await mdlWorkOrderSegment.ObjQuery(req: req).first()! + func read(req:Request) async throws -> Response{ + guard let obj = try? await mdlWorkOrderSegment.ObjQuery(req: req).first() else{ + throw Abort(.notFound, reason: "WorkOrderSegment was not found.") + } + guard let response = try? await obj.encodeResponse(status: .ok, for: req) else { + throw Abort(.noContent, reason: "Something went wrong decoding object.") + } + return response } - func create(req: Request) async throws -> mdlWorkOrderSegment { - let obj = try req.content.decode(mdlWorkOrderSegment.self) + func create(req: Request) async throws -> Response { + + guard let obj = try? req.content.decode(mdlWorkOrderSegment.self) else { + throw Abort(.notAcceptable, reason: "WorkOrderSegment json not formatted correctly or is missing parents.") + } + var exists = false if(obj.id != nil){ obj._$idExists = true + exists = true + } + guard let _ = try? await obj.save(on: req.db) else{ + throw Abort(.conflict, reason: "Something conflicts in the database. WorkOrderSegment cannot be saved.") } - try await obj.save(on: req.db) - return obj + + guard let response = try? await obj.encodeResponse(status: exists ? .accepted : .created, for: req) else{ + throw Abort(.noContent, reason: "Something went wrong decoding data. WorkOrderSegment has been saved.") + } + return response } - func delete(req: Request) async throws -> HTTPStatus { - guard let obj = try await mdlWorkOrderSegment.find(req.parameters.get("id"), on: req.db) else { - throw Abort(.notFound) + + func delete(req: Request) async throws -> Response { + guard let obj = try? await mdlWorkOrderSegment.ObjQuery(req: req).first() else { + throw Abort(.notFound, reason: "WorkOrderSegment's ID was not supplied. WorkOrderSegment will not be deleted") } - try await obj.delete(on: req.db) - return .noContent + guard let _ = try? await obj.delete(on: req.db) else{ + throw Abort(.conflict, reason: "There was a conflict deleting WorkOrderSegment. Make sure it has not children.") + } + guard let response = try? await obj.encodeResponse(status: .ok, for: req) else{ + throw Abort(.noContent, reason: "Something went wrong decoding data. WorkOrderSegment has been deleted.") + } + return response } } + diff --git a/Sources/App/Models/AuthTables/mdlRole.swift b/Sources/App/Models/AuthTables/mdlRole.swift index 7a3da1f..68e8a6b 100644 --- a/Sources/App/Models/AuthTables/mdlRole.swift +++ b/Sources/App/Models/AuthTables/mdlRole.swift @@ -92,12 +92,11 @@ extension mdlRole{ .with(\.$users) if let x = req.parameters.get("x"){ - let dataQuery = req.parameters.get("x") - let decryptedString = try CustomCrypto.DecryptString(input: dataQuery!) - let x = decryptedString.components(separatedBy: ":") + let decryptedString = try CustomCrypto.DecryptString(input: x) + let array = decryptedString.components(separatedBy: ":") ///This call is the standard get by id call - if (x.count == 1){ - let id = Int(x[0])! + 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 diff --git a/Sources/App/Models/AuthTables/mdlSegment.swift b/Sources/App/Models/AuthTables/mdlSegment.swift index d46168f..b96e3d1 100644 --- a/Sources/App/Models/AuthTables/mdlSegment.swift +++ b/Sources/App/Models/AuthTables/mdlSegment.swift @@ -83,12 +83,11 @@ extension mdlSegment{ let query = mdlSegment.query(on: req.db) if let x = req.parameters.get("x"){ - let dataQuery = req.parameters.get("x") - let decryptedString = try CustomCrypto.DecryptString(input: dataQuery!) - let x = decryptedString.components(separatedBy: ":") + let decryptedString = try CustomCrypto.DecryptString(input: x) + let array = decryptedString.components(separatedBy: ":") ///This call is the standard get by id call - if (x.count == 1){ - let id = Int(x[0])! + 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 diff --git a/Sources/App/Models/AuthTables/mdlUser.swift b/Sources/App/Models/AuthTables/mdlUser.swift index b3329d3..fee1bc2 100644 --- a/Sources/App/Models/AuthTables/mdlUser.swift +++ b/Sources/App/Models/AuthTables/mdlUser.swift @@ -36,7 +36,7 @@ final class mdlUser: Model, Content { @Timestamp(key: "updateDate", on: .update) var updatedate: Date? @Field(key: "updateUserId") var updateuserid: Int? - init() { } + init() {} init(id: Int? = nil, number: String? = nil, firstname: String? = nil, lastname: String? = nil, username: String? = nil, password: String? = nil, email: String? = nil, isactive: Bool? = nil, createuserid: Int? = nil, updateuserid: Int? = nil, divisionid: Int? = nil, locationid: Int? = nil, gateid: Int? = nil) { self.id = id @@ -48,10 +48,9 @@ final class mdlUser: Model, Content { self.password = password self.email = email self.isactive = isactive - - self.$division.$id.value = divisionid! - self.$location.$id.value = locationid! - self.$gate.$id.value = gateid! + if(divisionid != nil) { self.$division.$id.value = divisionid } + if(locationid != nil) { self.$location.$id.value = locationid } + if(gateid != nil) { self.$gate.$id.value = gateid } self.createuserid = createuserid self.updateuserid = updateuserid @@ -94,24 +93,26 @@ extension mdlUser{ .with(\.$workorders) if let x = req.parameters.get("x"){ - let dataQuery = req.parameters.get("x") - let decryptedString = try CustomCrypto.DecryptString(input: dataQuery!) - let x = decryptedString.components(separatedBy: ":") + let decryptedString = try CustomCrypto.DecryptString(input: x) + let array = decryptedString.components(separatedBy: ":") ///This call is the standard get by id call - if (x.count == 1){ - let id = Int(x[0])! + + let p = req.url.path.components(separatedBy: "/")[4] + 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] + /// + else if(array.count == 2 && p.uppercased() == "LOGIN"){ + let username = array[0] + let password = array[1] return query.filter(\.$username == username).filter(\.$password == password) } else{ - throw Abort(.badRequest) + throw Abort(.badRequest, reason: "Parameter missmatch.") } } diff --git a/Sources/App/Models/BaseTables/ChildTables/mdlResponseType.swift b/Sources/App/Models/BaseTables/ChildTables/mdlResponseType.swift index e70be73..4659d32 100644 --- a/Sources/App/Models/BaseTables/ChildTables/mdlResponseType.swift +++ b/Sources/App/Models/BaseTables/ChildTables/mdlResponseType.swift @@ -29,13 +29,13 @@ final class mdlResponseType: Model, Content { init() { } - init(id: Int? = nil, name: String? = nil, color: String? = nil, isactive: Bool? = nil, repairtypeid: Int? = nil, createuserid: Int? = nil, updateuserid: Int? = nil) { + init(id: Int? = nil, name: String? = nil, color: String? = nil, isactive: Bool? = nil, repairtypeid: Int, createuserid: Int? = nil, updateuserid: Int? = nil) { self.id = id self.name = name self.color = color self.isactive = isactive - self.$repairtype.$id.value = repairtypeid! + self.$repairtype.$id.value = repairtypeid self.createuserid = createuserid self.updateuserid = updateuserid @@ -84,12 +84,11 @@ extension mdlResponseType{ .with(\.$responses) if let x = req.parameters.get("x"){ - let dataQuery = req.parameters.get("x") - let decryptedString = try CustomCrypto.DecryptString(input: dataQuery!) - let x = decryptedString.components(separatedBy: ":") + let decryptedString = try CustomCrypto.DecryptString(input: x) + let array = decryptedString.components(separatedBy: ":") ///This call is the standard get by id call - if (x.count == 1){ - let id = Int(x[0])! + 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 diff --git a/Sources/App/Models/BaseTables/ChildTables/mdlUnit.swift b/Sources/App/Models/BaseTables/ChildTables/mdlUnit.swift index 6b2e4fc..8ce1d48 100644 --- a/Sources/App/Models/BaseTables/ChildTables/mdlUnit.swift +++ b/Sources/App/Models/BaseTables/ChildTables/mdlUnit.swift @@ -28,13 +28,13 @@ final class mdlUnit: Model, Content { init() { } - init(id: Int? = nil, name: String? = nil, description: String? = nil, isactive: Bool? = nil, datatypeid: Int? = nil, createuserid: Int? = nil, updateuserid: Int? = nil) { + init(id: Int? = nil, name: String? = nil, description: String? = nil, isactive: Bool? = nil, datatypeid: Int, createuserid: Int? = nil, updateuserid: Int? = nil) { self.id = id self.name = name self.description = description self.isactive = isactive - self.$datatype.$id.value = datatypeid! + self.$datatype.$id.value = datatypeid self.createuserid = createuserid self.updateuserid = updateuserid @@ -67,12 +67,11 @@ extension mdlUnit{ .with(\.$datatype) if let x = req.parameters.get("x"){ - let dataQuery = req.parameters.get("x") - let decryptedString = try CustomCrypto.DecryptString(input: dataQuery!) - let x = decryptedString.components(separatedBy: ":") + let decryptedString = try CustomCrypto.DecryptString(input: x) + let array = decryptedString.components(separatedBy: ":") ///This call is the standard get by id call - if (x.count == 1){ - let id = Int(x[0])! + 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 diff --git a/Sources/App/Models/BaseTables/mdlCategory.swift b/Sources/App/Models/BaseTables/mdlCategory.swift index 99a4c69..50d7121 100644 --- a/Sources/App/Models/BaseTables/mdlCategory.swift +++ b/Sources/App/Models/BaseTables/mdlCategory.swift @@ -65,12 +65,11 @@ extension mdlCategory{ .with(\.$templatecategories) if let x = req.parameters.get("x"){ - let dataQuery = req.parameters.get("x") - let decryptedString = try CustomCrypto.DecryptString(input: dataQuery!) - let x = decryptedString.components(separatedBy: ":") + let decryptedString = try CustomCrypto.DecryptString(input: x) + let array = decryptedString.components(separatedBy: ":") ///This call is the standard get by id call - if (x.count == 1){ - let id = Int(x[0])! + 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 diff --git a/Sources/App/Models/BaseTables/mdlComparisonType.swift b/Sources/App/Models/BaseTables/mdlComparisonType.swift index 71d25f2..6a5d6fa 100644 --- a/Sources/App/Models/BaseTables/mdlComparisonType.swift +++ b/Sources/App/Models/BaseTables/mdlComparisonType.swift @@ -80,12 +80,11 @@ extension mdlComparisonType{ let query = mdlComparisonType.query(on: req.db) if let x = req.parameters.get("x"){ - let dataQuery = req.parameters.get("x") - let decryptedString = try CustomCrypto.DecryptString(input: dataQuery!) - let x = decryptedString.components(separatedBy: ":") + let decryptedString = try CustomCrypto.DecryptString(input: x) + let array = decryptedString.components(separatedBy: ":") ///This call is the standard get by id call - if (x.count == 1){ - let id = Int(x[0])! + 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 diff --git a/Sources/App/Models/BaseTables/mdlContactType.swift b/Sources/App/Models/BaseTables/mdlContactType.swift index 6821cf3..c42eaf1 100644 --- a/Sources/App/Models/BaseTables/mdlContactType.swift +++ b/Sources/App/Models/BaseTables/mdlContactType.swift @@ -80,12 +80,11 @@ extension mdlContactType{ let query = mdlContactType.query(on: req.db) if let x = req.parameters.get("x"){ - let dataQuery = req.parameters.get("x") - let decryptedString = try CustomCrypto.DecryptString(input: dataQuery!) - let x = decryptedString.components(separatedBy: ":") + let decryptedString = try CustomCrypto.DecryptString(input: x) + let array = decryptedString.components(separatedBy: ":") ///This call is the standard get by id call - if (x.count == 1){ - let id = Int(x[0])! + 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 diff --git a/Sources/App/Models/BaseTables/mdlDataType.swift b/Sources/App/Models/BaseTables/mdlDataType.swift index 654819f..0e53973 100644 --- a/Sources/App/Models/BaseTables/mdlDataType.swift +++ b/Sources/App/Models/BaseTables/mdlDataType.swift @@ -82,12 +82,11 @@ extension mdlDataType{ .with(\.$units) if let x = req.parameters.get("x"){ - let dataQuery = req.parameters.get("x") - let decryptedString = try CustomCrypto.DecryptString(input: dataQuery!) - let x = decryptedString.components(separatedBy: ":") + let decryptedString = try CustomCrypto.DecryptString(input: x) + let array = decryptedString.components(separatedBy: ":") ///This call is the standard get by id call - if (x.count == 1){ - let id = Int(x[0])! + 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 diff --git a/Sources/App/Models/BaseTables/mdlDivision.swift b/Sources/App/Models/BaseTables/mdlDivision.swift index 5d04402..1cf659d 100644 --- a/Sources/App/Models/BaseTables/mdlDivision.swift +++ b/Sources/App/Models/BaseTables/mdlDivision.swift @@ -66,12 +66,11 @@ extension mdlDivision{ .with(\.$users) if let x = req.parameters.get("x"){ - let dataQuery = req.parameters.get("x") - let decryptedString = try CustomCrypto.DecryptString(input: dataQuery!) - let x = decryptedString.components(separatedBy: ":") + let decryptedString = try CustomCrypto.DecryptString(input: x) + let array = decryptedString.components(separatedBy: ":") ///This call is the standard get by id call - if (x.count == 1){ - let id = Int(x[0])! + 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 diff --git a/Sources/App/Models/BaseTables/mdlEquipmentType.swift b/Sources/App/Models/BaseTables/mdlEquipmentType.swift index b6f4c4a..d39a1ee 100644 --- a/Sources/App/Models/BaseTables/mdlEquipmentType.swift +++ b/Sources/App/Models/BaseTables/mdlEquipmentType.swift @@ -76,12 +76,11 @@ extension mdlEquipmentType{ let query = mdlEquipmentType.query(on: req.db) if let x = req.parameters.get("x"){ - let dataQuery = req.parameters.get("x") - let decryptedString = try CustomCrypto.DecryptString(input: dataQuery!) - let x = decryptedString.components(separatedBy: ":") + let decryptedString = try CustomCrypto.DecryptString(input: x) + let array = decryptedString.components(separatedBy: ":") ///This call is the standard get by id call - if (x.count == 1){ - let id = Int(x[0])! + 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 diff --git a/Sources/App/Models/BaseTables/mdlFluid.swift b/Sources/App/Models/BaseTables/mdlFluid.swift index 26f626f..ebaf02a 100644 --- a/Sources/App/Models/BaseTables/mdlFluid.swift +++ b/Sources/App/Models/BaseTables/mdlFluid.swift @@ -65,12 +65,11 @@ extension mdlFluid{ let query = mdlFluid.query(on: req.db) if let x = req.parameters.get("x"){ - let dataQuery = req.parameters.get("x") - let decryptedString = try CustomCrypto.DecryptString(input: dataQuery!) - let x = decryptedString.components(separatedBy: ":") + let decryptedString = try CustomCrypto.DecryptString(input: x) + let array = decryptedString.components(separatedBy: ":") ///This call is the standard get by id call - if (x.count == 1){ - let id = Int(x[0])! + 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 diff --git a/Sources/App/Models/BaseTables/mdlFuel.swift b/Sources/App/Models/BaseTables/mdlFuel.swift index 744fcd3..c140f94 100644 --- a/Sources/App/Models/BaseTables/mdlFuel.swift +++ b/Sources/App/Models/BaseTables/mdlFuel.swift @@ -68,12 +68,11 @@ extension mdlFuel{ .with(\.$workorders) if let x = req.parameters.get("x"){ - let dataQuery = req.parameters.get("x") - let decryptedString = try CustomCrypto.DecryptString(input: dataQuery!) - let x = decryptedString.components(separatedBy: ":") + let decryptedString = try CustomCrypto.DecryptString(input: x) + let array = decryptedString.components(separatedBy: ":") ///This call is the standard get by id call - if (x.count == 1){ - let id = Int(x[0])! + 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 diff --git a/Sources/App/Models/BaseTables/mdlGate.swift b/Sources/App/Models/BaseTables/mdlGate.swift index ce5982b..6bd7a38 100644 --- a/Sources/App/Models/BaseTables/mdlGate.swift +++ b/Sources/App/Models/BaseTables/mdlGate.swift @@ -65,12 +65,11 @@ .with(\.$users) if let x = req.parameters.get("x"){ - let dataQuery = req.parameters.get("x") - let decryptedString = try CustomCrypto.DecryptString(input: dataQuery!) - let x = decryptedString.components(separatedBy: ":") + let decryptedString = try CustomCrypto.DecryptString(input: x) + let array = decryptedString.components(separatedBy: ":") ///This call is the standard get by id call - if (x.count == 1){ - let id = Int(x[0])! + 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 diff --git a/Sources/App/Models/BaseTables/mdlIncompleteReason.swift b/Sources/App/Models/BaseTables/mdlIncompleteReason.swift index ffbc412..6a83320 100644 --- a/Sources/App/Models/BaseTables/mdlIncompleteReason.swift +++ b/Sources/App/Models/BaseTables/mdlIncompleteReason.swift @@ -65,12 +65,11 @@ extension mdlIncompleteReason{ .with(\.$workorders) if let x = req.parameters.get("x"){ - let dataQuery = req.parameters.get("x") - let decryptedString = try CustomCrypto.DecryptString(input: dataQuery!) - let x = decryptedString.components(separatedBy: ":") + let decryptedString = try CustomCrypto.DecryptString(input: x) + let array = decryptedString.components(separatedBy: ":") ///This call is the standard get by id call - if (x.count == 1){ - let id = Int(x[0])! + 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 diff --git a/Sources/App/Models/BaseTables/mdlInputType.swift b/Sources/App/Models/BaseTables/mdlInputType.swift index 34b0e9a..46ca491 100644 --- a/Sources/App/Models/BaseTables/mdlInputType.swift +++ b/Sources/App/Models/BaseTables/mdlInputType.swift @@ -81,12 +81,11 @@ extension mdlInputType{ .with(\.$inspectionitems) if let x = req.parameters.get("x"){ - let dataQuery = req.parameters.get("x") - let decryptedString = try CustomCrypto.DecryptString(input: dataQuery!) - let x = decryptedString.components(separatedBy: ":") + let decryptedString = try CustomCrypto.DecryptString(input: x) + let array = decryptedString.components(separatedBy: ":") ///This call is the standard get by id call - if (x.count == 1){ - let id = Int(x[0])! + 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 diff --git a/Sources/App/Models/BaseTables/mdlJobSite.swift b/Sources/App/Models/BaseTables/mdlJobSite.swift index 263380a..645ca50 100644 --- a/Sources/App/Models/BaseTables/mdlJobSite.swift +++ b/Sources/App/Models/BaseTables/mdlJobSite.swift @@ -79,12 +79,11 @@ extension mdlJobSite{ let query = mdlJobSite.query(on: req.db) if let x = req.parameters.get("x"){ - let dataQuery = req.parameters.get("x") - let decryptedString = try CustomCrypto.DecryptString(input: dataQuery!) - let x = decryptedString.components(separatedBy: ":") + let decryptedString = try CustomCrypto.DecryptString(input: x) + let array = decryptedString.components(separatedBy: ":") ///This call is the standard get by id call - if (x.count == 1){ - let id = Int(x[0])! + 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 diff --git a/Sources/App/Models/BaseTables/mdlLevel.swift b/Sources/App/Models/BaseTables/mdlLevel.swift index 6bad3d6..4399325 100644 --- a/Sources/App/Models/BaseTables/mdlLevel.swift +++ b/Sources/App/Models/BaseTables/mdlLevel.swift @@ -65,12 +65,11 @@ extension mdlLevel{ let query = mdlLevel.query(on: req.db) if let x = req.parameters.get("x"){ - let dataQuery = req.parameters.get("x") - let decryptedString = try CustomCrypto.DecryptString(input: dataQuery!) - let x = decryptedString.components(separatedBy: ":") + let decryptedString = try CustomCrypto.DecryptString(input: x) + let array = decryptedString.components(separatedBy: ":") ///This call is the standard get by id call - if (x.count == 1){ - let id = Int(x[0])! + 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 diff --git a/Sources/App/Models/BaseTables/mdlLocation.swift b/Sources/App/Models/BaseTables/mdlLocation.swift index d73ba8d..c07079b 100644 --- a/Sources/App/Models/BaseTables/mdlLocation.swift +++ b/Sources/App/Models/BaseTables/mdlLocation.swift @@ -50,7 +50,7 @@ final class mdlLocation: Model, Content { isactive: Bool? = nil, createuserid: Int? = nil, updateuserid: Int? = nil, - stateid: Int? = nil, + stateid: Int, unitid: Int? = nil ) { self.id = id @@ -67,8 +67,8 @@ final class mdlLocation: Model, Content { self.createuserid = createuserid self.updateuserid = updateuserid - self.$state.$id.value = stateid! - self.$unit.$id.value = unitid! + self.$state.$id.value = stateid + if(unitid != nil) {self.$unit.$id.value = unitid} } } @@ -121,12 +121,11 @@ extension mdlLocation{ .with(\.$labsetting) if let x = req.parameters.get("x"){ - let dataQuery = req.parameters.get("x") - let decryptedString = try CustomCrypto.DecryptString(input: dataQuery!) - let x = decryptedString.components(separatedBy: ":") + let decryptedString = try CustomCrypto.DecryptString(input: x) + let array = decryptedString.components(separatedBy: ":") ///This call is the standard get by id call - if (x.count == 1){ - let id = Int(x[0])! + 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 diff --git a/Sources/App/Models/BaseTables/mdlNoteType.swift b/Sources/App/Models/BaseTables/mdlNoteType.swift index 5eec74d..9b22ef6 100644 --- a/Sources/App/Models/BaseTables/mdlNoteType.swift +++ b/Sources/App/Models/BaseTables/mdlNoteType.swift @@ -65,12 +65,11 @@ extension mdlNoteType{ .with(\.$notes) if let x = req.parameters.get("x"){ - let dataQuery = req.parameters.get("x") - let decryptedString = try CustomCrypto.DecryptString(input: dataQuery!) - let x = decryptedString.components(separatedBy: ":") + let decryptedString = try CustomCrypto.DecryptString(input: x) + let array = decryptedString.components(separatedBy: ":") ///This call is the standard get by id call - if (x.count == 1){ - let id = Int(x[0])! + 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 diff --git a/Sources/App/Models/BaseTables/mdlPart.swift b/Sources/App/Models/BaseTables/mdlPart.swift index b334ffc..17ae264 100644 --- a/Sources/App/Models/BaseTables/mdlPart.swift +++ b/Sources/App/Models/BaseTables/mdlPart.swift @@ -63,12 +63,11 @@ extension mdlPart{ // .with(\.$<#parent/children#>) if let x = req.parameters.get("x"){ - let dataQuery = req.parameters.get("x") - let decryptedString = try CustomCrypto.DecryptString(input: dataQuery!) - let x = decryptedString.components(separatedBy: ":") + let decryptedString = try CustomCrypto.DecryptString(input: x) + let array = decryptedString.components(separatedBy: ":") ///This call is the standard get by id call - if (x.count == 1){ - let id = Int(x[0])! + 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 diff --git a/Sources/App/Models/BaseTables/mdlPriority.swift b/Sources/App/Models/BaseTables/mdlPriority.swift index 1e6b0a8..52d43d1 100644 --- a/Sources/App/Models/BaseTables/mdlPriority.swift +++ b/Sources/App/Models/BaseTables/mdlPriority.swift @@ -82,12 +82,11 @@ extension mdlPriority{ .with(\.$workorders) if let x = req.parameters.get("x"){ - let dataQuery = req.parameters.get("x") - let decryptedString = try CustomCrypto.DecryptString(input: dataQuery!) - let x = decryptedString.components(separatedBy: ":") + let decryptedString = try CustomCrypto.DecryptString(input: x) + let array = decryptedString.components(separatedBy: ":") ///This call is the standard get by id call - if (x.count == 1){ - let id = Int(x[0])! + 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 diff --git a/Sources/App/Models/BaseTables/mdlReason.swift b/Sources/App/Models/BaseTables/mdlReason.swift index 102d9bd..4373fa7 100644 --- a/Sources/App/Models/BaseTables/mdlReason.swift +++ b/Sources/App/Models/BaseTables/mdlReason.swift @@ -65,12 +65,11 @@ extension mdlReason{ .with(\.$workorders) if let x = req.parameters.get("x"){ - let dataQuery = req.parameters.get("x") - let decryptedString = try CustomCrypto.DecryptString(input: dataQuery!) - let x = decryptedString.components(separatedBy: ":") + let decryptedString = try CustomCrypto.DecryptString(input: x) + let array = decryptedString.components(separatedBy: ":") ///This call is the standard get by id call - if (x.count == 1){ - let id = Int(x[0])! + 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 diff --git a/Sources/App/Models/BaseTables/mdlRepairType.swift b/Sources/App/Models/BaseTables/mdlRepairType.swift index 9d8f5a2..12877ff 100644 --- a/Sources/App/Models/BaseTables/mdlRepairType.swift +++ b/Sources/App/Models/BaseTables/mdlRepairType.swift @@ -85,12 +85,11 @@ extension mdlRepairType{ .with(\.$units) if let x = req.parameters.get("x"){ - let dataQuery = req.parameters.get("x") - let decryptedString = try CustomCrypto.DecryptString(input: dataQuery!) - let x = decryptedString.components(separatedBy: ":") + let decryptedString = try CustomCrypto.DecryptString(input: x) + let array = decryptedString.components(separatedBy: ":") ///This call is the standard get by id call - if (x.count == 1){ - let id = Int(x[0])! + 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 diff --git a/Sources/App/Models/BaseTables/mdlSMSCCode.swift b/Sources/App/Models/BaseTables/mdlSMSCCode.swift index 1a1d132..1e2ab27 100644 --- a/Sources/App/Models/BaseTables/mdlSMSCCode.swift +++ b/Sources/App/Models/BaseTables/mdlSMSCCode.swift @@ -63,12 +63,11 @@ extension mdlSMSCCode{ // .with(\.$<#parent/children#>) if let x = req.parameters.get("x"){ - let dataQuery = req.parameters.get("x") - let decryptedString = try CustomCrypto.DecryptString(input: dataQuery!) - let x = decryptedString.components(separatedBy: ":") + let decryptedString = try CustomCrypto.DecryptString(input: x) + let array = decryptedString.components(separatedBy: ":") ///This call is the standard get by id call - if (x.count == 1){ - let id = Int(x[0])! + 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 diff --git a/Sources/App/Models/BaseTables/mdlSignatureType.swift b/Sources/App/Models/BaseTables/mdlSignatureType.swift index 55132ad..9efd91d 100644 --- a/Sources/App/Models/BaseTables/mdlSignatureType.swift +++ b/Sources/App/Models/BaseTables/mdlSignatureType.swift @@ -78,12 +78,11 @@ extension mdlSignatureType{ // .with(\.$<#parent/children#>) if let x = req.parameters.get("x"){ - let dataQuery = req.parameters.get("x") - let decryptedString = try CustomCrypto.DecryptString(input: dataQuery!) - let x = decryptedString.components(separatedBy: ":") + let decryptedString = try CustomCrypto.DecryptString(input: x) + let array = decryptedString.components(separatedBy: ":") ///This call is the standard get by id call - if (x.count == 1){ - let id = Int(x[0])! + 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 diff --git a/Sources/App/Models/BaseTables/mdlState.swift b/Sources/App/Models/BaseTables/mdlState.swift index bd0f492..64338aa 100644 --- a/Sources/App/Models/BaseTables/mdlState.swift +++ b/Sources/App/Models/BaseTables/mdlState.swift @@ -130,12 +130,11 @@ extension mdlState{ .with(\.$locations) if let x = req.parameters.get("x"){ - let dataQuery = req.parameters.get("x") - let decryptedString = try CustomCrypto.DecryptString(input: dataQuery!) - let x = decryptedString.components(separatedBy: ":") + let decryptedString = try CustomCrypto.DecryptString(input: x) + let array = decryptedString.components(separatedBy: ":") ///This call is the standard get by id call - if (x.count == 1){ - let id = Int(x[0])! + 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 diff --git a/Sources/App/Models/BaseTables/mdlStatus.swift b/Sources/App/Models/BaseTables/mdlStatus.swift index e5c00df..e052fc8 100644 --- a/Sources/App/Models/BaseTables/mdlStatus.swift +++ b/Sources/App/Models/BaseTables/mdlStatus.swift @@ -86,12 +86,11 @@ extension mdlStatus{ .with(\.$workorders) if let x = req.parameters.get("x"){ - let dataQuery = req.parameters.get("x") - let decryptedString = try CustomCrypto.DecryptString(input: dataQuery!) - let x = decryptedString.components(separatedBy: ":") + let decryptedString = try CustomCrypto.DecryptString(input: x) + let array = decryptedString.components(separatedBy: ":") ///This call is the standard get by id call - if (x.count == 1){ - let id = Int(x[0])! + 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 diff --git a/Sources/App/Models/BaseTables/mdlValueType.swift b/Sources/App/Models/BaseTables/mdlValueType.swift index 5e49006..b733a03 100644 --- a/Sources/App/Models/BaseTables/mdlValueType.swift +++ b/Sources/App/Models/BaseTables/mdlValueType.swift @@ -80,12 +80,11 @@ extension mdlValueType{ // .with(\.$<#parent/children#>) if let x = req.parameters.get("x"){ - let dataQuery = req.parameters.get("x") - let decryptedString = try CustomCrypto.DecryptString(input: dataQuery!) - let x = decryptedString.components(separatedBy: ":") + let decryptedString = try CustomCrypto.DecryptString(input: x) + let array = decryptedString.components(separatedBy: ":") ///This call is the standard get by id call - if (x.count == 1){ - let id = Int(x[0])! + 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 diff --git a/Sources/App/Models/BaseTables/mdlViscosity.swift b/Sources/App/Models/BaseTables/mdlViscosity.swift index 068e105..3c29fb4 100644 --- a/Sources/App/Models/BaseTables/mdlViscosity.swift +++ b/Sources/App/Models/BaseTables/mdlViscosity.swift @@ -63,12 +63,11 @@ extension mdlViscosity{ // .with(\.$<#parent/children#>) if let x = req.parameters.get("x"){ - let dataQuery = req.parameters.get("x") - let decryptedString = try CustomCrypto.DecryptString(input: dataQuery!) - let x = decryptedString.components(separatedBy: ":") + let decryptedString = try CustomCrypto.DecryptString(input: x) + let array = decryptedString.components(separatedBy: ":") ///This call is the standard get by id call - if (x.count == 1){ - let id = Int(x[0])! + 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 diff --git a/Sources/App/Models/CompartmentModule/mdlCompartment.swift b/Sources/App/Models/CompartmentModule/mdlCompartment.swift index 81e5737..7197919 100644 --- a/Sources/App/Models/CompartmentModule/mdlCompartment.swift +++ b/Sources/App/Models/CompartmentModule/mdlCompartment.swift @@ -68,12 +68,11 @@ extension mdlCompartment{ // .with(\.$<#parent/children#>) if let x = req.parameters.get("x"){ - let dataQuery = req.parameters.get("x") - let decryptedString = try CustomCrypto.DecryptString(input: dataQuery!) - let x = decryptedString.components(separatedBy: ":") + let decryptedString = try CustomCrypto.DecryptString(input: x) + let array = decryptedString.components(separatedBy: ":") ///This call is the standard get by id call - if (x.count == 1){ - let id = Int(x[0])! + 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 diff --git a/Sources/App/Models/CompartmentModule/mdlCompartmentLocation.swift b/Sources/App/Models/CompartmentModule/mdlCompartmentLocation.swift index 8ea2c1b..2e77a77 100644 --- a/Sources/App/Models/CompartmentModule/mdlCompartmentLocation.swift +++ b/Sources/App/Models/CompartmentModule/mdlCompartmentLocation.swift @@ -67,12 +67,11 @@ extension mdlCompartmentLocation{ // .with(\.$<#parent/children#>) if let x = req.parameters.get("x"){ - let dataQuery = req.parameters.get("x") - let decryptedString = try CustomCrypto.DecryptString(input: dataQuery!) - let x = decryptedString.components(separatedBy: ":") + let decryptedString = try CustomCrypto.DecryptString(input: x) + let array = decryptedString.components(separatedBy: ":") ///This call is the standard get by id call - if (x.count == 1){ - let id = Int(x[0])! + 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 diff --git a/Sources/App/Models/CustomerTables/mdlContact.swift b/Sources/App/Models/CustomerTables/mdlContact.swift index 1007f86..4b20117 100644 --- a/Sources/App/Models/CustomerTables/mdlContact.swift +++ b/Sources/App/Models/CustomerTables/mdlContact.swift @@ -29,13 +29,13 @@ final class mdlContact: Model, Content { init() { } - init(id: Int? = nil, firstname: String? = nil, lastname: String? = nil, isactive: Bool? = nil, createuserid: Int? = nil, updateuserid: Int? = nil, customerid: Int? = nil) { + init(id: Int? = nil, firstname: String? = nil, lastname: String? = nil, isactive: Bool? = nil, createuserid: Int? = nil, updateuserid: Int? = nil, customerid: Int) { self.id = id self.firstname = firstname self.lastname = lastname self.isactive = isactive - self.$customer.$id.value = customerid! + self.$customer.$id.value = customerid self.createuserid = createuserid self.updateuserid = updateuserid @@ -93,12 +93,11 @@ extension mdlContact{ .with(\.$customer) if let x = req.parameters.get("x"){ - let dataQuery = req.parameters.get("x") - let decryptedString = try CustomCrypto.DecryptString(input: dataQuery!) - let x = decryptedString.components(separatedBy: ":") + let decryptedString = try CustomCrypto.DecryptString(input: x) + let array = decryptedString.components(separatedBy: ":") ///This call is the standard get by id call - if (x.count == 1){ - let id = Int(x[0])! + 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 diff --git a/Sources/App/Models/CustomerTables/mdlContactInfo.swift b/Sources/App/Models/CustomerTables/mdlContactInfo.swift index 500c3db..a6ff184 100644 --- a/Sources/App/Models/CustomerTables/mdlContactInfo.swift +++ b/Sources/App/Models/CustomerTables/mdlContactInfo.swift @@ -28,13 +28,13 @@ final class mdlContactInfo: Model, Content { init() { } - init(id: Int? = nil, value: String? = nil, isactive: Bool? = nil, createuserid: Int? = nil, updateuserid: Int? = nil, contacttypeid: Int? = nil) { + init(id: Int? = nil, value: String? = nil, isactive: Bool? = nil, createuserid: Int? = nil, updateuserid: Int? = nil, contacttypeid: Int) { self.id = id self.value = value self.isactive = isactive - self.$contacttype.$id.value = contacttypeid! + self.$contacttype.$id.value = contacttypeid self.createuserid = createuserid self.updateuserid = updateuserid @@ -66,12 +66,11 @@ extension mdlContactInfo{ .with(\.$contacttype) if let x = req.parameters.get("x"){ - let dataQuery = req.parameters.get("x") - let decryptedString = try CustomCrypto.DecryptString(input: dataQuery!) - let x = decryptedString.components(separatedBy: ":") + let decryptedString = try CustomCrypto.DecryptString(input: x) + let array = decryptedString.components(separatedBy: ":") ///This call is the standard get by id call - if (x.count == 1){ - let id = Int(x[0])! + 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 diff --git a/Sources/App/Models/CustomerTables/mdlCustomer.swift b/Sources/App/Models/CustomerTables/mdlCustomer.swift index bede82c..7613317 100644 --- a/Sources/App/Models/CustomerTables/mdlCustomer.swift +++ b/Sources/App/Models/CustomerTables/mdlCustomer.swift @@ -68,12 +68,11 @@ extension mdlCustomer{ .with(\.$templates) if let x = req.parameters.get("x"){ - let dataQuery = req.parameters.get("x") - let decryptedString = try CustomCrypto.DecryptString(input: dataQuery!) - let x = decryptedString.components(separatedBy: ":") + let decryptedString = try CustomCrypto.DecryptString(input: x) + let array = decryptedString.components(separatedBy: ":") ///This call is the standard get by id call - if (x.count == 1){ - let id = Int(x[0])! + 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 diff --git a/Sources/App/Models/CustomerTables/mdlEquipment.swift b/Sources/App/Models/CustomerTables/mdlEquipment.swift index 9c49b4b..f6ef2d0 100644 --- a/Sources/App/Models/CustomerTables/mdlEquipment.swift +++ b/Sources/App/Models/CustomerTables/mdlEquipment.swift @@ -34,15 +34,16 @@ final class mdlEquipment: Model, Content { init() { } - init(id: Int? = nil, number: String? = nil, isactive: Bool? = nil, createuserid: Int? = nil, updateuserid: Int? = nil, customerid: Int? = nil, modelid: Int? = nil, equipmenttypeid: Int? = nil, parentid: Int? = nil) { + init(id: Int? = nil, number: String? = nil, isactive: Bool? = nil, createuserid: Int? = nil, updateuserid: Int? = nil, customerid: Int? = nil, modelid: Int, equipmenttypeid: Int, parentid: Int? = nil) { self.id = id self.number = number self.isactive = isactive - self.$customer.$id.value = customerid! - self.$model.$id.value = modelid! - self.$equipmenttype.$id.value = equipmenttypeid! + if(customerid != nil) { self.$customer.$id.value = customerid! } + self.$model.$id.value = modelid + self.$equipmenttype.$id.value = equipmenttypeid self.$parent.$id.value = parentid + if(parentid != nil) { self.$parent.$id.value = parentid! } self.createuserid = createuserid self.updateuserid = updateuserid @@ -95,12 +96,11 @@ extension mdlEquipment{ .with(\.$workorders) if let x = req.parameters.get("x"){ - let dataQuery = req.parameters.get("x") - let decryptedString = try CustomCrypto.DecryptString(input: dataQuery!) - let x = decryptedString.components(separatedBy: ":") + let decryptedString = try CustomCrypto.DecryptString(input: x) + let array = decryptedString.components(separatedBy: ":") ///This call is the standard get by id call - if (x.count == 1){ - let id = Int(x[0])! + 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 diff --git a/Sources/App/Models/CustomerTables/mdlMake.swift b/Sources/App/Models/CustomerTables/mdlMake.swift index b09e22c..ba972a8 100644 --- a/Sources/App/Models/CustomerTables/mdlMake.swift +++ b/Sources/App/Models/CustomerTables/mdlMake.swift @@ -64,12 +64,11 @@ extension mdlMake{ .with(\.$templates) if let x = req.parameters.get("x"){ - let dataQuery = req.parameters.get("x") - let decryptedString = try CustomCrypto.DecryptString(input: dataQuery!) - let x = decryptedString.components(separatedBy: ":") + let decryptedString = try CustomCrypto.DecryptString(input: x) + let array = decryptedString.components(separatedBy: ":") ///This call is the standard get by id call - if (x.count == 1){ - let id = Int(x[0])! + 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 diff --git a/Sources/App/Models/CustomerTables/mdlModel.swift b/Sources/App/Models/CustomerTables/mdlModel.swift index 2ab1895..4676155 100644 --- a/Sources/App/Models/CustomerTables/mdlModel.swift +++ b/Sources/App/Models/CustomerTables/mdlModel.swift @@ -75,12 +75,11 @@ extension mdlModel{ .with(\.$templates) if let x = req.parameters.get("x"){ - let dataQuery = req.parameters.get("x") - let decryptedString = try CustomCrypto.DecryptString(input: dataQuery!) - let x = decryptedString.components(separatedBy: ":") + let decryptedString = try CustomCrypto.DecryptString(input: x) + let array = decryptedString.components(separatedBy: ":") ///This call is the standard get by id call - if (x.count == 1){ - let id = Int(x[0])! + 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 diff --git a/Sources/App/Models/CustomerTables/mdlObjectProperty.swift b/Sources/App/Models/CustomerTables/mdlObjectProperty.swift index 6c57025..35c588e 100644 --- a/Sources/App/Models/CustomerTables/mdlObjectProperty.swift +++ b/Sources/App/Models/CustomerTables/mdlObjectProperty.swift @@ -18,7 +18,7 @@ final class mdlObjProperty: Model, Content { @Field(key: "label") var label: String? @Field(key: "value") var value: String? @Field(key: "isActive") var isactive: Bool? - @Parent(key: "equipmentId") var equipment: mdlEquipment + @OptionalParent(key: "equipmentId") var equipment: mdlEquipment? @Timestamp(key: "createDate", on: .create) var createdate: Date? @@ -33,7 +33,7 @@ final class mdlObjProperty: Model, Content { self.label = label self.value = value self.isactive = isactive - self.$equipment.$id.value = equipmentid + if(equipmentid != nil) { self.$equipment.$id.value = equipmentid } self.createuserid = createuserid self.updateuserid = updateuserid @@ -66,12 +66,11 @@ extension mdlObjProperty{ .with(\.$equipment) if let x = req.parameters.get("x"){ - let dataQuery = req.parameters.get("x") - let decryptedString = try CustomCrypto.DecryptString(input: dataQuery!) - let x = decryptedString.components(separatedBy: ":") + let decryptedString = try CustomCrypto.DecryptString(input: x) + let array = decryptedString.components(separatedBy: ":") ///This call is the standard get by id call - if (x.count == 1){ - let id = Int(x[0])! + 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 diff --git a/Sources/App/Models/CustomerTables/mdlProduct.swift b/Sources/App/Models/CustomerTables/mdlProduct.swift index 47f70c5..7e88741 100644 --- a/Sources/App/Models/CustomerTables/mdlProduct.swift +++ b/Sources/App/Models/CustomerTables/mdlProduct.swift @@ -64,12 +64,11 @@ extension mdlProduct{ .with(\.$templates) if let x = req.parameters.get("x"){ - let dataQuery = req.parameters.get("x") - let decryptedString = try CustomCrypto.DecryptString(input: dataQuery!) - let x = decryptedString.components(separatedBy: ":") + let decryptedString = try CustomCrypto.DecryptString(input: x) + let array = decryptedString.components(separatedBy: ":") ///This call is the standard get by id call - if (x.count == 1){ - let id = Int(x[0])! + 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 diff --git a/Sources/App/Models/ItemTables/mdlItem.swift b/Sources/App/Models/ItemTables/mdlItem.swift index a654443..792468b 100644 --- a/Sources/App/Models/ItemTables/mdlItem.swift +++ b/Sources/App/Models/ItemTables/mdlItem.swift @@ -65,12 +65,11 @@ extension mdlItem{ .with(\.$templateitems) if let x = req.parameters.get("x"){ - let dataQuery = req.parameters.get("x") - let decryptedString = try CustomCrypto.DecryptString(input: dataQuery!) - let x = decryptedString.components(separatedBy: ":") + let decryptedString = try CustomCrypto.DecryptString(input: x) + let array = decryptedString.components(separatedBy: ":") ///This call is the standard get by id call - if (x.count == 1){ - let id = Int(x[0])! + 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 diff --git a/Sources/App/Models/ItemTables/mdlItemResponseType.swift b/Sources/App/Models/ItemTables/mdlItemResponseType.swift index d5b4362..3840f31 100644 --- a/Sources/App/Models/ItemTables/mdlItemResponseType.swift +++ b/Sources/App/Models/ItemTables/mdlItemResponseType.swift @@ -26,11 +26,11 @@ final class mdlItemResponseType: Model, Content { init() { } - init(id: Int? = nil, itemid: Int? = nil, responsetypeid: Int? = nil, isactive: Bool? = true, createuserid: Int? = nil, updateuserid: Int? = nil) { + init(id: Int? = nil, itemid: Int, responsetypeid: Int, isactive: Bool? = true, createuserid: Int? = nil, updateuserid: Int? = nil) { self.id = id - self.$item.$id.value = itemid! - self.$responsetype.$id.value = responsetypeid! + self.$item.$id.value = itemid + self.$responsetype.$id.value = responsetypeid self.isactive = isactive self.createuserid = createuserid @@ -64,12 +64,11 @@ extension mdlItemResponseType{ .with(\.$responsetype) if let x = req.parameters.get("x"){ - let dataQuery = req.parameters.get("x") - let decryptedString = try CustomCrypto.DecryptString(input: dataQuery!) - let x = decryptedString.components(separatedBy: ":") + let decryptedString = try CustomCrypto.DecryptString(input: x) + let array = decryptedString.components(separatedBy: ":") ///This call is the standard get by id call - if (x.count == 1){ - let id = Int(x[0])! + 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 diff --git a/Sources/App/Models/LabTables/mdlLabSetting.swift b/Sources/App/Models/LabTables/mdlLabSetting.swift index 7dbf1b8..ff4248f 100644 --- a/Sources/App/Models/LabTables/mdlLabSetting.swift +++ b/Sources/App/Models/LabTables/mdlLabSetting.swift @@ -33,7 +33,7 @@ final class mdlLabSetting: Model, Content { init() { } - init(id: Int? = nil, name: String? = nil, description: String? = nil, barcodedelimeter: String? = nil, barcodeactive: Bool? = nil, textactive: Bool? = nil, logoactive: Bool? = nil, createuserid: Int? = nil, updateuserid: Int? = nil, cpc: String? = nil) { + init(id: Int? = nil, name: String? = nil, description: String? = nil, barcodedelimeter: String? = nil, barcodeactive: Bool? = nil, textactive: Bool? = nil, logoactive: Bool? = nil, createuserid: Int? = nil, updateuserid: Int? = nil, cpc: String? = nil, locationid: Int) { self.id = id self.name = name @@ -42,7 +42,7 @@ final class mdlLabSetting: Model, Content { self.barcodeactive = barcodeactive self.textactive = textactive self.logoactive = logoactive - + self.$location.$id.value = locationid self.createuserid = createuserid self.updateuserid = updateuserid self.cpcl = cpcl @@ -103,12 +103,11 @@ extension mdlLabSetting{ .with(\.$location) if let x = req.parameters.get("x"){ - let dataQuery = req.parameters.get("x") - let decryptedString = try CustomCrypto.DecryptString(input: dataQuery!) - let x = decryptedString.components(separatedBy: ":") + let decryptedString = try CustomCrypto.DecryptString(input: x) + let array = decryptedString.components(separatedBy: ":") ///This call is the standard get by id call - if (x.count == 1){ - let id = Int(x[0])! + 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 diff --git a/Sources/App/Models/LinkingTables/XFluidViscosity.swift b/Sources/App/Models/LinkingTables/XFluidViscosity.swift index 6b630f5..3f9e3fb 100644 --- a/Sources/App/Models/LinkingTables/XFluidViscosity.swift +++ b/Sources/App/Models/LinkingTables/XFluidViscosity.swift @@ -16,11 +16,7 @@ final class mdlXFluidViscosity: Model, Content { @ID(custom: "id", generatedBy: .database) var id: Int? @Parent(key: "fluidId") var fluid: mdlFluid @Parent(key: "viscosityId") var viscosity: mdlViscosity - @Field(key: "sortOrder") var sortorder: Int? - - - @Timestamp(key: "createDate", on: .create) var createdate: Date? @Field(key: "createUserId") var createuserid: Int? @Timestamp(key: "updateDate", on: .update) var updatedate: Date? @@ -85,12 +81,11 @@ extension mdlXFluidViscosity{ .with(\.$viscosity) if let x = req.parameters.get("x"){ - let dataQuery = req.parameters.get("x") - let decryptedString = try CustomCrypto.DecryptString(input: dataQuery!) - let x = decryptedString.components(separatedBy: ":") + let decryptedString = try CustomCrypto.DecryptString(input: x) + let array = decryptedString.components(separatedBy: ":") ///This call is the standard get by id call - if (x.count == 1){ - let id = Int(x[0])! + 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 diff --git a/Sources/App/Models/LinkingTables/XUserRole.swift b/Sources/App/Models/LinkingTables/XUserRole.swift index 355aa8b..9268e5e 100644 --- a/Sources/App/Models/LinkingTables/XUserRole.swift +++ b/Sources/App/Models/LinkingTables/XUserRole.swift @@ -16,9 +16,6 @@ final class mdlXUserRole: Model, Content { @ID(custom: "id", generatedBy: .database) var id: Int? @Parent(key: "userId") var user: mdlUser @Parent(key: "roleId") var role: mdlRole - - - @Timestamp(key: "createDate", on: .create) var createdate: Date? @Field(key: "createUserId") var createuserid: Int? @Timestamp(key: "updateDate", on: .update) var updatedate: Date? @@ -76,12 +73,11 @@ extension mdlXUserRole{ .with(\.$role) if let x = req.parameters.get("x"){ - let dataQuery = req.parameters.get("x") - let decryptedString = try CustomCrypto.DecryptString(input: dataQuery!) - let x = decryptedString.components(separatedBy: ":") + let decryptedString = try CustomCrypto.DecryptString(input: x) + let array = decryptedString.components(separatedBy: ":") ///This call is the standard get by id call - if (x.count == 1){ - let id = Int(x[0])! + 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 diff --git a/Sources/App/Models/LinkingTables/XWorkOrderSegment.swift b/Sources/App/Models/LinkingTables/XWorkOrderSegment.swift index 205e4c5..9ff680d 100644 --- a/Sources/App/Models/LinkingTables/XWorkOrderSegment.swift +++ b/Sources/App/Models/LinkingTables/XWorkOrderSegment.swift @@ -80,12 +80,11 @@ extension mdlXWorkOrderSegment{ .with(\.$segment) if let x = req.parameters.get("x"){ - let dataQuery = req.parameters.get("x") - let decryptedString = try CustomCrypto.DecryptString(input: dataQuery!) - let x = decryptedString.components(separatedBy: ":") + let decryptedString = try CustomCrypto.DecryptString(input: x) + let array = decryptedString.components(separatedBy: ":") ///This call is the standard get by id call - if (x.count == 1){ - let id = Int(x[0])! + 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 diff --git a/Sources/App/Models/SpecTables/mdlSpec.swift b/Sources/App/Models/SpecTables/mdlSpec.swift index 323ba80..f482f32 100644 --- a/Sources/App/Models/SpecTables/mdlSpec.swift +++ b/Sources/App/Models/SpecTables/mdlSpec.swift @@ -30,12 +30,12 @@ final class mdlSpec: Model, Content { init() { } - init(id: Int? = nil, name: String? = nil, numberoftests: Int? = nil, isactive: Bool? = true, createuserid: Int? = nil, updateuserid: Int? = nil, itemid: Int? = nil) { + init(id: Int? = nil, name: String? = nil, numberoftests: Int? = nil, isactive: Bool? = true, createuserid: Int? = nil, updateuserid: Int? = nil, itemid: Int) { self.id = id self.name = name self.numberoftests = numberoftests - self.$item.$id.value = itemid! + self.$item.$id.value = itemid self.isactive = isactive self.createuserid = createuserid @@ -71,12 +71,11 @@ extension mdlSpec{ .with(\.$templatespecs) if let x = req.parameters.get("x"){ - let dataQuery = req.parameters.get("x") - let decryptedString = try CustomCrypto.DecryptString(input: dataQuery!) - let x = decryptedString.components(separatedBy: ":") + let decryptedString = try CustomCrypto.DecryptString(input: x) + let array = decryptedString.components(separatedBy: ":") ///This call is the standard get by id call - if (x.count == 1){ - let id = Int(x[0])! + 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 diff --git a/Sources/App/Models/SpecTables/mdlSpecItem.swift b/Sources/App/Models/SpecTables/mdlSpecItem.swift index 047071d..8cb8896 100644 --- a/Sources/App/Models/SpecTables/mdlSpecItem.swift +++ b/Sources/App/Models/SpecTables/mdlSpecItem.swift @@ -32,15 +32,15 @@ final class mdlSpecItem: Model, Content { init() { } - init(id: Int? = nil, name: String? = nil, qualifier: String? = nil, isactive: Bool? = true, createuserid: Int? = nil, sortorder: Int? = nil, updateuserid: Int? = nil, specid: Int? = nil, comparisontypeid: Int? = nil, unitid: Int? = nil) { + init(id: Int? = nil, name: String? = nil, qualifier: String? = nil, isactive: Bool? = true, createuserid: Int? = nil, sortorder: Int? = nil, updateuserid: Int? = nil, specid: Int, comparisontypeid: Int, unitid: Int) { self.id = id self.name = name self.qualifier = qualifier self.sortorder = sortorder - self.$spec.$id.value = specid! - self.$comparisontype.$id.value = comparisontypeid! - self.$unit.$id.value = unitid! + self.$spec.$id.value = specid + self.$comparisontype.$id.value = comparisontypeid + self.$unit.$id.value = unitid self.isactive = isactive @@ -81,12 +81,11 @@ extension mdlSpecItem{ .with(\.$values) if let x = req.parameters.get("x"){ - let dataQuery = req.parameters.get("x") - let decryptedString = try CustomCrypto.DecryptString(input: dataQuery!) - let x = decryptedString.components(separatedBy: ":") + let decryptedString = try CustomCrypto.DecryptString(input: x) + let array = decryptedString.components(separatedBy: ":") ///This call is the standard get by id call - if (x.count == 1){ - let id = Int(x[0])! + 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 diff --git a/Sources/App/Models/SpecTables/mdlSpecItemValue.swift b/Sources/App/Models/SpecTables/mdlSpecItemValue.swift index ede9cdc..1a1be1a 100644 --- a/Sources/App/Models/SpecTables/mdlSpecItemValue.swift +++ b/Sources/App/Models/SpecTables/mdlSpecItemValue.swift @@ -28,13 +28,13 @@ final class mdlSpecItemValue: Model, Content { init() { } - init(id: Int? = nil, value: String? = nil, sortorder: Int? = nil, isactive: Bool? = true, createuserid: Int? = nil, updateuserid: Int? = nil, specitemid: Int? = nil, valuetypeid: Int? = nil) { + init(id: Int? = nil, value: String? = nil, sortorder: Int? = nil, isactive: Bool? = true, createuserid: Int? = nil, updateuserid: Int? = nil, specitemid: Int, valuetypeid: Int) { self.id = id self.value = value self.sortorder = sortorder - self.$specitem.$id.value = specitemid! - self.$valuetype.$id.value = valuetypeid! + self.$specitem.$id.value = specitemid + self.$valuetype.$id.value = valuetypeid self.isactive = isactive self.createuserid = createuserid @@ -97,12 +97,11 @@ extension mdlSpecItemValue{ .with(\.$valuetype) if let x = req.parameters.get("x"){ - let dataQuery = req.parameters.get("x") - let decryptedString = try CustomCrypto.DecryptString(input: dataQuery!) - let x = decryptedString.components(separatedBy: ":") + let decryptedString = try CustomCrypto.DecryptString(input: x) + let array = decryptedString.components(separatedBy: ":") ///This call is the standard get by id call - if (x.count == 1){ - let id = Int(x[0])! + 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 diff --git a/Sources/App/Models/TemplateTables/mdlTemplate.swift b/Sources/App/Models/TemplateTables/mdlTemplate.swift index 33b27aa..023f245 100644 --- a/Sources/App/Models/TemplateTables/mdlTemplate.swift +++ b/Sources/App/Models/TemplateTables/mdlTemplate.swift @@ -50,12 +50,12 @@ final class mdlTemplate: Model, Content { updateuserid: Int? = nil ) { self.id = id - self.$level.$id.value = levelid! - self.$product.$id.value = productid! - self.$make.$id.value = makeid! - self.$model.$id.value = modelid! - self.$customer.$id.value = customerid! - self.$equipment.$id.value = equipmentid! + if(levelid != nil) {self.$level.$id.value = levelid} + if(productid != nil){self.$product.$id.value = productid} + if(makeid != nil){self.$make.$id.value = makeid} + if(modelid != nil){self.$model.$id.value = modelid} + if(customerid != nil){self.$customer.$id.value = customerid} + if(equipmentid != nil){self.$equipment.$id.value = equipmentid} self.name = name self.description = description self.isactive = isactive @@ -115,12 +115,11 @@ extension mdlTemplate{ .with(\.$categories) if let x = req.parameters.get("x"){ - let dataQuery = req.parameters.get("x") - let decryptedString = try CustomCrypto.DecryptString(input: dataQuery!) - let x = decryptedString.components(separatedBy: ":") + let decryptedString = try CustomCrypto.DecryptString(input: x) + let array = decryptedString.components(separatedBy: ":") ///This call is the standard get by id call - if (x.count == 1){ - let id = Int(x[0])! + 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 diff --git a/Sources/App/Models/TemplateTables/mdlTemplateCategory.swift b/Sources/App/Models/TemplateTables/mdlTemplateCategory.swift index 2ef342a..46cad26 100644 --- a/Sources/App/Models/TemplateTables/mdlTemplateCategory.swift +++ b/Sources/App/Models/TemplateTables/mdlTemplateCategory.swift @@ -72,12 +72,11 @@ extension mdlTemplateCategory{ .with(\.$items) if let x = req.parameters.get("x"){ - let dataQuery = req.parameters.get("x") - let decryptedString = try CustomCrypto.DecryptString(input: dataQuery!) - let x = decryptedString.components(separatedBy: ":") + let decryptedString = try CustomCrypto.DecryptString(input: x) + let array = decryptedString.components(separatedBy: ":") ///This call is the standard get by id call - if (x.count == 1){ - let id = Int(x[0])! + 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 diff --git a/Sources/App/Models/TemplateTables/mdlTemplateItemSpec.swift b/Sources/App/Models/TemplateTables/mdlTemplateItemSpec.swift index 08a8cd4..2a4f65f 100644 --- a/Sources/App/Models/TemplateTables/mdlTemplateItemSpec.swift +++ b/Sources/App/Models/TemplateTables/mdlTemplateItemSpec.swift @@ -39,8 +39,8 @@ final class mdlTemplateItemSpec: Model, Content { ) { self.id = id self.$templatecategory.$id.value = categoryid - self.$item.$id.value = itemid - self.$spec.$id.value = specid + if(itemid != nil){self.$item.$id.value = itemid} + if(itemid != nil){self.$spec.$id.value = specid} self.sortorder = sortorder self.createuserid = createuserid self.updateuserid = updateuserid @@ -74,12 +74,11 @@ extension mdlTemplateItemSpec{ .with(\.$spec) if let x = req.parameters.get("x"){ - let dataQuery = req.parameters.get("x") - let decryptedString = try CustomCrypto.DecryptString(input: dataQuery!) - let x = decryptedString.components(separatedBy: ":") + let decryptedString = try CustomCrypto.DecryptString(input: x) + let array = decryptedString.components(separatedBy: ":") ///This call is the standard get by id call - if (x.count == 1){ - let id = Int(x[0])! + 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 diff --git a/Sources/App/Models/WorkOrderTables/Attachments/mdlWorkOrderAttachment.swift b/Sources/App/Models/WorkOrderTables/Attachments/mdlWorkOrderAttachment.swift index 438f33f..8d06b78 100644 --- a/Sources/App/Models/WorkOrderTables/Attachments/mdlWorkOrderAttachment.swift +++ b/Sources/App/Models/WorkOrderTables/Attachments/mdlWorkOrderAttachment.swift @@ -64,12 +64,11 @@ extension mdlWorkOrderAttachment{ .with(\.$equipment) if let x = req.parameters.get("x"){ - let dataQuery = req.parameters.get("x") - let decryptedString = try CustomCrypto.DecryptString(input: dataQuery!) - let x = decryptedString.components(separatedBy: ":") + let decryptedString = try CustomCrypto.DecryptString(input: x) + let array = decryptedString.components(separatedBy: ":") ///This call is the standard get by id call - if (x.count == 1){ - let id = Int(x[0])! + 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 diff --git a/Sources/App/Models/WorkOrderTables/Consumptions/mdlConsumption.swift b/Sources/App/Models/WorkOrderTables/Consumptions/mdlConsumption.swift index 8bb72ef..149d10c 100644 --- a/Sources/App/Models/WorkOrderTables/Consumptions/mdlConsumption.swift +++ b/Sources/App/Models/WorkOrderTables/Consumptions/mdlConsumption.swift @@ -97,12 +97,11 @@ extension mdlConsumption{ .with(\.$viscosity) if let x = req.parameters.get("x"){ - let dataQuery = req.parameters.get("x") - let decryptedString = try CustomCrypto.DecryptString(input: dataQuery!) - let x = decryptedString.components(separatedBy: ":") + let decryptedString = try CustomCrypto.DecryptString(input: x) + let array = decryptedString.components(separatedBy: ":") ///This call is the standard get by id call - if (x.count == 1){ - let id = Int(x[0])! + 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 diff --git a/Sources/App/Models/WorkOrderTables/Inspections/mdlInspection.swift b/Sources/App/Models/WorkOrderTables/Inspections/mdlInspection.swift index f0756ff..3b81b8a 100644 --- a/Sources/App/Models/WorkOrderTables/Inspections/mdlInspection.swift +++ b/Sources/App/Models/WorkOrderTables/Inspections/mdlInspection.swift @@ -78,12 +78,11 @@ extension mdlInspection{ .with(\.$notes) if let x = req.parameters.get("x"){ - let dataQuery = req.parameters.get("x") - let decryptedString = try CustomCrypto.DecryptString(input: dataQuery!) - let x = decryptedString.components(separatedBy: ":") + let decryptedString = try CustomCrypto.DecryptString(input: x) + let array = decryptedString.components(separatedBy: ":") ///This call is the standard get by id call - if (x.count == 1){ - let id = Int(x[0])! + 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 diff --git a/Sources/App/Models/WorkOrderTables/Inspections/mdlInspectionCategory.swift b/Sources/App/Models/WorkOrderTables/Inspections/mdlInspectionCategory.swift index 699d385..c25e0d8 100644 --- a/Sources/App/Models/WorkOrderTables/Inspections/mdlInspectionCategory.swift +++ b/Sources/App/Models/WorkOrderTables/Inspections/mdlInspectionCategory.swift @@ -71,12 +71,11 @@ extension mdlInspectionCategory{ .with(\.$inspectionitems) if let x = req.parameters.get("x"){ - let dataQuery = req.parameters.get("x") - let decryptedString = try CustomCrypto.DecryptString(input: dataQuery!) - let x = decryptedString.components(separatedBy: ":") + let decryptedString = try CustomCrypto.DecryptString(input: x) + let array = decryptedString.components(separatedBy: ":") ///This call is the standard get by id call - if (x.count == 1){ - let id = Int(x[0])! + 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 diff --git a/Sources/App/Models/WorkOrderTables/Inspections/mdlInspectionItem.swift b/Sources/App/Models/WorkOrderTables/Inspections/mdlInspectionItem.swift index 7496e53..84fce71 100644 --- a/Sources/App/Models/WorkOrderTables/Inspections/mdlInspectionItem.swift +++ b/Sources/App/Models/WorkOrderTables/Inspections/mdlInspectionItem.swift @@ -54,8 +54,8 @@ final class mdlInspectionItem: Model, Content { self.name = name self.description = description self.sortorder = sortorder - self.$previousinspectionitem.$id.value = previousinspectionitemid - self.$previousinspectionitemresponse.$id.value = previousinspectionitemresponseid + if(previousinspectionitemid != nil){self.$previousinspectionitem.$id.value = previousinspectionitemid} + if(previousinspectionitemresponseid != nil){self.$previousinspectionitemresponse.$id.value = previousinspectionitemresponseid} self.createuserid = createuserid self.updateuserid = updateuserid } @@ -122,12 +122,11 @@ extension mdlInspectionItem{ .with(\.$notes) if let x = req.parameters.get("x"){ - let dataQuery = req.parameters.get("x") - let decryptedString = try CustomCrypto.DecryptString(input: dataQuery!) - let x = decryptedString.components(separatedBy: ":") + let decryptedString = try CustomCrypto.DecryptString(input: x) + let array = decryptedString.components(separatedBy: ":") ///This call is the standard get by id call - if (x.count == 1){ - let id = Int(x[0])! + 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 diff --git a/Sources/App/Models/WorkOrderTables/Inspections/mdlInspectionItemResponse.swift b/Sources/App/Models/WorkOrderTables/Inspections/mdlInspectionItemResponse.swift index 3c2eaad..44e87fa 100644 --- a/Sources/App/Models/WorkOrderTables/Inspections/mdlInspectionItemResponse.swift +++ b/Sources/App/Models/WorkOrderTables/Inspections/mdlInspectionItemResponse.swift @@ -77,12 +77,11 @@ extension mdlInspectionItemResponse{ .with(\.$responsetype) if let x = req.parameters.get("x"){ - let dataQuery = req.parameters.get("x") - let decryptedString = try CustomCrypto.DecryptString(input: dataQuery!) - let x = decryptedString.components(separatedBy: ":") + let decryptedString = try CustomCrypto.DecryptString(input: x) + let array = decryptedString.components(separatedBy: ":") ///This call is the standard get by id call - if (x.count == 1){ - let id = Int(x[0])! + 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 diff --git a/Sources/App/Models/WorkOrderTables/Inspections/mdlInspectionItemSpec.swift b/Sources/App/Models/WorkOrderTables/Inspections/mdlInspectionItemSpec.swift index dbee740..b2e059d 100644 --- a/Sources/App/Models/WorkOrderTables/Inspections/mdlInspectionItemSpec.swift +++ b/Sources/App/Models/WorkOrderTables/Inspections/mdlInspectionItemSpec.swift @@ -69,12 +69,11 @@ extension mdlInspectionItemSpec{ .with(\.$inspectionitem) if let x = req.parameters.get("x"){ - let dataQuery = req.parameters.get("x") - let decryptedString = try CustomCrypto.DecryptString(input: dataQuery!) - let x = decryptedString.components(separatedBy: ":") + let decryptedString = try CustomCrypto.DecryptString(input: x) + let array = decryptedString.components(separatedBy: ":") ///This call is the standard get by id call - if (x.count == 1){ - let id = Int(x[0])! + 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 diff --git a/Sources/App/Models/WorkOrderTables/Inspections/mdlInspectionItemSpecItem.swift b/Sources/App/Models/WorkOrderTables/Inspections/mdlInspectionItemSpecItem.swift index 760befe..8e6db30 100644 --- a/Sources/App/Models/WorkOrderTables/Inspections/mdlInspectionItemSpecItem.swift +++ b/Sources/App/Models/WorkOrderTables/Inspections/mdlInspectionItemSpecItem.swift @@ -93,12 +93,11 @@ extension mdlInspectionItemSpecItem{ .with(\.$values) if let x = req.parameters.get("x"){ - let dataQuery = req.parameters.get("x") - let decryptedString = try CustomCrypto.DecryptString(input: dataQuery!) - let x = decryptedString.components(separatedBy: ":") + let decryptedString = try CustomCrypto.DecryptString(input: x) + let array = decryptedString.components(separatedBy: ":") ///This call is the standard get by id call - if (x.count == 1){ - let id = Int(x[0])! + 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 diff --git a/Sources/App/Models/WorkOrderTables/Inspections/mdlInspectionItemSpecItemTest.swift b/Sources/App/Models/WorkOrderTables/Inspections/mdlInspectionItemSpecItemTest.swift index 2af4c1e..4086bbb 100644 --- a/Sources/App/Models/WorkOrderTables/Inspections/mdlInspectionItemSpecItemTest.swift +++ b/Sources/App/Models/WorkOrderTables/Inspections/mdlInspectionItemSpecItemTest.swift @@ -66,12 +66,11 @@ extension mdlInspectionItemSpecItemTest{ .with(\.$specitem) if let x = req.parameters.get("x"){ - let dataQuery = req.parameters.get("x") - let decryptedString = try CustomCrypto.DecryptString(input: dataQuery!) - let x = decryptedString.components(separatedBy: ":") + let decryptedString = try CustomCrypto.DecryptString(input: x) + let array = decryptedString.components(separatedBy: ":") ///This call is the standard get by id call - if (x.count == 1){ - let id = Int(x[0])! + 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 diff --git a/Sources/App/Models/WorkOrderTables/Inspections/mdlInspectionItemSpecItemValue.swift b/Sources/App/Models/WorkOrderTables/Inspections/mdlInspectionItemSpecItemValue.swift index 949f33d..23fbe96 100644 --- a/Sources/App/Models/WorkOrderTables/Inspections/mdlInspectionItemSpecItemValue.swift +++ b/Sources/App/Models/WorkOrderTables/Inspections/mdlInspectionItemSpecItemValue.swift @@ -67,12 +67,11 @@ extension mdlInspectionItemSpecItemValue{ .with(\.$valuetype) if let x = req.parameters.get("x"){ - let dataQuery = req.parameters.get("x") - let decryptedString = try CustomCrypto.DecryptString(input: dataQuery!) - let x = decryptedString.components(separatedBy: ":") + let decryptedString = try CustomCrypto.DecryptString(input: x) + let array = decryptedString.components(separatedBy: ":") ///This call is the standard get by id call - if (x.count == 1){ - let id = Int(x[0])! + 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 diff --git a/Sources/App/Models/WorkOrderTables/Inspections/mdlInspectionLevel.swift b/Sources/App/Models/WorkOrderTables/Inspections/mdlInspectionLevel.swift index b7649c8..035c2bf 100644 --- a/Sources/App/Models/WorkOrderTables/Inspections/mdlInspectionLevel.swift +++ b/Sources/App/Models/WorkOrderTables/Inspections/mdlInspectionLevel.swift @@ -75,12 +75,11 @@ extension mdlInspectionLevel{ .with(\.$templates) if let x = req.parameters.get("x"){ - let dataQuery = req.parameters.get("x") - let decryptedString = try CustomCrypto.DecryptString(input: dataQuery!) - let x = decryptedString.components(separatedBy: ":") + let decryptedString = try CustomCrypto.DecryptString(input: x) + let array = decryptedString.components(separatedBy: ":") ///This call is the standard get by id call - if (x.count == 1){ - let id = Int(x[0])! + 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 diff --git a/Sources/App/Models/WorkOrderTables/Inspections/mdlInspectionType.swift b/Sources/App/Models/WorkOrderTables/Inspections/mdlInspectionType.swift index 9c3cc3e..3bcf165 100644 --- a/Sources/App/Models/WorkOrderTables/Inspections/mdlInspectionType.swift +++ b/Sources/App/Models/WorkOrderTables/Inspections/mdlInspectionType.swift @@ -71,12 +71,11 @@ extension mdlInspectionType{ .with(\.$inspectionlevels) if let x = req.parameters.get("x"){ - let dataQuery = req.parameters.get("x") - let decryptedString = try CustomCrypto.DecryptString(input: dataQuery!) - let x = decryptedString.components(separatedBy: ":") + let decryptedString = try CustomCrypto.DecryptString(input: x) + let array = decryptedString.components(separatedBy: ":") ///This call is the standard get by id call - if (x.count == 1){ - let id = Int(x[0])! + 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 diff --git a/Sources/App/Models/WorkOrderTables/Notes/mdlNote.swift b/Sources/App/Models/WorkOrderTables/Notes/mdlNote.swift index 175b714..07b04cd 100644 --- a/Sources/App/Models/WorkOrderTables/Notes/mdlNote.swift +++ b/Sources/App/Models/WorkOrderTables/Notes/mdlNote.swift @@ -98,12 +98,11 @@ extension mdlNote{ .with(\.$item) if let x = req.parameters.get("x"){ - let dataQuery = req.parameters.get("x") - let decryptedString = try CustomCrypto.DecryptString(input: dataQuery!) - let x = decryptedString.components(separatedBy: ":") + let decryptedString = try CustomCrypto.DecryptString(input: x) + let array = decryptedString.components(separatedBy: ":") ///This call is the standard get by id call - if (x.count == 1){ - let id = Int(x[0])! + 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 diff --git a/Sources/App/Models/WorkOrderTables/Parts/mdlWorkOrderPart.swift b/Sources/App/Models/WorkOrderTables/Parts/mdlWorkOrderPart.swift index 258018b..2a3892a 100644 --- a/Sources/App/Models/WorkOrderTables/Parts/mdlWorkOrderPart.swift +++ b/Sources/App/Models/WorkOrderTables/Parts/mdlWorkOrderPart.swift @@ -107,12 +107,11 @@ extension mdlWorkOrderPart{ .with(\.$segment) if let x = req.parameters.get("x"){ - let dataQuery = req.parameters.get("x") - let decryptedString = try CustomCrypto.DecryptString(input: dataQuery!) - let x = decryptedString.components(separatedBy: ":") + let decryptedString = try CustomCrypto.DecryptString(input: x) + let array = decryptedString.components(separatedBy: ":") ///This call is the standard get by id call - if (x.count == 1){ - let id = Int(x[0])! + 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 diff --git a/Sources/App/Models/WorkOrderTables/Samples/mdlOilSample.swift b/Sources/App/Models/WorkOrderTables/Samples/mdlOilSample.swift index ae8dbac..31fc167 100644 --- a/Sources/App/Models/WorkOrderTables/Samples/mdlOilSample.swift +++ b/Sources/App/Models/WorkOrderTables/Samples/mdlOilSample.swift @@ -142,12 +142,11 @@ extension mdlOilSample{ .with(\.$segment) if let x = req.parameters.get("x"){ - let dataQuery = req.parameters.get("x") - let decryptedString = try CustomCrypto.DecryptString(input: dataQuery!) - let x = decryptedString.components(separatedBy: ":") + let decryptedString = try CustomCrypto.DecryptString(input: x) + let array = decryptedString.components(separatedBy: ":") ///This call is the standard get by id call - if (x.count == 1){ - let id = Int(x[0])! + 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 diff --git a/Sources/App/Models/WorkOrderTables/Signatures/mdlSignature.swift b/Sources/App/Models/WorkOrderTables/Signatures/mdlSignature.swift index a8fda45..c551647 100644 --- a/Sources/App/Models/WorkOrderTables/Signatures/mdlSignature.swift +++ b/Sources/App/Models/WorkOrderTables/Signatures/mdlSignature.swift @@ -84,12 +84,11 @@ extension mdlSignature{ .with(\.$signaturetype) if let x = req.parameters.get("x"){ - let dataQuery = req.parameters.get("x") - let decryptedString = try CustomCrypto.DecryptString(input: dataQuery!) - let x = decryptedString.components(separatedBy: ":") + let decryptedString = try CustomCrypto.DecryptString(input: x) + let array = decryptedString.components(separatedBy: ":") ///This call is the standard get by id call - if (x.count == 1){ - let id = Int(x[0])! + 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 diff --git a/Sources/App/Models/WorkOrderTables/mdlWorkOrder.swift b/Sources/App/Models/WorkOrderTables/mdlWorkOrder.swift index dda9ae7..07b4fe4 100644 --- a/Sources/App/Models/WorkOrderTables/mdlWorkOrder.swift +++ b/Sources/App/Models/WorkOrderTables/mdlWorkOrder.swift @@ -152,12 +152,11 @@ extension mdlWorkOrder{ .with(\.$notes) if let x = req.parameters.get("x"){ - let dataQuery = req.parameters.get("x") - let decryptedString = try CustomCrypto.DecryptString(input: dataQuery!) - let x = decryptedString.components(separatedBy: ":") + let decryptedString = try CustomCrypto.DecryptString(input: x) + let array = decryptedString.components(separatedBy: ":") ///This call is the standard get by id call - if (x.count == 1){ - let id = Int(x[0])! + 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 diff --git a/Sources/App/Models/WorkOrderTables/mdlWorkOrderFlow.swift b/Sources/App/Models/WorkOrderTables/mdlWorkOrderFlow.swift index 2c09751..3807caa 100644 --- a/Sources/App/Models/WorkOrderTables/mdlWorkOrderFlow.swift +++ b/Sources/App/Models/WorkOrderTables/mdlWorkOrderFlow.swift @@ -71,12 +71,11 @@ extension mdlWorkOrderFlow{ // .with(\.$<#parent/children#>) if let x = req.parameters.get("x"){ - let dataQuery = req.parameters.get("x") - let decryptedString = try CustomCrypto.DecryptString(input: dataQuery!) - let x = decryptedString.components(separatedBy: ":") + let decryptedString = try CustomCrypto.DecryptString(input: x) + let array = decryptedString.components(separatedBy: ":") ///This call is the standard get by id call - if (x.count == 1){ - let id = Int(x[0])! + 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 diff --git a/Sources/App/Models/WorkOrderTables/mdlWorkOrderSegment.swift b/Sources/App/Models/WorkOrderTables/mdlWorkOrderSegment.swift index 7887339..091c11f 100644 --- a/Sources/App/Models/WorkOrderTables/mdlWorkOrderSegment.swift +++ b/Sources/App/Models/WorkOrderTables/mdlWorkOrderSegment.swift @@ -71,12 +71,11 @@ extension mdlWorkOrderSegment{ .with(\.$consumptions) if let x = req.parameters.get("x"){ - let dataQuery = req.parameters.get("x") - let decryptedString = try CustomCrypto.DecryptString(input: dataQuery!) - let x = decryptedString.components(separatedBy: ":") + let decryptedString = try CustomCrypto.DecryptString(input: x) + let array = decryptedString.components(separatedBy: ":") ///This call is the standard get by id call - if (x.count == 1){ - let id = Int(x[0])! + 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 diff --git a/Sources/App/routes.swift b/Sources/App/routes.swift index 09b09a5..ee8a923 100644 --- a/Sources/App/routes.swift +++ b/Sources/App/routes.swift @@ -18,7 +18,7 @@ func routes(_ app: Application) throws { } try baseApp.register(collection: cntrlCategory()) try baseApp.register(collection: cntrlValueType()) - try baseApp.register(collection: cntrlDataTypes()) + try baseApp.register(collection: cntrlDataType()) try baseApp.register(collection: cntrlRole()) try baseApp.register(collection: cntrlUser()) try baseApp.register(collection: cntrlLocation()) @@ -67,7 +67,7 @@ func routes(_ app: Application) throws { try baseApp.register(collection: cntrlSpecItem()) try baseApp.register(collection: cntrlSpecItemValue()) try baseApp.register(collection: cntrlWorkOrder()) - try baseApp.register(collection: cntrlWorkorderFlow()) + try baseApp.register(collection: cntrlWorkOrderFlow()) try baseApp.register(collection: cntrlSignature()) try baseApp.register(collection: cntrlWorkOrderPart()) try baseApp.register(collection: cntrlWorkOrderAttachment()) diff --git a/Tests/AppTests/UserTests.swift b/Tests/AppTests/UserTests.swift index 8a453b6..b3aee38 100644 --- a/Tests/AppTests/UserTests.swift +++ b/Tests/AppTests/UserTests.swift @@ -2,78 +2,113 @@ import Fluent import XCTVapor -final class AppTests: XCTestCase { - - func testUsersIndex() async throws{ - let app = Application(.testing) - defer { app.shutdown() } - try await configure(app) - try app.test(.GET, "api/v2.0/users/", afterResponse: { res in - XCTAssertEqual(res.status, .ok) - XCTAssertEqual(res.headers.contentType, .json) - _ = try res.content.decode([mdlUser].self) - }) - } - - func testUsersRead() async throws{ - let app = Application(.testing) - defer{app.shutdown()} - try await configure(app) - let basepath = "api/v2.0/users/" - let parameter = try CustomCrypto.EncryptString(input: "1") - print(parameter) - let path = "\(basepath)\(parameter)" - try app.test(.GET, path, afterResponse: { res in - XCTAssertEqual(res.status, .ok) - XCTAssertEqual(res.headers.contentType, .json) - _ = try res.content.decode(mdlUser.self) - }) - } - - func testUserLogin() async throws{ +final class UserTests: XCTestCase { + + func test1() async throws{ let app = Application(.testing) defer{app.shutdown()} try await configure(app) - let api = "api/v2.0/users/login/" - let gooduser = "bryckman" - let goodpwd = "password" - - - let baduser = "brycman_" - let badpwd = "p@$$w0rd" - - var combo = "\(gooduser):\(goodpwd)" - var parameter = try CustomCrypto.EncryptString(input: combo) - var path = "\(api)\(parameter)" - try app.test(.GET, path, afterResponse: { - res in - XCTAssertEqual(res.status, .ok) - XCTAssertEqual(res.headers.contentType, .json) - _ = try res.content.decode(mdlUser.self) - }) - combo = "\(gooduser):\(badpwd)" - parameter = try CustomCrypto.EncryptString(input: combo) - path = "\(api)\(parameter)" - try app.test(.GET, path, afterResponse: { - res in - XCTAssertEqual(res.status, .unauthorized) - XCTAssertEqual(res.headers.contentType, .json) - }) - combo = "\(baduser):\(badpwd)" - parameter = try CustomCrypto.EncryptString(input: combo) - path = "\(api)\(parameter)" - try app.test(.GET, path, afterResponse: { - res in - XCTAssertEqual(res.status, .unauthorized) - XCTAssertEqual(res.headers.contentType, .json) - }) - combo = "\(baduser):\(goodpwd)" - parameter = try CustomCrypto.EncryptString(input: combo) - path = "\(api)\(parameter)" - try app.test(.GET, path, afterResponse: { + let basepath = "/api/v2.0/users/" + let fn = "Mike" + let ln = "Carman" + let email = "mcarman@rpmindustries.com" + let username = "bryckman" + let password = "password" + let isactive = true + let cuserid = -1 + let uuserid = -1 + //new + try app.test(.POST, basepath, beforeRequest: { + req in + let input = mdlUser(id: nil, number: nil, firstname: fn, lastname: ln, username: username, password: password, email: email, isactive: isactive, createuserid: cuserid, updateuserid: uuserid, divisionid: nil, locationid: nil, gateid: nil) + try req.content.encode(input) + }, afterResponse: { res in - XCTAssertEqual(res.status, .unauthorized) - XCTAssertEqual(res.headers.contentType, .json) + XCTAssertEqual(res.status, .created) + let obj = try res.content.decode(mdlUser.self) + XCTAssertEqual(obj.firstname, fn) + let id = obj.id + + //Update object just created + try app.test(.POST, basepath, beforeRequest:{ + req in + let input = mdlUser(id: id, number: "1", firstname: fn, lastname: ln, username: username, password: password, email: email, isactive: isactive, createuserid: cuserid, updateuserid: uuserid, divisionid: nil, locationid: nil, gateid: nil) + try req.content.encode(input) + }, afterResponse: { + res in + XCTAssertEqual(res.status, .accepted) + let obj = try res.content.decode(mdlUser.self) + XCTAssertEqual(obj.firstname, fn) + }) + + //All + try app.test(.GET, basepath, afterResponse: { res in + XCTAssertEqual(res.status, .ok) + XCTAssertEqual(res.headers.contentType, .json) + _ = try res.content.decode([mdlUser].self) + }) + + //read + var parameter = try CustomCrypto.EncryptString(input: String(id!)) + + var path = "\(basepath)\(parameter)" + try app.test(.GET, path, afterResponse: { res in + XCTAssertEqual(res.status, .ok) + XCTAssertEqual(res.headers.contentType, .json) + _ = try res.content.decode(mdlUser.self) + }) + + //login + let gooduser = "bryckman" + let goodpwd = "password" + + + let baduser = "brycman_" + let badpwd = "p@$$w0rd" + + var combo = "\(gooduser):\(goodpwd)" + parameter = try CustomCrypto.EncryptString(input: combo) + path = "\(basepath)login/\(parameter)" + try app.test(.GET, path, afterResponse: { + res in + XCTAssertEqual(res.status, .ok) + XCTAssertEqual(res.headers.contentType, .json) + _ = try res.content.decode(mdlUser.self) + }) + combo = "\(gooduser):\(badpwd)" + parameter = try CustomCrypto.EncryptString(input: combo) + path = "\(basepath)login/\(parameter)" + try app.test(.GET, path, afterResponse: { + res in + XCTAssertEqual(res.status, .unauthorized) + XCTAssertEqual(res.headers.contentType, .json) + }) + combo = "\(baduser):\(badpwd)" + parameter = try CustomCrypto.EncryptString(input: combo) + path = "\(basepath)login/\(parameter)" + try app.test(.GET, path, afterResponse: { + res in + XCTAssertEqual(res.status, .unauthorized) + XCTAssertEqual(res.headers.contentType, .json) + }) + combo = "\(baduser):\(goodpwd)" + parameter = try CustomCrypto.EncryptString(input: combo) + path = "\(basepath)login/\(parameter)" + try app.test(.GET, path, afterResponse: { + res in + XCTAssertEqual(res.status, .unauthorized) + XCTAssertEqual(res.headers.contentType, .json) + }) + + //delete user +// let encryptedid = try CustomCrypto.EncryptString(input: String(id!)) +// path = "api/v2.0/users/" +// let url = "\(path)\(encryptedid)" +// try app.test(.DELETE, url, afterResponse: { +// res in +// XCTAssertEqual(res.status, .ok) +// _ = try res.content.decode(mdlUser.self) +// }) }) }