feat: Moves database dependency directory.
This commit is contained in:
@@ -25,9 +25,9 @@ struct EmployeeApiController: RouteCollection {
|
|||||||
|
|
||||||
@Sendable
|
@Sendable
|
||||||
func create(req: Request) async throws -> Employee.DTO {
|
func create(req: Request) async throws -> Employee.DTO {
|
||||||
try Employee.Create.validate(content: req)
|
try await employees.create(
|
||||||
let create = try req.content.decode(Employee.Create.self)
|
req.ensureValidContent(Employee.Create.self)
|
||||||
return try await employees.create(create)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
@Sendable
|
@Sendable
|
||||||
@@ -42,11 +42,10 @@ struct EmployeeApiController: RouteCollection {
|
|||||||
|
|
||||||
@Sendable
|
@Sendable
|
||||||
func update(req: Request) async throws -> Employee.DTO {
|
func update(req: Request) async throws -> Employee.DTO {
|
||||||
try Employee.Update.validate(content: req)
|
|
||||||
guard let employeeID = req.parameters.get("employeeID", as: Employee.IDValue.self) else {
|
guard let employeeID = req.parameters.get("employeeID", as: Employee.IDValue.self) else {
|
||||||
throw Abort(.badRequest, reason: "Employee id value not provided")
|
throw Abort(.badRequest, reason: "Employee id value not provided")
|
||||||
}
|
}
|
||||||
let updates = try req.content.decode(Employee.Update.self)
|
let updates = try req.ensureValidContent(Employee.Update.self)
|
||||||
return try await employees.update(employeeID, updates)
|
return try await employees.update(employeeID, updates)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -25,10 +25,8 @@ struct PurchaseOrderApiController: RouteCollection {
|
|||||||
|
|
||||||
@Sendable
|
@Sendable
|
||||||
func create(req: Request) async throws -> PurchaseOrder.DTO {
|
func create(req: Request) async throws -> PurchaseOrder.DTO {
|
||||||
try PurchaseOrder.Create.validate(content: req)
|
try await purchaseOrders.create(
|
||||||
let model = try req.content.decode(PurchaseOrder.Create.self)
|
req.ensureValidContent(PurchaseOrder.Create.self),
|
||||||
return try await purchaseOrders.create(
|
|
||||||
model,
|
|
||||||
req.auth.require(User.self).requireID()
|
req.auth.require(User.self).requireID()
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -33,9 +33,7 @@ struct UserApiController: RouteCollection {
|
|||||||
throw Abort(.unauthorized)
|
throw Abort(.unauthorized)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
try User.Create.validate(content: req)
|
return try await users.create(req.ensureValidContent(User.Create.self))
|
||||||
let model = try req.content.decode(User.Create.self)
|
|
||||||
return try await users.create(model)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Sendable
|
@Sendable
|
||||||
|
|||||||
@@ -24,9 +24,7 @@ struct VendorApiController: RouteCollection {
|
|||||||
|
|
||||||
@Sendable
|
@Sendable
|
||||||
func create(req: Request) async throws -> Vendor.DTO {
|
func create(req: Request) async throws -> Vendor.DTO {
|
||||||
try Vendor.Create.validate(content: req)
|
try await vendors.create(req.ensureValidContent(Vendor.Create.self))
|
||||||
let model = try req.content.decode(Vendor.Create.self)
|
|
||||||
return try await vendors.create(model)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Sendable
|
@Sendable
|
||||||
|
|||||||
@@ -39,9 +39,10 @@ struct VendorBranchApiController: RouteCollection {
|
|||||||
guard let id = req.parameters.get("vendorID", as: Vendor.IDValue.self) else {
|
guard let id = req.parameters.get("vendorID", as: Vendor.IDValue.self) else {
|
||||||
throw Abort(.badRequest, reason: "Vendor id not provided.")
|
throw Abort(.badRequest, reason: "Vendor id not provided.")
|
||||||
}
|
}
|
||||||
try VendorBranch.Create.validate(content: req)
|
return try await vendorBranches.create(
|
||||||
let model = try req.content.decode(VendorBranch.Create.self)
|
req.ensureValidContent(VendorBranch.Create.self),
|
||||||
return try await vendorBranches.create(model, id)
|
id
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
@Sendable
|
@Sendable
|
||||||
|
|||||||
8
Sources/App/Extensions/Request+ensureValidContent.swift
Normal file
8
Sources/App/Extensions/Request+ensureValidContent.swift
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
import Vapor
|
||||||
|
|
||||||
|
extension Request {
|
||||||
|
func ensureValidContent<T>(_ decoding: T.Type) throws -> T where T: Content, T: Validatable {
|
||||||
|
try T.validate(content: self)
|
||||||
|
return try content.decode(T.self)
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user