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
|
||||
}
|
||||
|
||||
|
||||
@@ -1,9 +1,11 @@
|
||||
import Dependencies
|
||||
import Fluent
|
||||
import Vapor
|
||||
|
||||
// TODO: Add update and get by id.
|
||||
struct UserApiController: RouteCollection {
|
||||
let users = UserDB()
|
||||
|
||||
@Dependency(\.users) var users
|
||||
|
||||
func boot(routes: any RoutesBuilder) throws {
|
||||
let unProtected = routes.apiUnprotected(route: "users")
|
||||
@@ -19,7 +21,7 @@ struct UserApiController: RouteCollection {
|
||||
|
||||
@Sendable
|
||||
func index(req: Request) async throws -> [User.DTO] {
|
||||
try await users.fetchAll(on: req.db)
|
||||
try await users.fetchAll()
|
||||
}
|
||||
|
||||
@Sendable
|
||||
@@ -33,13 +35,13 @@ struct UserApiController: RouteCollection {
|
||||
}
|
||||
try User.Create.validate(content: req)
|
||||
let model = try req.content.decode(User.Create.self)
|
||||
return try await users.create(model, on: req.db)
|
||||
return try await users.create(model)
|
||||
}
|
||||
|
||||
@Sendable
|
||||
func login(req: Request) async throws -> UserToken {
|
||||
let user = try req.auth.require(User.self)
|
||||
return try await users.login(user: user, on: req.db)
|
||||
return try await users.login(user)
|
||||
}
|
||||
|
||||
// @Sendable
|
||||
@@ -53,7 +55,7 @@ struct UserApiController: RouteCollection {
|
||||
guard let id = req.parameters.get("id", as: User.IDValue.self) else {
|
||||
throw Abort(.badRequest, reason: "User id not provided")
|
||||
}
|
||||
try await users.delete(id: id, on: req.db)
|
||||
try await users.delete(id)
|
||||
return .ok
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,8 +1,10 @@
|
||||
import Dependencies
|
||||
import Fluent
|
||||
import Vapor
|
||||
|
||||
struct VendorApiController: RouteCollection {
|
||||
private let vendors = VendorDB()
|
||||
|
||||
@Dependency(\.vendors) var vendors
|
||||
|
||||
func boot(routes: any RoutesBuilder) throws {
|
||||
let protected = routes.apiProtected(route: "vendors")
|
||||
@@ -17,14 +19,14 @@ struct VendorApiController: RouteCollection {
|
||||
@Sendable
|
||||
func index(req: Request) async throws -> [Vendor.DTO] {
|
||||
let params = try req.query.decode(VendorsIndexQuery.self)
|
||||
return try await vendors.fetchAll(withBranches: params.branches, on: req.db)
|
||||
return try await vendors.fetchAll(params.fetchRequest)
|
||||
}
|
||||
|
||||
@Sendable
|
||||
func create(req: Request) async throws -> Vendor.DTO {
|
||||
try Vendor.Create.validate(content: req)
|
||||
let model = try req.content.decode(Vendor.Create.self)
|
||||
return try await vendors.create(model, on: req.db)
|
||||
return try await vendors.create(model)
|
||||
}
|
||||
|
||||
@Sendable
|
||||
@@ -34,7 +36,7 @@ struct VendorApiController: RouteCollection {
|
||||
}
|
||||
try Vendor.Update.validate(content: req)
|
||||
let updates = try req.content.decode(Vendor.Update.self)
|
||||
return try await vendors.update(id: id, with: updates, on: req.db)
|
||||
return try await vendors.update(id, updates)
|
||||
}
|
||||
|
||||
@Sendable
|
||||
@@ -42,11 +44,16 @@ struct VendorApiController: RouteCollection {
|
||||
guard let id = req.parameters.get("id", as: Vendor.IDValue.self) else {
|
||||
throw Abort(.badRequest, reason: "Vendor id not provided.")
|
||||
}
|
||||
try await vendors.delete(id: id, on: req.db)
|
||||
try await vendors.delete(id)
|
||||
return .ok
|
||||
}
|
||||
}
|
||||
|
||||
struct VendorsIndexQuery: Content {
|
||||
let branches: Bool?
|
||||
|
||||
var fetchRequest: VendorDB.FetchRequest {
|
||||
if branches == true { return .withBranches }
|
||||
return .default
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user