|
- //
- // Location.swift
- //
- //
- // Created by Michiel Carman on 11/3/21.
- //
-
- import Foundation
- import Vapor
- import Fluent
- import FluentPostgresDriver
-
- final class LocationCtrl{
- //Create Unit
- func create(req: Request) throws -> EventLoopFuture<mdlLocation.Output>{
- let input = try req.content.decode(mdlLocation.Input.self)
- let output = self.createUnit(input)
- return self.save(output, req, input)
-
- }
-
- //Update Unit
- func update(req: Request) throws -> EventLoopFuture<mdlLocation.Output>{
- let input = try req.content.decode(mdlLocation.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<mdlLocation.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<mdlLocation.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 mdlLocation.find(id, on: req.db).unwrap(or: Abort(.notFound)).flatMap{ $0.delete(on: req.db)}.map{.ok}
- }
-
- }
- extension LocationCtrl{
- //Query Settings
- func qbase(req: Request) throws -> QueryBuilder<mdlLocation> {
- let query = mdlLocation.query(on: req.db)
- .with(\.$unit)
- .with(\.$labsetting)
- .with(\.$user)
- .with(\.$contacts)
- .with(\.$gates)
- if let id = req.parameters.get("id", as: Int.self){
- return query.filter(\.$id == id)
- } else{
- return query
- }
- }
- //create Output
- func createRead(_ output: mdlLocation) -> mdlLocation.Output {
- return mdlLocation.Output(
- id: output.id!,
- name: output.name,
- address: output.address,
- address2: output.address2,
- city: output.city,
- state: output.state,
- zip: output.zip,
- dealercode: output.dealercode,
- logofile: output.logofile,
- isactive: output.isactive,
- createdate: output.createdate,
- createuserid: output.createuserid,
- updatedate: output.updatedate,
- updateuserid: output.updateuserid,
- unit: output.$unit.value!,
- labsetting: output.$labsetting.value!,
- user: output.$user.value!,
- contacts: output.$contacts.value!,
- gates: output.$gates.value!
- )
- }
-
- func mapOutput(_ output: Optional<mdlLocation>.WrappedType, _ input: mdlLocation.Input){
- 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.dealercode = input.dealercode;
- output.logofile = input.logofile;
- output.isactive = input.isactive
- output.createdate = input.createdate;
- output.updatedate = input.updatedate;
- output.createuserid = input.createuserid;
- output.updateuserid = input.updateuserid;
- output.$unit.id = input.unit.id!;
- output.$labsetting.id = input.id!;
- output.$user.value = input.user;
- output.$contacts.value = input.contacts
- output.$gates.value = input.gates
- }
-
- func createUnit(_ input: mdlLocation.Input) -> mdlLocation {
- return mdlLocation(
- id: input.id ?? nil
- , name: input.name
- , address: input.address
- , address2: input.address2
- , city: input.city
- , state: input.state
- , zip: input.zip
- , dealercode: input.dealercode
- , logofile: input.logofile
- , isactive: input.isactive
- , createuserid: input.createuserid ?? -1
- , createdate: input.createdate
- , updateuserid: input.updateuserid ?? -1
- , updatedate: input.updatedate
- , unit: input.unit
- , labsetting: input.labsetting
- , user: input.user
- , contacts: input.contacts
- , gates: input.gates
- )
- }
-
- //update function
- func save(_ output: Optional<mdlLocation>.WrappedType, req: Request) -> EventLoopFuture<mdlLocation.Output> {
- return output
- .save(on: req.db)
- .map{mdlLocation.Output(
- id: output.id!
- , name: output.name
- , address: output.address
- , address2: output.address2
- , city: output.city
- , state: output.state
- , zip: output.zip
- , dealercode: output.dealercode
- , logofile: output.logofile
- , isactive: output.isactive
- , createdate: output.createdate
- , createuserid: output.createuserid
- , updatedate: output.updatedate
- , updateuserid: output.updateuserid
- , unit: output.unit
- , labsetting: output.labsetting
- , user: output.user
- , contacts: output.contacts
- , gates: output.gates
- )
- }
- }
- //create function
- func save(_ output: mdlLocation, _ req: Request, _ input: mdlLocation.Input) -> EventLoopFuture<mdlLocation.Output> {
- return output
- .save(on: req.db)
- .map{
- mdlLocation.Output(
- id: output.id!
- , name: output.name
- , address: output.address
- , address2: output.address2
- , city: output.city
- , state: output.state
- , zip: output.zip
- , dealercode: output.dealercode
- , logofile: output.logofile
- , isactive: output.isactive
- , createdate: output.createdate
- , createuserid: output.createuserid
- , updatedate: output.updatedate
- , updateuserid: output.updateuserid
- , unit: output.unit
- , labsetting: input.labsetting
- , user: input.user
- , contacts: input.contacts
- , gates: input.gates
- )
-
- }
- }
- }
- extension LocationCtrl: RouteCollection{
- func boot(routes: RoutesBuilder) throws{
- let apigroup = routes.grouped("locations")
- apigroup.get("", use: readAll)
- apigroup.get(":id", use: read)
- apigroup.post("", use: create)
- apigroup.post(":id", use: update)
- apigroup.delete(":id", use: delete)
-
-
- }
- }
|