Files
gridpilot.gg/core/racing/application/use-cases/GetTotalRacesUseCase.ts
2025-12-20 12:55:07 +01:00

25 lines
1.1 KiB
TypeScript

import type { IRaceRepository } from '../../domain/repositories/IRaceRepository';
import type { GetTotalRacesOutputPort } from '../ports/output/GetTotalRacesOutputPort';
import { Result } from '@core/shared/application/Result';
import type { ApplicationErrorCode } from '@core/shared/errors/ApplicationErrorCode';
import type { AsyncUseCase , Logger } from '@core/shared/application';
export class GetTotalRacesUseCase implements AsyncUseCase<void, GetTotalRacesOutputPort, 'REPOSITORY_ERROR'>
{
constructor(
private readonly raceRepository: IRaceRepository,
private readonly logger: Logger,
) {}
async execute(): Promise<Result<GetTotalRacesOutputPort, ApplicationErrorCode<'REPOSITORY_ERROR'>>> {
try {
const races = await this.raceRepository.findAll();
const output: GetTotalRacesOutputPort = { totalRaces: races.length };
return Result.ok(output);
} catch (error) {
this.logger.error('Error retrieving total races', error as Error);
return Result.err({ code: 'REPOSITORY_ERROR', details: { message: 'Failed to retrieve total races' } });
}
}
}