feat: Begins breaking out database interfaces and api controllers into seperate items.
This commit is contained in:
64
Sources/App/Controllers/Api/VendorBranchApiController.swift
Normal file
64
Sources/App/Controllers/Api/VendorBranchApiController.swift
Normal file
@@ -0,0 +1,64 @@
|
||||
import Fluent
|
||||
import Vapor
|
||||
|
||||
struct VendorBranchApiController: RouteCollection {
|
||||
private let vendorBranches = VendorBranchDB()
|
||||
|
||||
func boot(routes: any RoutesBuilder) throws {
|
||||
let prefix = routes.apiProtected(route: "vendors")
|
||||
let root = prefix.grouped("branches")
|
||||
root.get(use: index(req:))
|
||||
root.group(":id") {
|
||||
$0.put(use: update(req:))
|
||||
$0.delete(use: delete(req:))
|
||||
}
|
||||
|
||||
prefix.group(":vendorID", "branches") {
|
||||
$0.get(use: indexForVendor(req:))
|
||||
$0.post(use: create(req:))
|
||||
}
|
||||
}
|
||||
|
||||
@Sendable
|
||||
func index(req: Request) async throws -> [VendorBranch.DTO] {
|
||||
try await vendorBranches.fetchAll(on: req.db)
|
||||
}
|
||||
|
||||
@Sendable
|
||||
func indexForVendor(req: Request) async throws -> [VendorBranch.DTO] {
|
||||
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)
|
||||
}
|
||||
|
||||
@Sendable
|
||||
func create(req: Request) async throws -> VendorBranch.DTO {
|
||||
guard let id = req.parameters.get("vendorID", as: Vendor.IDValue.self) else {
|
||||
throw Abort(.badRequest, reason: "Vendor id not provided.")
|
||||
}
|
||||
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)
|
||||
}
|
||||
|
||||
@Sendable
|
||||
func update(req: Request) async throws -> VendorBranch.DTO {
|
||||
guard let id = req.parameters.get("id", as: VendorBranch.IDValue.self) else {
|
||||
throw Abort(.badRequest, reason: "Vendor branch id not provided.")
|
||||
}
|
||||
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)
|
||||
}
|
||||
|
||||
@Sendable
|
||||
func delete(req: Request) async throws -> HTTPStatus {
|
||||
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)
|
||||
return .ok
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user