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