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