feat: working on detail views.

This commit is contained in:
2025-01-12 17:42:06 -05:00
parent 0e31d2c30c
commit 1ce369e156
27 changed files with 527 additions and 137 deletions

View File

@@ -15,14 +15,19 @@ extension DependencyValues {
@DependencyClient
struct EmployeeDB: Sendable {
var create: @Sendable (Employee.Create) async throws -> Employee.DTO
var fetchAll: @Sendable (Bool) async throws -> [Employee.DTO]
var fetchAll: @Sendable (FetchRequest) async throws -> [Employee.DTO]
var get: @Sendable (Employee.IDValue) async throws -> Employee.DTO?
var update: @Sendable (Employee.IDValue, Employee.Update) async throws -> Employee.DTO
var delete: @Sendable (Employee.IDValue) async throws -> Void
var toggleActive: @Sendable (Employee.IDValue) async throws -> Employee.DTO
enum FetchRequest {
case active
case `default`
}
func fetchAll() async throws -> [Employee.DTO] {
try await fetchAll(false)
try await fetchAll(.default)
}
func get(_ id: String?) async throws -> Employee.DTO? {
@@ -43,12 +48,12 @@ extension EmployeeDB: TestDependencyKey {
try await model.save(on: database)
return model.toDTO()
},
fetchAll: { active in
fetchAll: { request in
var query = Employee.query(on: database)
.sort(\.$lastName)
if active {
query = query.filter(\.$active == active)
if request == .active {
query = query.filter(\.$active == true)
}
return try await query.all().map { $0.toDTO() }

View File

@@ -15,6 +15,7 @@ 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 get: @Sendable (User.IDValue) async throws -> User.DTO?
var login: @Sendable (User) async throws -> UserToken
}
@@ -46,6 +47,9 @@ extension UserDB: TestDependencyKey {
fetchAll: {
try await User.query(on: db).all().map { $0.toDTO() }
},
get: { id in
try await User.find(id, on: db).map { $0.toDTO() }
},
login: { user in
let token = try user.generateToken()
try await token.save(on: db)

View File

@@ -14,13 +14,18 @@ public extension DependencyValues {
public struct VendorBranchDB: Sendable {
var create: @Sendable (VendorBranch.Create, Vendor.IDValue) async throws -> VendorBranch.DTO
var delete: @Sendable (VendorBranch.IDValue) async throws -> Void
var fetchAll: @Sendable (Bool) async throws -> [VendorBranch.DTO]
var fetchForVendor: @Sendable (Vendor.IDValue) async throws -> [VendorBranch.DTO]
var fetchAll: @Sendable (FetchRequest) async throws -> [VendorBranch.DTO]
var get: @Sendable (VendorBranch.IDValue) async throws -> VendorBranch.DTO?
var update: @Sendable (VendorBranch.IDValue, VendorBranch.Update) async throws -> VendorBranch.DTO
enum FetchRequest: Equatable {
case `default`
case `for`(vendorID: Vendor.IDValue)
case withVendor
}
func fetchAll() async throws -> [VendorBranch.DTO] {
try await fetchAll(false)
try await fetchAll(.default)
}
}
@@ -43,28 +48,30 @@ extension VendorBranchDB: TestDependencyKey {
}
try await branch.delete(on: db)
},
fetchAll: { withVendor in
fetchAll: { request in
var query = VendorBranch.query(on: db)
if withVendor == true {
switch request {
case .withVendor:
query = query.with(\.$vendor)
case let .for(vendorID: vendorID):
let branches = try await Vendor.query(on: db)
.filter(\.$id == vendorID)
.with(\.$branches)
.first()?
.branches
.map { $0.toDTO() }
guard let branches else { throw Abort(.badGateway, reason: "Vendor id not found.") }
return branches
case .default:
break
}
return try await query.all().map { $0.toDTO() }
},
fetchForVendor: { vendorID in
guard let vendor = try await Vendor.query(on: db)
.filter(\.$id == vendorID)
.with(\.$branches)
.first()
else {
throw Abort(.notFound)
}
return vendor.branches.map { $0.toDTO() }
},
get: { id in
try await VendorBranch.find(id, on: db).map { $0.toDTO() }
},
update: { id, updates in