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