// // ItemCtrl.swift // // // Created by Michiel Carman on 11/9/21. // import Foundation import Vapor import Fluent import FluentPostgresDriver final class ItemCtrl{ //Create Unit func create(req: Request) throws -> EventLoopFuture{ let input = try req.content.decode(mdlItem.Input.self) let output = self.createUnit(input) return self.save(output, req, input) } //Update Unit func update(req: Request) throws -> EventLoopFuture{ let input = try req.content.decode(mdlItem.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>{ 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{ return try qbase(req: req).first().unwrap(or: Abort(.internalServerError)).map{self.createRead($0)} } //Delete 1 Division func delete(req: Request) throws -> EventLoopFuture{ guard let id = req.parameters.get("id", as: Int.self) else{ throw Abort(.badRequest) } return mdlItem.find(id, on: req.db).unwrap(or: Abort(.notFound)).flatMap{ $0.delete(on: req.db)}.map{.ok} } } extension ItemCtrl{ //Query Settings func qbase(req: Request) throws -> QueryBuilder { let query = mdlItem.query(on: req.db) .with(\.$category) .with(\.$code) .with(\.$inputtype) .with(\.$specs) .with(\.$responsetypes) .with(\.$templatecategoryitems) .with(\.$inspectioncategoryitems) if let id = req.parameters.get("id", as: Int.self){ return query.filter(\.$id == id) } else{ return query } } //create Output func createRead(_ output: mdlItem) -> mdlItem.Output { return mdlItem.Output( id: output.id!, name: output.name, description: output.description, isactive: output.isactive, createdate: output.createdate, createuserid: output.createuserid, updatedate: output.updatedate, updateuserid: output.updateuserid, category: output.$category.value!, code: output.$code.value!, inputtype: output.$inputtype.value!, responsetypes: output.$responsetypes.value! , specs: output.$specs.value!, templatecategoryitems: output.$templatecategoryitems.value!, inspectioncategoryitems: output.$inspectioncategoryitems.value! ) } func mapOutput(_ output: Optional.WrappedType, _ input: mdlItem.Input){ output.name = input.name; output.description = input.description; output.isactive = input.isactive; output.createdate = input.createdate; output.updatedate = input.updatedate; output.createuserid = input.createuserid; output.updateuserid = input.updateuserid; output.$category.id = input.category.id!; output.$code.id = input.code.id!; output.$inputtype.id = input.inputtype.id!; output.$specs.value = input.specs; output.$templatecategoryitems.value = input.templatecategoryitems; output.$inspectioncategoryitems.value = input.inspectioncategoryitems; output.$responsetypes.value = input.responsetypes; } func createUnit(_ input: mdlItem.Input) -> mdlItem { return mdlItem( id: input.id ?? nil , name: input.name , description: input.description , isactive: input.isactive , createuserid: input.createuserid ?? -1 , createdate: input.createdate , updateuserid: input.updateuserid ?? -1 , updatedate: input.updatedate , responsetypes: input.responsetypes , specs: input.specs , templatecategoryitems: input.templatecategoryitems , inspectioncategoryitems: input.inspectioncategoryitems ) } //update function func save(_ output: Optional.WrappedType, req: Request) -> EventLoopFuture { return output .save(on: req.db) .map{mdlItem.Output( id: output.id! , name: output.name , description: output.description , isactive: output.isactive , createdate: output.createdate , createuserid: output.createuserid , updatedate: output.updatedate , updateuserid: output.updateuserid , category: output.category , code: output.code , inputtype: output.inputtype , responsetypes: output.responsetypes , specs: output.specs , templatecategoryitems: output.templatecategoryitems , inspectioncategoryitems: output.inspectioncategoryitems ) } } //create function func save(_ output: mdlItem, _ req: Request, _ input: mdlItem.Input) -> EventLoopFuture { return output .save(on: req.db) .map{ mdlItem.Output( id: output.id! , name: output.name , description: output.description , isactive: output.isactive , createdate: output.createdate , createuserid: output.createuserid , updatedate: output.updatedate , updateuserid: output.updateuserid , category: input.category , code: input.code , inputtype: input.inputtype , responsetypes: input.responsetypes , specs: input.specs , templatecategoryitems: input.templatecategoryitems , inspectioncategoryitems: input.inspectioncategoryitems ) } } } extension ItemCtrl: RouteCollection{ func boot(routes: RoutesBuilder) throws{ let apigroup = routes.grouped("items") apigroup.get("", use: readAll) apigroup.get(":id", use: read) apigroup.post("", use: create) apigroup.post(":id", use: update) apigroup.delete(":id", use: delete) } }