|
- //
- // CustomerCtrl.swift
- //
- //
- // Created by Michiel Carman on 11/4/21.
- //
-
- import Foundation
- import Vapor
- import Fluent
- import FluentPostgresDriver
-
- final class CustomerCtrl{
- //Create Unit
- func create(req: Request) throws -> EventLoopFuture<mdlCustomer.Output>{
- let input = try req.content.decode(mdlCustomer.Input.self)
- let output = self.createUnit(input)
- return self.save(output, req, input)
-
- }
-
- //Update Unit
- func update(req: Request) throws -> EventLoopFuture<mdlCustomer.Output>{
- let input = try req.content.decode(mdlCustomer.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<mdlCustomer.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<mdlCustomer.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 mdlCustomer.find(id, on: req.db).unwrap(or: Abort(.notFound)).flatMap{ $0.delete(on: req.db)}.map{.ok}
- }
-
- }
- extension CustomerCtrl{
- //Query Settings
- func qbase(req: Request) throws -> QueryBuilder<mdlCustomer> {
- let query = mdlCustomer.query(on: req.db)
- .with(\.$contacts)
- .with(\.$templates)
- .with(\.$workorders)
- .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: mdlCustomer) -> mdlCustomer.Output {
- return mdlCustomer.Output(
- id: output.id!,
- number: output.number,
- name: output.name,
- address: output.address,
- address2: output.address2,
- city: output.city,
- state: output.state,
- zip: output.zip,
- phone1: output.phone1,
- phone2: output.phone2,
- fax: output.fax,
- isactive: output.isactive,
- createdate: output.createdate,
- createuserid: output.createuserid,
- updatedate: output.updatedate,
- updateuserid: output.updateuserid,
- contacts: output.contacts,
- machines: output.machines,
- templates: output.templates,
- workorders: output.workorders
- )
- }
-
- func mapOutput(_ output: Optional<mdlCustomer>.WrappedType, _ input: mdlCustomer.Input){
- output.number = input.number;
- output.name = input.name;
- output.address = input.address;
- output.address2 = input.address2;
- output.city = input.city;
- output.state = input.state;
- output.zip = input.zip;
- output.phone1 = input.phone1;
- output.phone2 = input.phone2;
- output.fax = input.fax;
- output.isactive = input.isactive;
- output.createdate = input.createdate;
- output.updatedate = input.updatedate;
- output.createuserid = input.createuserid;
- output.updateuserid = input.updateuserid;
- output.$contacts.value = input.contacts;
- output.$machines.value = input.machines;
- output.$templates.value = input.templates;
- output.$workorders.value = input.workorders
- }
-
- func createUnit(_ input: mdlCustomer.Input) -> mdlCustomer {
- return mdlCustomer(
- id: input.id ?? nil
- , number: input.number
- , name: input.name
- , address: input.address
- , address2: input.address2
- , city: input.city
- , state: input.state
- , zip: input.zip
- , phone1: input.phone1
- , phone2: input.phone2
- , fax: input.fax
- , isactive: input.isactive
- , createuserid: input.createuserid ?? -1
- , createdate: input.createdate
- , updateuserid: input.updateuserid ?? -1
- , updatedate: input.updatedate
- , contacts: input.contacts
- , machines: input.machines
- , templates: input.templates
- , workorders: input.workorders
- )
- }
-
- //update function
- func save(_ output: Optional<mdlCustomer>.WrappedType, req: Request) -> EventLoopFuture<mdlCustomer.Output> {
- return output
- .save(on: req.db)
- .map{mdlCustomer.Output(
- id: output.id!
- , number: output.number
- , name: output.name
- , address: output.address
- , address2: output.address2
- , city: output.city
- , state: output.state
- , zip: output.zip
- , phone1: output.phone1
- , phone2: output.phone2
- , fax: output.fax
- , isactive: output.isactive
- , createdate: output.createdate
- , createuserid: output.createuserid
- , updatedate: output.updatedate
- , updateuserid: output.updateuserid
- , contacts: output.contacts
- , machines: output.machines
- , templates: output.templates
- , workorders: output.workorders
- )
- }
- }
- //create function
- func save(_ output: mdlCustomer, _ req: Request, _ input: mdlCustomer.Input) -> EventLoopFuture<mdlCustomer.Output> {
- return output
- .save(on: req.db)
- .map{
- mdlCustomer.Output(
- id: output.id!
- , number: output.number
- , name: output.name
- , address: output.address
- , address2: output.address2
- , city: output.city
- , state: output.state
- , zip: output.zip
- , phone1: output.phone1
- , phone2: output.phone2
- , fax: output.fax
- , isactive: output.isactive
- , createdate: output.createdate
- , createuserid: output.createuserid
- , updatedate: output.updatedate
- , updateuserid: output.updateuserid
- , contacts: input.contacts
- , machines: input.machines
- , templates: input.templates
- , workorders: input.workorders
- )
-
- }
- }
-
- }
- extension CustomerCtrl: RouteCollection{
- func boot(routes: RoutesBuilder) throws{
- let apigroup = routes.grouped("customers")
- apigroup.get("", use: readAll)
- apigroup.get(":id", use: read)
- apigroup.post("", use: create)
- apigroup.post(":id", use: update)
- apigroup.delete(":id", use: delete)
-
-
- }
- }
|