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