|
- //
- // InspectionCategoryItemSpecCtrl.swift
- //
- //
- // Created by Michiel Carman on 11/11/21.
- //
-
- import Foundation
- import Vapor
- import Fluent
- import FluentPostgresDriver
-
- final class InspectionCategoryItemSpecCtrl{
- //Create Unit
- func create(req: Request) throws -> EventLoopFuture<mdlInspectionCategoryItemSpec.Output>{
- let input = try req.content.decode(mdlInspectionCategoryItemSpec.Input.self)
- let output = self.createUnit(input)
- return self.save(output, req, input)
-
- }
-
- //Update Unit
- func update(req: Request) throws -> EventLoopFuture<mdlInspectionCategoryItemSpec.Output>{
- let input = try req.content.decode(mdlInspectionCategoryItemSpec.Input.self)
- return try qbase(req: req).first().unwrap(or: Abort(.notFound)).flatMap{output in self.mapOutput(output, input)
- return self.save(output, req: req)
- }
- }
-
- //Get all Divisions
- func readAll(req: Request) throws -> EventLoopFuture<Page<mdlInspectionCategoryItemSpec.Output>>{
- return try qbase(req: req)
- .paginate(for: req)
- .map{
- page in page.map{
- self.createRead($0)
- }
- }
- }
- //Get 1 Division
- func read(req: Request) throws -> EventLoopFuture<mdlInspectionCategoryItemSpec.Output>{
-
- return try qbase(req: req).first().unwrap(or: Abort(.internalServerError)).map{self.createRead($0)}
-
- }
- //Delete 1 Division
- func delete(req: Request) throws -> EventLoopFuture<HTTPStatus>{
-
- guard let id = req.parameters.get("id", as: Int.self) else{
- throw Abort(.badRequest)
- }
- return mdlInspectionCategoryItemSpec.find(id, on: req.db).unwrap(or: Abort(.notFound)).flatMap{ $0.delete(on: req.db)}.map{.ok}
- }
-
- }
- extension InspectionCategoryItemSpecCtrl{
- //Query Settings
- func qbase(req: Request) throws -> QueryBuilder<mdlInspectionCategoryItemSpec> {
- let query = mdlInspectionCategoryItemSpec.query(on: req.db)
- .with(\.$inspectioncategoryitem)
- .with(\.$spec)
- .with(\.$inspectioncategoryitemspecitems)
- if let id = req.parameters.get("id", as: Int.self){
- return query.filter(\.$id == id)
- } else{
- return query
- }
- }
- //create Output
- func createRead(_ output: mdlInspectionCategoryItemSpec) -> mdlInspectionCategoryItemSpec.Output {
- return mdlInspectionCategoryItemSpec.Output(
- id: output.id!,
- name: output.name,
- numberoftests: output.numberoftests,
- createdate: output.createdate,
- createuserid: output.createuserid,
- updatedate: output.updatedate,
- updateuserid: output.updateuserid,
- inspectioncategoryitem: output.$inspectioncategoryitem.value!,
- spec: output.$spec.value!,
- inspectioncategoryitemspecitems: output.$inspectioncategoryitemspecitems.value!
- )
- }
-
- func mapOutput(_ output: Optional<mdlInspectionCategoryItemSpec>.WrappedType, _ input: mdlInspectionCategoryItemSpec.Input){
- output.name = input.name;
- output.numberoftests = input.numberoftests;
- output.createdate = input.createdate;
- output.updatedate = input.updatedate;
- output.createuserid = input.createuserid;
- output.updateuserid = input.updateuserid;
- output.$inspectioncategoryitem.id = input.inspectioncategoryitem.id!;
- output.$spec.id = input.spec.id!;
- output.$inspectioncategoryitemspecitems.value = input.inspectioncategoryitemspecitems
- }
-
- func createUnit(_ input: mdlInspectionCategoryItemSpec.Input) -> mdlInspectionCategoryItemSpec {
- return mdlInspectionCategoryItemSpec(
- id: input.id ?? nil
- , name: input.name
- , numberoftests: input.numberoftests
- , createuserid: input.createuserid ?? -1
- , createdate: input.createdate
- , updateuserid: input.updateuserid ?? -1
- , updatedate: input.updatedate
- , inspectioncategoryitem: input.inspectioncategoryitem
- , spec: input.spec
- , inspectioncategoryitemspecitems: input.inspectioncategoryitemspecitems
- )
- }
-
- //update function
- func save(_ output: Optional<mdlInspectionCategoryItemSpec>.WrappedType, req: Request) -> EventLoopFuture<mdlInspectionCategoryItemSpec.Output> {
- return output
- .save(on: req.db)
- .map{mdlInspectionCategoryItemSpec.Output(
- id: output.id!
- , name: output.name
- , numberoftests: output.numberoftests
- , createdate: output.createdate
- , createuserid: output.createuserid
- , updatedate: output.updatedate
- , updateuserid: output.updateuserid
- , inspectioncategoryitem: output.inspectioncategoryitem
- , spec: output.spec
- , inspectioncategoryitemspecitems: output.inspectioncategoryitemspecitems
- )
- }
- }
- //create function
- func save(_ output: mdlInspectionCategoryItemSpec, _ req: Request, _ input: mdlInspectionCategoryItemSpec.Input) -> EventLoopFuture<mdlInspectionCategoryItemSpec.Output> {
- return output
- .save(on: req.db)
- .map{
- mdlInspectionCategoryItemSpec.Output(
- id: output.id!
- , name: output.name
- , numberoftests: output.numberoftests
- , createdate: output.createdate
- , createuserid: output.createuserid
- , updatedate: output.updatedate
- , updateuserid: output.updateuserid
- , inspectioncategoryitem: input.inspectioncategoryitem
- , spec: input.spec
- , inspectioncategoryitemspecitems: input.inspectioncategoryitemspecitems
- )
-
- }
- }
-
- }
- extension InspectionCategoryItemSpecCtrl: RouteCollection{
- func boot(routes: RoutesBuilder) throws{
- let apigroup = routes.grouped("inspectioncategoryitemspecs")
- apigroup.get("", use: readAll)
- apigroup.get(":id", use: read)
- apigroup.post("", use: create)
- apigroup.post(":id", use: update)
- apigroup.delete(":id", use: delete)
-
-
- }
- }
|