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