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