// // File.swift // // // Created by Michiel Carman on 11/2/21. // import Foundation import Vapor import Fluent import FluentPostgresDriver final class GateCtrl{ //Create Division func create(req: Request) throws -> EventLoopFuture{ let input = try req.content.decode(mdlGate.Input.self) let output = buildObj(input) return output.save(on: req.db).map{ self.buildObj(output) } } //Update Division func update(req: Request) throws -> EventLoopFuture{ let input = try req.content.decode(mdlGate.Input.self) return try qbase(req: req).first().unwrap(or: Abort(.internalServerError)).flatMap{output in self.buildObj(output, input) return self.save(output, req) } } //Get all Divisions func readAll(req: Request) throws -> EventLoopFuture>{ return try qbase(req: req) .paginate(for: req) .map{ page in page.map{ self.buildObj($0) } } } //Get 1 Division func read(req: Request) throws -> EventLoopFuture{ return try qbase(req: req).first().unwrap(or: Abort(.internalServerError)).map{self.buildObj($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 mdlGate.find(id, on: req.db).unwrap(or: Abort(.notFound)).flatMap{ $0.delete(on: req.db)}.map{.ok} } } extension GateCtrl{ //Query Settings func qbase(req: Request) throws -> QueryBuilder { let query = mdlGate.query(on: req.db).with(\.$location) if let id = req.parameters.get("id", as: Int.self){ return query.filter(\.$id == id) } else{ return query } } //create Input func buildObj(_ input: mdlGate.Input) -> mdlGate { return mdlGate(name: input.name, description: input.description, sortorder: input.sortorder, isactive: input.isactive, createuserid: input.createuserid, createdate: input.createdate, updateuserid: input.updateuserid, updatedate: input.updatedate, location: input.location) } //create Output func buildObj(_ output: mdlGate) -> mdlGate.Output { return mdlGate.Output(id: output.id!, name: output.name, description: output.description, sortorder: output.sortorder, isactive: output.isactive, createdate: output.createdate, createuserid: output.createuserid, updatedate: output.updatedate, updateuserid: output.updateuserid, location: output.location) } func buildObj(_ output: Optional.WrappedType, _ input: mdlGate.Input){ output.name = input.name; output.description = input.description; output.sortorder = input.sortorder; output.isactive = input.isactive; output.createdate = input.createdate; output.createuserid = input.createuserid; output.updatedate = input.updatedate; output.updateuserid = input.updateuserid; output.location = input.location; } func save(_ output: mdlGate, _ req: Request) -> EventLoopFuture{ return output.save(on: req.db).map{self.buildObj(output)} } } extension GateCtrl: RouteCollection{ func boot(routes: RoutesBuilder) throws{ let apigroup = routes.grouped("gates") apigroup.get("", use: readAll) apigroup.get(":id", use: read) apigroup.post("", use: create) apigroup.post(":id", use: update) apigroup.delete(":id", use: delete) } }