// // ModelCtrl.swift // // // Created by Michiel Carman on 11/5/21. // import Foundation import Vapor import Fluent import FluentPostgresDriver import AppKit final class ModelCtrl{ //Create Unit func create(req: Request) throws -> EventLoopFuture{ let input = try req.content.decode(mdlModel.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(mdlModel.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 mdlModel.find(id, on: req.db).unwrap(or: Abort(.notFound)).flatMap{ $0.delete(on: req.db)}.map{.ok} } } extension ModelCtrl{ //Query Settings func qbase(req: Request) throws -> QueryBuilder { let query = mdlModel.query(on: req.db) .with(\.$make) .with(\.$product) .with(\.$templates) .with(\.$machines) if let id = req.parameters.get("id", as: Int.self){ return query.filter(\.$id == id) } else{ return query } } //create Output func createRead(_ output: mdlModel) -> mdlModel.Output { return mdlModel.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, make: output.$make.value!, product: output.$product.value!, templates: output.$templates.value!, machines: output.$machines.value! ) } func makemake(_ output: Optional.Wrapped,_ input: mdlMake.Input) { output.id = input.id!; output.name = input.name; output.description = input.description; output.isactive = input.isactive; output.createdate = input.createdate; output.createuserid = input.createuserid; output.updatedate = input.updatedate; output.updateuserid = input.updateuserid; output.$templates.value = input.templates; output.$models.value = input.models; } func makeproduct(input: mdlModel.Input) -> mdlProduct{ return mdlProduct(id: input.product.id!, code: input.product.code, description: input.product.description, isactive: input.product.isactive, createuserid: input.product.createuserid, createdate: input.product.createdate, updateuserid: input.product.updateuserid, updatedate: input.product.updatedate, models: input.product.models, templates: input.product.templates) } func mapOutput(_ output: Optional.WrappedType, _ input: mdlModel.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.$make.id = input.make.id!; output.$product.id = input.product.id!; output.$templates.value = input.templates; output.$machines.value = input.machines } func createUnit(_ input: mdlModel.Input) -> mdlModel { return mdlModel( 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 , make: input.make , product: input.product , templates: input.templates , machines: input.machines ) } //update function func save(_ output: Optional.WrappedType, req: Request) -> EventLoopFuture { return output .save(on: req.db) .map{mdlModel.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 , make: output.make , product: output.product , templates: output.templates , machines: output.machines ) } } //create function func save(_ output: mdlModel, _ req: Request, _ input: mdlModel.Input) -> EventLoopFuture { return output .save(on: req.db) .map{ mdlModel.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 , make: input.make , product: input.product , templates: input.templates , machines: input.machines ) } } } extension ModelCtrl: RouteCollection{ func boot(routes: RoutesBuilder) throws{ let apigroup = routes.grouped("models") apigroup.get("", use: readAll) apigroup.get(":id", use: read) apigroup.post("", use: create) apigroup.post(":id", use: update) apigroup.delete(":id", use: delete) } }