import Fluent import Vapor struct EmployeeApiController: RouteCollection { private let employeeDB = EmployeeDB() func boot(routes: any RoutesBuilder) throws { let protected = routes.apiProtected(route: "employees") protected.get(use: index(req:)) protected.post(use: create(req:)) protected.group(":employeeID") { $0.get(use: get(req:)) $0.put(use: update(req:)) $0.delete(use: delete(req:)) } } @Sendable func index(req: Request) async throws -> [Employee.DTO] { let params = try req.query.decode(EmployeesIndexQuery.self) return try await employeeDB.fetchAll(active: params.active, on: req.db) } @Sendable func create(req: Request) async throws -> Employee.DTO { try Employee.Create.validate(content: req) let create = try req.content.decode(Employee.Create.self) return try await employeeDB.create(create, on: req.db) } @Sendable func get(req: Request) async throws -> Employee.DTO { guard let id = req.parameters.get("employeeID", as: Employee.IDValue.self), let employee = try await employeeDB.get(id: id, on: req.db) else { throw Abort(.notFound) } return employee } @Sendable func update(req: Request) async throws -> Employee.DTO { try Employee.Update.validate(content: req) guard let employeeID = req.parameters.get("employeeID", as: Employee.IDValue.self) else { throw Abort(.badRequest, reason: "Employee id value not provided") } let updates = try req.content.decode(Employee.Update.self) return try await employeeDB.update(id: employeeID, with: updates, on: req.db) } @Sendable func delete(req: Request) async throws -> HTTPStatus { guard let employeeID = req.parameters.get("employeeID", as: Employee.IDValue.self) else { throw Abort(.badRequest, reason: "Employee id value not provided") } try await employeeDB.delete(id: employeeID, on: req.db) return .ok } } struct EmployeesIndexQuery: Content { let active: Bool? }