feat: Implements common database interactions as dependencies.

This commit is contained in:
2025-01-10 22:37:59 -05:00
parent 6f206bbd82
commit 69351d0a0b
11 changed files with 325 additions and 207 deletions

View File

@@ -1,8 +1,10 @@
import Dependencies
import Fluent
import Vapor
struct VendorBranchApiController: RouteCollection {
private let vendorBranches = VendorBranchDB()
@Dependency(\.vendorBranches) var vendorBranches
func boot(routes: any RoutesBuilder) throws {
let prefix = routes.apiProtected(route: "vendors")
@@ -21,7 +23,7 @@ struct VendorBranchApiController: RouteCollection {
@Sendable
func index(req: Request) async throws -> [VendorBranch.DTO] {
try await vendorBranches.fetchAll(on: req.db)
try await vendorBranches.fetchAll()
}
@Sendable
@@ -29,7 +31,7 @@ struct VendorBranchApiController: RouteCollection {
guard let id = req.parameters.get("vendorID", as: Vendor.IDValue.self) else {
throw Abort(.badRequest, reason: "Vendor id not provided.")
}
return try await vendorBranches.fetch(for: id, on: req.db)
return try await vendorBranches.fetchForVendor(id)
}
@Sendable
@@ -39,7 +41,7 @@ struct VendorBranchApiController: RouteCollection {
}
try VendorBranch.Create.validate(content: req)
let model = try req.content.decode(VendorBranch.Create.self)
return try await vendorBranches.create(model, for: id, on: req.db)
return try await vendorBranches.create(model, id)
}
@Sendable
@@ -49,7 +51,7 @@ struct VendorBranchApiController: RouteCollection {
}
try VendorBranch.Update.validate(content: req)
let updates = try req.content.decode(VendorBranch.Update.self)
return try await vendorBranches.update(id: id, with: updates, on: req.db)
return try await vendorBranches.update(id, updates)
}
@Sendable
@@ -57,7 +59,7 @@ struct VendorBranchApiController: RouteCollection {
guard let id = req.parameters.get("id", as: VendorBranch.IDValue.self) else {
throw Abort(.badRequest, reason: "Vendor branch id not provided.")
}
try await vendorBranches.delete(id: id, on: req.db)
try await vendorBranches.delete(id)
return .ok
}