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