feat: Implements common database interactions as dependencies.
This commit is contained in:
@@ -1,10 +1,12 @@
|
||||
import Dependencies
|
||||
import Fluent
|
||||
import Vapor
|
||||
|
||||
// TODO: Add update route.
|
||||
|
||||
struct PurchaseOrderApiController: RouteCollection {
|
||||
private let purchaseOrders = PurchaseOrderDB()
|
||||
|
||||
@Dependency(\.purchaseOrders) var purchaseOrders
|
||||
|
||||
func boot(routes: any RoutesBuilder) throws {
|
||||
let protected = routes.apiProtected(route: "purchase-orders")
|
||||
@@ -18,7 +20,7 @@ struct PurchaseOrderApiController: RouteCollection {
|
||||
|
||||
@Sendable
|
||||
func index(req: Request) async throws -> [PurchaseOrder.DTO] {
|
||||
try await purchaseOrders.fetchAll(on: req.db)
|
||||
try await purchaseOrders.fetchAll()
|
||||
}
|
||||
|
||||
@Sendable
|
||||
@@ -27,15 +29,14 @@ struct PurchaseOrderApiController: RouteCollection {
|
||||
let model = try req.content.decode(PurchaseOrder.Create.self)
|
||||
return try await purchaseOrders.create(
|
||||
model,
|
||||
createdById: req.auth.require(User.self).requireID(),
|
||||
on: req.db
|
||||
req.auth.require(User.self).requireID()
|
||||
)
|
||||
}
|
||||
|
||||
@Sendable
|
||||
func get(req: Request) async throws -> PurchaseOrder.DTO {
|
||||
guard let id = req.parameters.get("id", as: PurchaseOrder.IDValue.self),
|
||||
let purchaseOrder = try await purchaseOrders.get(id: id, on: req.db)
|
||||
let purchaseOrder = try await purchaseOrders.get(id)
|
||||
else {
|
||||
throw Abort(.notFound)
|
||||
}
|
||||
@@ -47,7 +48,7 @@ struct PurchaseOrderApiController: RouteCollection {
|
||||
guard let id = req.parameters.get("id", as: PurchaseOrder.IDValue.self) else {
|
||||
throw Abort(.badRequest, reason: "Purchase order id not provided.")
|
||||
}
|
||||
try await purchaseOrders.delete(id: id, on: req.db)
|
||||
try await purchaseOrders.delete(id)
|
||||
return .ok
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user