feat: Begins breaking out database interfaces and api controllers into seperate items.
This commit is contained in:
62
Sources/App/Controllers/Api/PurchaseOrderApiController.swift
Normal file
62
Sources/App/Controllers/Api/PurchaseOrderApiController.swift
Normal file
@@ -0,0 +1,62 @@
|
||||
import Fluent
|
||||
import Vapor
|
||||
|
||||
// TODO: Add update route.
|
||||
|
||||
struct PurchaseOrderApiController: RouteCollection {
|
||||
private let purchaseOrders = PurchaseOrderDB()
|
||||
|
||||
func boot(routes: any RoutesBuilder) throws {
|
||||
let protected = routes.apiProtected(route: "purchase-orders")
|
||||
protected.get(use: index(req:))
|
||||
protected.post(use: create(req:))
|
||||
protected.group(":id") {
|
||||
$0.get(use: get(req:))
|
||||
$0.delete(use: delete(req:))
|
||||
}
|
||||
}
|
||||
|
||||
@Sendable
|
||||
func index(req: Request) async throws -> [PurchaseOrder.DTO] {
|
||||
try await purchaseOrders.fetchAll(on: req.db)
|
||||
}
|
||||
|
||||
@Sendable
|
||||
func create(req: Request) async throws -> PurchaseOrder.DTO {
|
||||
try PurchaseOrder.Create.validate(content: req)
|
||||
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
|
||||
)
|
||||
}
|
||||
|
||||
@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)
|
||||
else {
|
||||
throw Abort(.notFound)
|
||||
}
|
||||
return purchaseOrder
|
||||
}
|
||||
|
||||
@Sendable
|
||||
func delete(req: Request) async throws -> HTTPStatus {
|
||||
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)
|
||||
return .ok
|
||||
}
|
||||
|
||||
// @Sendable
|
||||
// func update(req: Request) async throws -> PurchaseOrder.DTO {
|
||||
// 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)
|
||||
// return .ok
|
||||
// }
|
||||
}
|
||||
Reference in New Issue
Block a user