feat: Implements common database interactions as dependencies.
This commit is contained in:
@@ -1,10 +1,12 @@
|
|||||||
|
import Dependencies
|
||||||
import Fluent
|
import Fluent
|
||||||
import Vapor
|
import Vapor
|
||||||
|
|
||||||
// TODO: Add update route.
|
// TODO: Add update route.
|
||||||
|
|
||||||
struct PurchaseOrderApiController: RouteCollection {
|
struct PurchaseOrderApiController: RouteCollection {
|
||||||
private let purchaseOrders = PurchaseOrderDB()
|
|
||||||
|
@Dependency(\.purchaseOrders) var purchaseOrders
|
||||||
|
|
||||||
func boot(routes: any RoutesBuilder) throws {
|
func boot(routes: any RoutesBuilder) throws {
|
||||||
let protected = routes.apiProtected(route: "purchase-orders")
|
let protected = routes.apiProtected(route: "purchase-orders")
|
||||||
@@ -18,7 +20,7 @@ struct PurchaseOrderApiController: RouteCollection {
|
|||||||
|
|
||||||
@Sendable
|
@Sendable
|
||||||
func index(req: Request) async throws -> [PurchaseOrder.DTO] {
|
func index(req: Request) async throws -> [PurchaseOrder.DTO] {
|
||||||
try await purchaseOrders.fetchAll(on: req.db)
|
try await purchaseOrders.fetchAll()
|
||||||
}
|
}
|
||||||
|
|
||||||
@Sendable
|
@Sendable
|
||||||
@@ -27,15 +29,14 @@ struct PurchaseOrderApiController: RouteCollection {
|
|||||||
let model = try req.content.decode(PurchaseOrder.Create.self)
|
let model = try req.content.decode(PurchaseOrder.Create.self)
|
||||||
return try await purchaseOrders.create(
|
return try await purchaseOrders.create(
|
||||||
model,
|
model,
|
||||||
createdById: req.auth.require(User.self).requireID(),
|
req.auth.require(User.self).requireID()
|
||||||
on: req.db
|
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
@Sendable
|
@Sendable
|
||||||
func get(req: Request) async throws -> PurchaseOrder.DTO {
|
func get(req: Request) async throws -> PurchaseOrder.DTO {
|
||||||
guard let id = req.parameters.get("id", as: PurchaseOrder.IDValue.self),
|
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 {
|
else {
|
||||||
throw Abort(.notFound)
|
throw Abort(.notFound)
|
||||||
}
|
}
|
||||||
@@ -47,7 +48,7 @@ struct PurchaseOrderApiController: RouteCollection {
|
|||||||
guard let id = req.parameters.get("id", as: PurchaseOrder.IDValue.self) else {
|
guard let id = req.parameters.get("id", as: PurchaseOrder.IDValue.self) else {
|
||||||
throw Abort(.badRequest, reason: "Purchase order id not provided.")
|
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
|
return .ok
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,9 +1,11 @@
|
|||||||
|
import Dependencies
|
||||||
import Fluent
|
import Fluent
|
||||||
import Vapor
|
import Vapor
|
||||||
|
|
||||||
// TODO: Add update and get by id.
|
// TODO: Add update and get by id.
|
||||||
struct UserApiController: RouteCollection {
|
struct UserApiController: RouteCollection {
|
||||||
let users = UserDB()
|
|
||||||
|
@Dependency(\.users) var users
|
||||||
|
|
||||||
func boot(routes: any RoutesBuilder) throws {
|
func boot(routes: any RoutesBuilder) throws {
|
||||||
let unProtected = routes.apiUnprotected(route: "users")
|
let unProtected = routes.apiUnprotected(route: "users")
|
||||||
@@ -19,7 +21,7 @@ struct UserApiController: RouteCollection {
|
|||||||
|
|
||||||
@Sendable
|
@Sendable
|
||||||
func index(req: Request) async throws -> [User.DTO] {
|
func index(req: Request) async throws -> [User.DTO] {
|
||||||
try await users.fetchAll(on: req.db)
|
try await users.fetchAll()
|
||||||
}
|
}
|
||||||
|
|
||||||
@Sendable
|
@Sendable
|
||||||
@@ -33,13 +35,13 @@ struct UserApiController: RouteCollection {
|
|||||||
}
|
}
|
||||||
try User.Create.validate(content: req)
|
try User.Create.validate(content: req)
|
||||||
let model = try req.content.decode(User.Create.self)
|
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
|
@Sendable
|
||||||
func login(req: Request) async throws -> UserToken {
|
func login(req: Request) async throws -> UserToken {
|
||||||
let user = try req.auth.require(User.self)
|
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
|
// @Sendable
|
||||||
@@ -53,7 +55,7 @@ struct UserApiController: RouteCollection {
|
|||||||
guard let id = req.parameters.get("id", as: User.IDValue.self) else {
|
guard let id = req.parameters.get("id", as: User.IDValue.self) else {
|
||||||
throw Abort(.badRequest, reason: "User id not provided")
|
throw Abort(.badRequest, reason: "User id not provided")
|
||||||
}
|
}
|
||||||
try await users.delete(id: id, on: req.db)
|
try await users.delete(id)
|
||||||
return .ok
|
return .ok
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,8 +1,10 @@
|
|||||||
|
import Dependencies
|
||||||
import Fluent
|
import Fluent
|
||||||
import Vapor
|
import Vapor
|
||||||
|
|
||||||
struct VendorApiController: RouteCollection {
|
struct VendorApiController: RouteCollection {
|
||||||
private let vendors = VendorDB()
|
|
||||||
|
@Dependency(\.vendors) var vendors
|
||||||
|
|
||||||
func boot(routes: any RoutesBuilder) throws {
|
func boot(routes: any RoutesBuilder) throws {
|
||||||
let protected = routes.apiProtected(route: "vendors")
|
let protected = routes.apiProtected(route: "vendors")
|
||||||
@@ -17,14 +19,14 @@ struct VendorApiController: RouteCollection {
|
|||||||
@Sendable
|
@Sendable
|
||||||
func index(req: Request) async throws -> [Vendor.DTO] {
|
func index(req: Request) async throws -> [Vendor.DTO] {
|
||||||
let params = try req.query.decode(VendorsIndexQuery.self)
|
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
|
@Sendable
|
||||||
func create(req: Request) async throws -> Vendor.DTO {
|
func create(req: Request) async throws -> Vendor.DTO {
|
||||||
try Vendor.Create.validate(content: req)
|
try Vendor.Create.validate(content: req)
|
||||||
let model = try req.content.decode(Vendor.Create.self)
|
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
|
@Sendable
|
||||||
@@ -34,7 +36,7 @@ struct VendorApiController: RouteCollection {
|
|||||||
}
|
}
|
||||||
try Vendor.Update.validate(content: req)
|
try Vendor.Update.validate(content: req)
|
||||||
let updates = try req.content.decode(Vendor.Update.self)
|
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
|
@Sendable
|
||||||
@@ -42,11 +44,16 @@ struct VendorApiController: RouteCollection {
|
|||||||
guard let id = req.parameters.get("id", as: Vendor.IDValue.self) else {
|
guard let id = req.parameters.get("id", as: Vendor.IDValue.self) else {
|
||||||
throw Abort(.badRequest, reason: "Vendor id not provided.")
|
throw Abort(.badRequest, reason: "Vendor id not provided.")
|
||||||
}
|
}
|
||||||
try await vendors.delete(id: id, on: req.db)
|
try await vendors.delete(id)
|
||||||
return .ok
|
return .ok
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
struct VendorsIndexQuery: Content {
|
struct VendorsIndexQuery: Content {
|
||||||
let branches: Bool?
|
let branches: Bool?
|
||||||
|
|
||||||
|
var fetchRequest: VendorDB.FetchRequest {
|
||||||
|
if branches == true { return .withBranches }
|
||||||
|
return .default
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,8 +1,10 @@
|
|||||||
|
import Dependencies
|
||||||
import Fluent
|
import Fluent
|
||||||
import Vapor
|
import Vapor
|
||||||
|
|
||||||
struct VendorBranchApiController: RouteCollection {
|
struct VendorBranchApiController: RouteCollection {
|
||||||
private let vendorBranches = VendorBranchDB()
|
|
||||||
|
@Dependency(\.vendorBranches) var vendorBranches
|
||||||
|
|
||||||
func boot(routes: any RoutesBuilder) throws {
|
func boot(routes: any RoutesBuilder) throws {
|
||||||
let prefix = routes.apiProtected(route: "vendors")
|
let prefix = routes.apiProtected(route: "vendors")
|
||||||
@@ -21,7 +23,7 @@ struct VendorBranchApiController: RouteCollection {
|
|||||||
|
|
||||||
@Sendable
|
@Sendable
|
||||||
func index(req: Request) async throws -> [VendorBranch.DTO] {
|
func index(req: Request) async throws -> [VendorBranch.DTO] {
|
||||||
try await vendorBranches.fetchAll(on: req.db)
|
try await vendorBranches.fetchAll()
|
||||||
}
|
}
|
||||||
|
|
||||||
@Sendable
|
@Sendable
|
||||||
@@ -29,7 +31,7 @@ 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.")
|
||||||
}
|
}
|
||||||
return try await vendorBranches.fetch(for: id, on: req.db)
|
return try await vendorBranches.fetchForVendor(id)
|
||||||
}
|
}
|
||||||
|
|
||||||
@Sendable
|
@Sendable
|
||||||
@@ -39,7 +41,7 @@ struct VendorBranchApiController: RouteCollection {
|
|||||||
}
|
}
|
||||||
try VendorBranch.Create.validate(content: req)
|
try VendorBranch.Create.validate(content: req)
|
||||||
let model = try req.content.decode(VendorBranch.Create.self)
|
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
|
@Sendable
|
||||||
@@ -49,7 +51,7 @@ struct VendorBranchApiController: RouteCollection {
|
|||||||
}
|
}
|
||||||
try VendorBranch.Update.validate(content: req)
|
try VendorBranch.Update.validate(content: req)
|
||||||
let updates = try req.content.decode(VendorBranch.Update.self)
|
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
|
@Sendable
|
||||||
@@ -57,7 +59,7 @@ struct VendorBranchApiController: RouteCollection {
|
|||||||
guard let id = req.parameters.get("id", as: VendorBranch.IDValue.self) else {
|
guard let id = req.parameters.get("id", as: VendorBranch.IDValue.self) else {
|
||||||
throw Abort(.badRequest, reason: "Vendor branch id not provided.")
|
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
|
return .ok
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,15 +1,33 @@
|
|||||||
|
import Dependencies
|
||||||
|
import DependenciesMacros
|
||||||
import Fluent
|
import Fluent
|
||||||
import Vapor
|
import Vapor
|
||||||
|
|
||||||
// An intermediate between our api and view controllers that interacts with the
|
extension DependencyValues {
|
||||||
// database.
|
// An intermediate between our api and view controllers that interacts with the
|
||||||
struct PurchaseOrderDB {
|
// database.
|
||||||
|
var purchaseOrders: PurchaseOrdersDB {
|
||||||
|
get { self[PurchaseOrdersDB.self] }
|
||||||
|
set { self[PurchaseOrdersDB.self] = newValue }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func create(
|
@DependencyClient
|
||||||
_ model: PurchaseOrder.Create,
|
struct PurchaseOrdersDB: Sendable {
|
||||||
createdById: User.IDValue,
|
var create: @Sendable (PurchaseOrder.Create, User.IDValue) async throws -> PurchaseOrder.DTO
|
||||||
on db: any Database
|
var fetchAll: @Sendable () async throws -> [PurchaseOrder.DTO]
|
||||||
) async throws -> PurchaseOrder.DTO {
|
var fetchPage: @Sendable (PageRequest) async throws -> Page<PurchaseOrder.DTO>
|
||||||
|
var get: @Sendable (PurchaseOrder.IDValue) async throws -> PurchaseOrder.DTO?
|
||||||
|
// var update: @Sendable (PurchaseOrder.IDValue, PurchaseOrder.Update) async throws -> PurchaseOrder.DTO
|
||||||
|
var delete: @Sendable (PurchaseOrder.IDValue) async throws -> Void
|
||||||
|
}
|
||||||
|
|
||||||
|
extension PurchaseOrdersDB: TestDependencyKey {
|
||||||
|
static let testValue: PurchaseOrdersDB = Self()
|
||||||
|
|
||||||
|
static func live(database db: any Database) -> Self {
|
||||||
|
.init(
|
||||||
|
create: { model, createdById in
|
||||||
guard let employee = try await Employee.find(model.createdForID, on: db) else {
|
guard let employee = try await Employee.find(model.createdForID, on: db) else {
|
||||||
throw Abort(.notFound, reason: "Employee not found.")
|
throw Abort(.notFound, reason: "Employee not found.")
|
||||||
}
|
}
|
||||||
@@ -20,37 +38,35 @@ struct PurchaseOrderDB {
|
|||||||
|
|
||||||
let purchaseOrder = model.toModel(createdByID: createdById)
|
let purchaseOrder = model.toModel(createdByID: createdById)
|
||||||
try await purchaseOrder.save(on: db)
|
try await purchaseOrder.save(on: db)
|
||||||
guard let loaded = try await get(id: purchaseOrder.requireID(), on: db) else {
|
guard let loaded = try await PurchaseOrder.get(purchaseOrder.requireID(), on: db) else {
|
||||||
return purchaseOrder.toDTO()
|
return purchaseOrder.toDTO()
|
||||||
}
|
}
|
||||||
return loaded
|
return loaded
|
||||||
}
|
|
||||||
|
|
||||||
func fetchAll(on db: any Database) async throws -> [PurchaseOrder.DTO] {
|
},
|
||||||
|
fetchAll: {
|
||||||
try await PurchaseOrder.allQuery(on: db)
|
try await PurchaseOrder.allQuery(on: db)
|
||||||
.sort(\.$id, .descending)
|
.sort(\.$id, .descending)
|
||||||
.all().map { $0.toDTO() }
|
.all().map { $0.toDTO() }
|
||||||
}
|
},
|
||||||
|
fetchPage: { request in
|
||||||
func fetchPage(_ page: Int, limit: Int, on db: any Database) async throws -> Page<PurchaseOrder.DTO> {
|
|
||||||
try await PurchaseOrder.allQuery(on: db)
|
try await PurchaseOrder.allQuery(on: db)
|
||||||
.sort(\.$id, .descending)
|
.sort(\.$id, .descending)
|
||||||
.paginate(PageRequest(page: page, per: limit))
|
.paginate(request)
|
||||||
.map { $0.toDTO() }
|
.map { $0.toDTO() }
|
||||||
}
|
},
|
||||||
|
get: { id in
|
||||||
func get(id: PurchaseOrder.IDValue, on db: any Database) async throws -> PurchaseOrder.DTO? {
|
try await PurchaseOrder.get(id, on: db)
|
||||||
try await PurchaseOrder.allQuery(on: db)
|
},
|
||||||
.filter(\.$id == id)
|
delete: { id in
|
||||||
.first()?.toDTO()
|
|
||||||
}
|
|
||||||
|
|
||||||
func delete(id: PurchaseOrder.IDValue, on db: any Database) async throws {
|
|
||||||
guard let purchaseOrder = try await PurchaseOrder.find(id, on: db) else {
|
guard let purchaseOrder = try await PurchaseOrder.find(id, on: db) else {
|
||||||
throw Abort(.notFound)
|
throw Abort(.notFound)
|
||||||
}
|
}
|
||||||
try await purchaseOrder.delete(on: db)
|
try await purchaseOrder.delete(on: db)
|
||||||
}
|
}
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private extension PurchaseOrder {
|
private extension PurchaseOrder {
|
||||||
@@ -62,4 +78,10 @@ private extension PurchaseOrder {
|
|||||||
branch.with(\.$vendor)
|
branch.with(\.$vendor)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static func get(_ id: PurchaseOrder.IDValue, on db: any Database) async throws -> PurchaseOrder.DTO? {
|
||||||
|
try await PurchaseOrder.allQuery(on: db)
|
||||||
|
.filter(\.$id == id)
|
||||||
|
.first()?.toDTO()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,9 +1,29 @@
|
|||||||
|
import Dependencies
|
||||||
|
import DependenciesMacros
|
||||||
import Fluent
|
import Fluent
|
||||||
import Vapor
|
import Vapor
|
||||||
|
|
||||||
struct UserDB {
|
extension DependencyValues {
|
||||||
|
var users: UserDB {
|
||||||
|
get { self[UserDB.self] }
|
||||||
|
set { self[UserDB.self] = newValue }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func create(_ model: User.Create, on db: any Database) async throws -> User.DTO {
|
@DependencyClient
|
||||||
|
struct UserDB: Sendable {
|
||||||
|
var create: @Sendable (User.Create) async throws -> User.DTO
|
||||||
|
var delete: @Sendable (User.IDValue) async throws -> Void
|
||||||
|
var fetchAll: @Sendable () async throws -> [User.DTO]
|
||||||
|
var login: @Sendable (User) async throws -> UserToken
|
||||||
|
}
|
||||||
|
|
||||||
|
extension UserDB: TestDependencyKey {
|
||||||
|
static let testValue: UserDB = Self()
|
||||||
|
|
||||||
|
static func live(database db: any Database) -> Self {
|
||||||
|
self.init(
|
||||||
|
create: { model in
|
||||||
guard model.password == model.confirmPassword else {
|
guard model.password == model.confirmPassword else {
|
||||||
throw Abort(.badRequest, reason: "Passwords did not match.")
|
throw Abort(.badRequest, reason: "Passwords did not match.")
|
||||||
}
|
}
|
||||||
@@ -14,23 +34,23 @@ struct UserDB {
|
|||||||
)
|
)
|
||||||
try await user.save(on: db)
|
try await user.save(on: db)
|
||||||
return user.toDTO()
|
return user.toDTO()
|
||||||
}
|
|
||||||
|
|
||||||
func login(user: User, on db: any Database) async throws -> UserToken {
|
},
|
||||||
let token = try user.generateToken()
|
delete: { id in
|
||||||
try await token.save(on: db)
|
|
||||||
return token
|
|
||||||
}
|
|
||||||
|
|
||||||
func fetchAll(on db: any Database) async throws -> [User.DTO] {
|
|
||||||
try await User.query(on: db).all().map { $0.toDTO() }
|
|
||||||
}
|
|
||||||
|
|
||||||
func delete(id: User.IDValue, on db: any Database) async throws {
|
|
||||||
guard let user = try await User.find(id, on: db) else {
|
guard let user = try await User.find(id, on: db) else {
|
||||||
throw Abort(.notFound)
|
throw Abort(.notFound)
|
||||||
}
|
}
|
||||||
try await user.delete(on: db)
|
try await user.delete(on: db)
|
||||||
}
|
|
||||||
|
|
||||||
|
},
|
||||||
|
fetchAll: {
|
||||||
|
try await User.query(on: db).all().map { $0.toDTO() }
|
||||||
|
},
|
||||||
|
login: { user in
|
||||||
|
let token = try user.generateToken()
|
||||||
|
try await token.save(on: db)
|
||||||
|
return token
|
||||||
|
}
|
||||||
|
)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,30 +1,57 @@
|
|||||||
|
import Dependencies
|
||||||
|
import DependenciesMacros
|
||||||
import Fluent
|
import Fluent
|
||||||
import Vapor
|
import Vapor
|
||||||
|
|
||||||
struct VendorBranchDB {
|
public extension DependencyValues {
|
||||||
|
var vendorBranches: VendorBranchDB {
|
||||||
|
get { self[VendorBranchDB.self] }
|
||||||
|
set { self[VendorBranchDB.self] = newValue }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func create(
|
@DependencyClient
|
||||||
_ model: VendorBranch.Create,
|
public struct VendorBranchDB: Sendable {
|
||||||
for vendorID: Vendor.IDValue,
|
var create: @Sendable (VendorBranch.Create, Vendor.IDValue) async throws -> VendorBranch.DTO
|
||||||
on db: any Database
|
var delete: @Sendable (VendorBranch.IDValue) async throws -> Void
|
||||||
) async throws -> VendorBranch.DTO {
|
var fetchAll: @Sendable (Bool) async throws -> [VendorBranch.DTO]
|
||||||
|
var fetchForVendor: @Sendable (Vendor.IDValue) async throws -> [VendorBranch.DTO]
|
||||||
|
var get: @Sendable (VendorBranch.IDValue) async throws -> VendorBranch.DTO?
|
||||||
|
var update: @Sendable (VendorBranch.IDValue, VendorBranch.Update) async throws -> VendorBranch.DTO
|
||||||
|
|
||||||
|
func fetchAll() async throws -> [VendorBranch.DTO] {
|
||||||
|
try await fetchAll(false)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
extension VendorBranchDB: TestDependencyKey {
|
||||||
|
public static let testValue: VendorBranchDB = Self()
|
||||||
|
|
||||||
|
static func live(database db: any Database) -> Self {
|
||||||
|
.init(
|
||||||
|
create: { model, vendorID in
|
||||||
let branch = model.toModel()
|
let branch = model.toModel()
|
||||||
guard let vendor = try await Vendor.find(vendorID, on: db) else {
|
guard let vendor = try await Vendor.find(vendorID, on: db) else {
|
||||||
throw Abort(.badRequest, reason: "Vendor does not exist.")
|
throw Abort(.badRequest, reason: "Vendor does not exist.")
|
||||||
}
|
}
|
||||||
try await vendor.$branches.create(branch, on: db)
|
try await vendor.$branches.create(branch, on: db)
|
||||||
return branch.toDTO()
|
return branch.toDTO()
|
||||||
|
},
|
||||||
|
delete: { id in
|
||||||
|
guard let branch = try await VendorBranch.find(id, on: db) else {
|
||||||
|
throw Abort(.notFound)
|
||||||
}
|
}
|
||||||
|
try await branch.delete(on: db)
|
||||||
func fetchAll(withVendor: Bool? = nil, on db: any Database) async throws -> [VendorBranch.DTO] {
|
},
|
||||||
|
fetchAll: { withVendor in
|
||||||
var query = VendorBranch.query(on: db)
|
var query = VendorBranch.query(on: db)
|
||||||
if withVendor == true {
|
if withVendor == true {
|
||||||
query = query.with(\.$vendor)
|
query = query.with(\.$vendor)
|
||||||
}
|
}
|
||||||
return try await query.all().map { $0.toDTO() }
|
return try await query.all().map { $0.toDTO() }
|
||||||
}
|
|
||||||
|
|
||||||
func fetch(for vendorID: Vendor.IDValue, on db: any Database) async throws -> [VendorBranch.DTO] {
|
},
|
||||||
|
fetchForVendor: { vendorID in
|
||||||
guard let vendor = try await Vendor.query(on: db)
|
guard let vendor = try await Vendor.query(on: db)
|
||||||
.filter(\.$id == vendorID)
|
.filter(\.$id == vendorID)
|
||||||
.with(\.$branches)
|
.with(\.$branches)
|
||||||
@@ -34,17 +61,13 @@ struct VendorBranchDB {
|
|||||||
}
|
}
|
||||||
|
|
||||||
return vendor.branches.map { $0.toDTO() }
|
return vendor.branches.map { $0.toDTO() }
|
||||||
}
|
|
||||||
|
|
||||||
func get(id: VendorBranch.IDValue, on db: any Database) async throws -> VendorBranch.DTO? {
|
},
|
||||||
|
get: { id in
|
||||||
|
|
||||||
try await VendorBranch.find(id, on: db).map { $0.toDTO() }
|
try await VendorBranch.find(id, on: db).map { $0.toDTO() }
|
||||||
}
|
},
|
||||||
|
update: { id, updates in
|
||||||
func update(
|
|
||||||
id: VendorBranch.IDValue,
|
|
||||||
with updates: VendorBranch.Update,
|
|
||||||
on db: any Database
|
|
||||||
) async throws -> VendorBranch.DTO {
|
|
||||||
guard let branch = try await VendorBranch.find(id, on: db) else {
|
guard let branch = try await VendorBranch.find(id, on: db) else {
|
||||||
throw Abort(.notFound)
|
throw Abort(.notFound)
|
||||||
}
|
}
|
||||||
@@ -52,11 +75,6 @@ struct VendorBranchDB {
|
|||||||
try await branch.save(on: db)
|
try await branch.save(on: db)
|
||||||
return branch.toDTO()
|
return branch.toDTO()
|
||||||
}
|
}
|
||||||
|
)
|
||||||
func delete(id: VendorBranch.IDValue, on db: any Database) async throws {
|
|
||||||
guard let branch = try await VendorBranch.find(id, on: db) else {
|
|
||||||
throw Abort(.notFound)
|
|
||||||
}
|
|
||||||
try await branch.delete(on: db)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,47 +1,85 @@
|
|||||||
|
import Dependencies
|
||||||
|
import DependenciesMacros
|
||||||
import Fluent
|
import Fluent
|
||||||
import Vapor
|
import Vapor
|
||||||
|
|
||||||
struct VendorDB {
|
public extension DependencyValues {
|
||||||
func create(_ model: Vendor.Create, on db: any Database) async throws -> Vendor.DTO {
|
var vendors: VendorDB {
|
||||||
|
get { self[VendorDB.self] }
|
||||||
|
set { self[VendorDB.self] = newValue }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@DependencyClient
|
||||||
|
public struct VendorDB: Sendable {
|
||||||
|
var create: @Sendable (Vendor.Create) async throws -> Vendor.DTO
|
||||||
|
var delete: @Sendable (Vendor.IDValue) async throws -> Void
|
||||||
|
var fetchAll: @Sendable (FetchRequest) async throws -> [Vendor.DTO]
|
||||||
|
var get: @Sendable (Vendor.IDValue, GetRequest) async throws -> Vendor.DTO?
|
||||||
|
var update: @Sendable (Vendor.IDValue, Vendor.Update) async throws -> Vendor.DTO
|
||||||
|
|
||||||
|
enum FetchRequest {
|
||||||
|
case `default`
|
||||||
|
case withBranches
|
||||||
|
}
|
||||||
|
|
||||||
|
enum GetRequest {
|
||||||
|
case `default`
|
||||||
|
case withBranches
|
||||||
|
}
|
||||||
|
|
||||||
|
func fetchAll() async throws -> [Vendor.DTO] {
|
||||||
|
try await fetchAll(.default)
|
||||||
|
}
|
||||||
|
|
||||||
|
func get(_ id: Vendor.IDValue) async throws -> Vendor.DTO? {
|
||||||
|
try await get(id, .default)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
extension VendorDB: TestDependencyKey {
|
||||||
|
public static let testValue: VendorDB = Self()
|
||||||
|
|
||||||
|
static func live(database db: any Database) -> Self {
|
||||||
|
.init(
|
||||||
|
create: { model in
|
||||||
let model = model.toModel()
|
let model = model.toModel()
|
||||||
try await model.save(on: db)
|
try await model.save(on: db)
|
||||||
return model.toDTO()
|
return model.toDTO()
|
||||||
}
|
|
||||||
|
|
||||||
func fetchAll(withBranches: Bool? = nil, on db: any Database) async throws -> [Vendor.DTO] {
|
},
|
||||||
|
delete: { id in
|
||||||
|
guard let vendor = try await Vendor.find(id, on: db) else {
|
||||||
|
throw Abort(.notFound)
|
||||||
|
}
|
||||||
|
try await vendor.delete(on: db)
|
||||||
|
|
||||||
|
},
|
||||||
|
fetchAll: { request in
|
||||||
var query = Vendor.query(on: db).sort(\.$name, .ascending)
|
var query = Vendor.query(on: db).sort(\.$name, .ascending)
|
||||||
if withBranches == true {
|
let withBranches = request == .withBranches
|
||||||
|
if withBranches {
|
||||||
query = query.with(\.$branches)
|
query = query.with(\.$branches)
|
||||||
}
|
}
|
||||||
return try await query.all().map { $0.toDTO(includeBranches: withBranches) }
|
return try await query.all().map { $0.toDTO(includeBranches: withBranches) }
|
||||||
}
|
|
||||||
|
|
||||||
func get(id: Vendor.IDValue, withBranches: Bool? = nil, on db: any Database) async throws -> Vendor.DTO? {
|
},
|
||||||
|
get: { id, request in
|
||||||
var query = Vendor.query(on: db).filter(\.$id == id)
|
var query = Vendor.query(on: db).filter(\.$id == id)
|
||||||
|
let withBranches = request == .withBranches
|
||||||
if withBranches == true {
|
if withBranches {
|
||||||
query = query.with(\.$branches)
|
query = query.with(\.$branches)
|
||||||
}
|
}
|
||||||
return try await query.first().map { $0.toDTO(includeBranches: withBranches) }
|
return try await query.first().map { $0.toDTO(includeBranches: withBranches) }
|
||||||
}
|
|
||||||
|
|
||||||
func update(
|
},
|
||||||
id: Vendor.IDValue,
|
update: { id, updates in
|
||||||
with updates: Vendor.Update,
|
|
||||||
on db: any Database
|
|
||||||
) async throws -> Vendor.DTO {
|
|
||||||
guard let vendor = try await Vendor.find(id, on: db) else {
|
guard let vendor = try await Vendor.find(id, on: db) else {
|
||||||
throw Abort(.notFound)
|
throw Abort(.notFound)
|
||||||
}
|
}
|
||||||
vendor.applyUpdates(updates)
|
vendor.applyUpdates(updates)
|
||||||
return vendor.toDTO()
|
return vendor.toDTO()
|
||||||
}
|
}
|
||||||
|
)
|
||||||
func delete(id: Vendor.IDValue, on db: any Database) async throws {
|
|
||||||
guard let vendor = try await Vendor.find(id, on: db) else {
|
|
||||||
throw Abort(.notFound)
|
|
||||||
}
|
}
|
||||||
try await vendor.delete(on: db)
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -23,7 +23,7 @@ struct EmployeeViewController: RouteCollection {
|
|||||||
|
|
||||||
@Sendable
|
@Sendable
|
||||||
func index(req: Request) async throws -> View {
|
func index(req: Request) async throws -> View {
|
||||||
return try await req.view.render("employees/index", EmployeesCTX())
|
return try await req.view.render("employees/index", EmployeesCTX(db: employees))
|
||||||
}
|
}
|
||||||
|
|
||||||
@Sendable
|
@Sendable
|
||||||
@@ -31,7 +31,7 @@ struct EmployeeViewController: RouteCollection {
|
|||||||
try Employee.Create.validate(content: req)
|
try Employee.Create.validate(content: req)
|
||||||
let model = try req.content.decode(Employee.Create.self)
|
let model = try req.content.decode(Employee.Create.self)
|
||||||
_ = try await employees.create(model)
|
_ = try await employees.create(model)
|
||||||
return try await req.view.render("employees/index", EmployeesCTX(oob: true))
|
return try await req.view.render("employees/index", EmployeesCTX(oob: true, db: employees))
|
||||||
}
|
}
|
||||||
|
|
||||||
@Sendable
|
@Sendable
|
||||||
@@ -63,7 +63,7 @@ struct EmployeeViewController: RouteCollection {
|
|||||||
@Sendable
|
@Sendable
|
||||||
func update(req: Request) async throws -> View {
|
func update(req: Request) async throws -> View {
|
||||||
_ = try await api.update(req: req)
|
_ = try await api.update(req: req)
|
||||||
return try await req.view.render("employees/index", EmployeesCTX(oob: true))
|
return try await req.view.render("employees/index", EmployeesCTX(oob: true, db: employees))
|
||||||
}
|
}
|
||||||
|
|
||||||
@Sendable
|
@Sendable
|
||||||
@@ -80,11 +80,11 @@ private struct EmployeesCTX: Content {
|
|||||||
|
|
||||||
init(
|
init(
|
||||||
oob: Bool = false,
|
oob: Bool = false,
|
||||||
employee: Employee? = nil
|
employee: Employee? = nil,
|
||||||
|
db: EmployeeDB
|
||||||
) async throws {
|
) async throws {
|
||||||
@Dependency(\.employees) var employees
|
|
||||||
self.oob = oob
|
self.oob = oob
|
||||||
self.employees = try await employees.fetchAll()
|
self.employees = try await db.fetchAll()
|
||||||
self.form = .init(employee: employee.map { $0.toDTO() })
|
self.form = .init(employee: employee.map { $0.toDTO() })
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,11 +1,13 @@
|
|||||||
|
import Dependencies
|
||||||
import Fluent
|
import Fluent
|
||||||
import Vapor
|
import Vapor
|
||||||
|
|
||||||
struct PurchaseOrderViewController: RouteCollection {
|
struct PurchaseOrderViewController: RouteCollection {
|
||||||
|
@Dependency(\.purchaseOrders) var purchaseOrders
|
||||||
|
|
||||||
private let employeesApi = EmployeeApiController()
|
private let employeesApi = EmployeeApiController()
|
||||||
private let branches = VendorBranchDB()
|
private let branches = VendorBranchDB()
|
||||||
private let api = ApiController()
|
private let api = ApiController()
|
||||||
private let api2 = PurchaseOrderDB()
|
|
||||||
|
|
||||||
func boot(routes: any RoutesBuilder) throws {
|
func boot(routes: any RoutesBuilder) throws {
|
||||||
let pos = routes.protected.grouped("purchase-orders")
|
let pos = routes.protected.grouped("purchase-orders")
|
||||||
@@ -20,7 +22,9 @@ struct PurchaseOrderViewController: RouteCollection {
|
|||||||
@Sendable
|
@Sendable
|
||||||
func index(req: Request) async throws -> View {
|
func index(req: Request) async throws -> View {
|
||||||
let params = try? req.query.decode(PurchaseOrderIndex.self)
|
let params = try? req.query.decode(PurchaseOrderIndex.self)
|
||||||
let purchaseOrdersPage = try await api2.fetchPage(params?.page ?? 1, limit: params?.limit ?? 50, on: req.db)
|
let purchaseOrdersPage = try await purchaseOrders.fetchPage(
|
||||||
|
.init(page: params?.page ?? 1, per: params?.limit ?? 50)
|
||||||
|
)
|
||||||
let branches = try await self.branches.getBranches(req: req)
|
let branches = try await self.branches.getBranches(req: req)
|
||||||
let employees = try await employeesApi.index(req: req)
|
let employees = try await employeesApi.index(req: req)
|
||||||
req.logger.debug("Branches: \(branches)")
|
req.logger.debug("Branches: \(branches)")
|
||||||
@@ -38,7 +42,7 @@ struct PurchaseOrderViewController: RouteCollection {
|
|||||||
guard let id = req.parameters.get("id", as: PurchaseOrder.IDValue.self) else {
|
guard let id = req.parameters.get("id", as: PurchaseOrder.IDValue.self) else {
|
||||||
throw Abort(.badRequest, reason: "Id not supplied.")
|
throw Abort(.badRequest, reason: "Id not supplied.")
|
||||||
}
|
}
|
||||||
let purchaseOrder = try await api2.get(id: id, on: req.db)
|
let purchaseOrder = try await purchaseOrders.get(id)
|
||||||
return try await req.view.render("purchaseOrders/detail", ["purchaseOrder": purchaseOrder])
|
return try await req.view.render("purchaseOrders/detail", ["purchaseOrder": purchaseOrder])
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -47,7 +51,7 @@ struct PurchaseOrderViewController: RouteCollection {
|
|||||||
try PurchaseOrder.FormCreate.validate(content: req)
|
try PurchaseOrder.FormCreate.validate(content: req)
|
||||||
let createdById = try req.auth.require(User.self).requireID()
|
let createdById = try req.auth.require(User.self).requireID()
|
||||||
let create = try req.content.decode(PurchaseOrder.FormCreate.self).toCreate()
|
let create = try req.content.decode(PurchaseOrder.FormCreate.self).toCreate()
|
||||||
let purchaseOrder = try await api2.create(create, createdById: createdById, on: req.db)
|
let purchaseOrder = try await purchaseOrders.create(create, createdById)
|
||||||
return try await req.view.render("purchaseOrders/table-row", purchaseOrder)
|
return try await req.view.render("purchaseOrders/table-row", purchaseOrder)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -34,6 +34,10 @@ public func configure(_ app: Application) async throws {
|
|||||||
|
|
||||||
try withDependencies {
|
try withDependencies {
|
||||||
$0.employees = .live(database: app.db(.sqlite))
|
$0.employees = .live(database: app.db(.sqlite))
|
||||||
|
$0.purchaseOrders = .live(database: app.db(.sqlite))
|
||||||
|
$0.users = .live(database: app.db(.sqlite))
|
||||||
|
$0.vendorBranches = .live(database: app.db(.sqlite))
|
||||||
|
$0.vendors = .live(database: app.db(.sqlite))
|
||||||
} operation: {
|
} operation: {
|
||||||
// register routes
|
// register routes
|
||||||
try routes(app)
|
try routes(app)
|
||||||
|
|||||||
Reference in New Issue
Block a user