feat: Updates api controllers to use database client.
This commit is contained in:
@@ -1,64 +1,70 @@
|
||||
// import Dependencies
|
||||
// import Fluent
|
||||
// import Vapor
|
||||
//
|
||||
// struct EmployeeApiController: RouteCollection {
|
||||
//
|
||||
// @Dependency(\.employees) var employees
|
||||
//
|
||||
// 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 employees.fetchAll(params.active == true ? .active : .default)
|
||||
// }
|
||||
//
|
||||
// @Sendable
|
||||
// func create(req: Request) async throws -> Employee.DTO {
|
||||
// try await employees.create(
|
||||
// req.ensureValidContent(Employee.Create.self)
|
||||
// )
|
||||
// }
|
||||
//
|
||||
// @Sendable
|
||||
// func get(req: Request) async throws -> Employee.DTO {
|
||||
// guard let id = req.parameters.get("employeeID", as: Employee.IDValue.self),
|
||||
// let employee = try await employees.get(id)
|
||||
// else {
|
||||
// throw Abort(.notFound)
|
||||
// }
|
||||
// return employee
|
||||
// }
|
||||
//
|
||||
// @Sendable
|
||||
// func update(req: Request) async throws -> Employee.DTO {
|
||||
// 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.ensureValidContent(Employee.Update.self)
|
||||
// return try await employees.update(employeeID, updates)
|
||||
// }
|
||||
//
|
||||
// @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 employees.delete(employeeID)
|
||||
// return .ok
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// struct EmployeesIndexQuery: Content {
|
||||
// let active: Bool?
|
||||
// }
|
||||
import DatabaseClient
|
||||
import Dependencies
|
||||
import SharedModels
|
||||
import Vapor
|
||||
|
||||
struct EmployeeApiController: RouteCollection {
|
||||
|
||||
@Dependency(\.database.employees) var employees
|
||||
|
||||
func boot(routes: any RoutesBuilder) throws {
|
||||
let protected = routes.apiProtected(route: "employees")
|
||||
protected.get(use: index(req:))
|
||||
protected.post(use: create(req:))
|
||||
protected.group(":id") {
|
||||
$0.get(use: get(req:))
|
||||
$0.put(use: update(req:))
|
||||
$0.delete(use: delete(req:))
|
||||
}
|
||||
}
|
||||
|
||||
@Sendable
|
||||
func index(req: Request) async throws -> [Employee] {
|
||||
let params = try req.query.decode(EmployeesIndexQuery.self)
|
||||
return try await employees.fetchAll(params.request)
|
||||
}
|
||||
|
||||
@Sendable
|
||||
func create(req: Request) async throws -> Employee {
|
||||
try await employees.create(
|
||||
req.content.decode(Employee.Create.self)
|
||||
)
|
||||
}
|
||||
|
||||
@Sendable
|
||||
func get(req: Request) async throws -> Employee {
|
||||
guard let employee = try await employees.get(req.ensureIDPathComponent()) else {
|
||||
throw Abort(.notFound)
|
||||
}
|
||||
return employee
|
||||
}
|
||||
|
||||
@Sendable
|
||||
func update(req: Request) async throws -> Employee {
|
||||
return try await employees.update(
|
||||
req.ensureIDPathComponent(),
|
||||
req.content.decode(Employee.Update.self)
|
||||
)
|
||||
}
|
||||
|
||||
@Sendable
|
||||
func delete(req: Request) async throws -> HTTPStatus {
|
||||
try await employees.delete(req.ensureIDPathComponent())
|
||||
return .ok
|
||||
}
|
||||
}
|
||||
|
||||
struct EmployeesIndexQuery: Content {
|
||||
let active: Bool?
|
||||
|
||||
var request: DatabaseClient.Employees.FetchRequest {
|
||||
switch active {
|
||||
case .none:
|
||||
return .all
|
||||
case .some(true):
|
||||
return .active
|
||||
case .some(false):
|
||||
return .inactive
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user