feat: Begins integrating database client into vapor app.
This commit is contained in:
@@ -1,64 +1,64 @@
|
||||
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 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?
|
||||
// }
|
||||
|
||||
Reference in New Issue
Block a user