feat: Some updates to employee views.

This commit is contained in:
2025-01-10 23:32:36 -05:00
parent 69351d0a0b
commit 9994644d21
4 changed files with 79 additions and 99 deletions

View File

@@ -19,10 +19,18 @@ struct EmployeeDB: Sendable {
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
func fetchAll() async throws -> [Employee.DTO] {
try await fetchAll(false)
}
func get(_ id: String?) async throws -> Employee.DTO? {
guard let idString = id, let id = UUID(uuidString: idString) else {
throw Abort(.badRequest, reason: "Employee id not valid.")
}
return try await get(id)
}
}
extension EmployeeDB: TestDependencyKey {
@@ -61,54 +69,15 @@ extension EmployeeDB: TestDependencyKey {
throw Abort(.badRequest, reason: "Employee id not found.")
}
try await employee.delete(on: database)
},
toggleActive: { id in
guard let employee = try await Employee.find(id, on: database) else {
throw Abort(.notFound)
}
employee.active.toggle()
try await employee.save(on: database)
return employee.toDTO()
}
)
}
}
// An intermediate layer between our api and view controllers that interacts with the
// database model.
// struct EmployeeDB {
//
// func create(_ model: Employee.Create, on db: any Database) async throws -> Employee.DTO {
// let model = model.toModel()
// try await model.save(on: db)
// return model.toDTO()
// }
//
// func fetchAll(active: Bool? = nil, on db: any Database) async throws -> [Employee.DTO] {
// var query = Employee.query(on: db)
// .sort(\.$lastName)
//
// if let active {
// query = query.filter(\.$active == active)
// }
//
// return try await query.all().map { $0.toDTO() }
// }
//
// func get(id: Employee.IDValue, on db: any Database) async throws -> Employee.DTO? {
// try await Employee.find(id, on: db).map { $0.toDTO() }
// }
//
// func update(
// id: Employee.IDValue,
// with updates: Employee.Update,
// on db: any Database
// ) async throws -> Employee.DTO {
// guard let employee = try await Employee.find(id, on: db) else {
// throw Abort(.badRequest, reason: "Employee id not found.")
// }
// employee.applyUpdates(updates)
// try await employee.save(on: db)
// return employee.toDTO()
// }
//
// func delete(id: Employee.IDValue, on db: any Database) async throws {
// guard let employee = try await Employee.find(id, on: db) else {
// throw Abort(.badRequest, reason: "Employee id not found.")
// }
// try await employee.delete(on: db)
// }
//
// }