Files
gridpilot.gg/core/racing/application/use-cases/GetTotalDriversUseCase.ts
2025-12-19 19:42:19 +01:00

31 lines
1.2 KiB
TypeScript

import type { IDriverRepository } from '../../domain/repositories/IDriverRepository';
import type { TotalDriversOutputPort } from '../ports/output/TotalDriversOutputPort';
import { Result } from '@core/shared/application/Result';
import type { ApplicationErrorCode } from '@core/shared/errors/ApplicationErrorCode';
import type { AsyncUseCase } from '@core/shared/application';
import type { Logger } from '@core/shared/application';
/**
* Use Case for retrieving total number of drivers.
*/
export class GetTotalDriversUseCase implements AsyncUseCase<void, TotalDriversOutputPort, 'REPOSITORY_ERROR'>
{
constructor(
private readonly driverRepository: IDriverRepository,
private readonly logger: Logger,
) {}
async execute(): Promise<Result<TotalDriversOutputPort, ApplicationErrorCode<'REPOSITORY_ERROR'>>> {
try {
const drivers = await this.driverRepository.findAll();
const output: TotalDriversOutputPort = {
totalDrivers: drivers.length,
};
return Result.ok(output);
} catch (error) {
this.logger.error('Error retrieving total drivers', error as Error);
return Result.err({ code: 'REPOSITORY_ERROR', details: { message: 'Failed to retrieve total drivers' } });
}
}
}