feat: Begins breaking out database interfaces and api controllers into seperate items.
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
import Fluent
|
||||
import Vapor
|
||||
|
||||
// TODO: Use DB controllers.
|
||||
struct ApiController: RouteCollection {
|
||||
func boot(routes: RoutesBuilder) throws {
|
||||
let api = routes.grouped("api", "v1")
|
||||
@@ -29,6 +30,9 @@ struct ApiController: RouteCollection {
|
||||
|
||||
purchaseOrders.get(use: purchaseOrdersIndex(req:))
|
||||
purchaseOrders.post(use: createPurchaseOrder(req:))
|
||||
purchaseOrders.group(":purchaseOrderID") {
|
||||
$0.get(use: getPurchaseOrder(req:))
|
||||
}
|
||||
|
||||
users.get(use: usersIndex(req:))
|
||||
api.post("users", use: createUser(req:))
|
||||
@@ -102,8 +106,6 @@ struct ApiController: RouteCollection {
|
||||
|
||||
// MARK: - PurchaseOrders
|
||||
|
||||
// TODO: Add fetch by id.
|
||||
|
||||
// TODO: Add pagination and filters.
|
||||
@Sendable
|
||||
func purchaseOrdersIndex(req: Request) async throws -> [PurchaseOrder.DTO] {
|
||||
@@ -118,6 +120,27 @@ struct ApiController: RouteCollection {
|
||||
.map { $0.toDTO() }
|
||||
}
|
||||
|
||||
@Sendable
|
||||
func getPurchaseOrder(req: Request) async throws -> PurchaseOrder.DTO {
|
||||
guard let id = req.parameters.get("purchaseOrderID", as: Int.self) else {
|
||||
throw Abort(.badRequest)
|
||||
}
|
||||
|
||||
guard let purchaseOrder = try await PurchaseOrder.query(on: req.db)
|
||||
.filter(\.$id == id)
|
||||
.with(\.$createdBy)
|
||||
.with(\.$createdFor)
|
||||
.with(\.$vendorBranch, {
|
||||
$0.with(\.$vendor)
|
||||
})
|
||||
.first()
|
||||
else {
|
||||
throw Abort(.notFound)
|
||||
}
|
||||
|
||||
return purchaseOrder.toDTO()
|
||||
}
|
||||
|
||||
@Sendable
|
||||
func createPurchaseOrder(req: Request) async throws -> PurchaseOrder.DTO {
|
||||
try PurchaseOrder.Create.validate(content: req)
|
||||
@@ -287,11 +310,3 @@ struct ApiController: RouteCollection {
|
||||
return branch.toDTO()
|
||||
}
|
||||
}
|
||||
|
||||
struct VendorsIndexQuery: Content {
|
||||
let branches: Bool?
|
||||
}
|
||||
|
||||
struct EmployeesIndexQuery: Content {
|
||||
let active: Bool?
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user