import Fluent import Vapor // 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) } }