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