refactor use cases

This commit is contained in:
2026-01-08 15:34:51 +01:00
parent d984ab24a8
commit 52e9a2f6a7
362 changed files with 5192 additions and 8409 deletions

View File

@@ -1,42 +1,33 @@
import type { IRaceRepository } from '../../domain/repositories/IRaceRepository';
import { Result } from '@core/shared/application/Result';
import type { ApplicationErrorCode } from '@core/shared/errors/ApplicationErrorCode';
import type { Logger, UseCaseOutputPort } from '@core/shared/application';
import type { IRaceRepository } from '../../domain/repositories/IRaceRepository';
export type GetTotalRacesInput = {};
export interface GetTotalRacesInput {}
export type GetTotalRacesErrorCode = 'REPOSITORY_ERROR';
export interface GetTotalRacesResult {
totalRaces: number;
}
export type GetTotalRacesErrorCode = 'REPOSITORY_ERROR';
export class GetTotalRacesUseCase {
constructor(
private readonly raceRepository: IRaceRepository,
private readonly logger: Logger,
private readonly output: UseCaseOutputPort<GetTotalRacesResult>,
) {}
constructor(private readonly raceRepository: IRaceRepository) {}
async execute(_input: GetTotalRacesInput): Promise<
Result<void, ApplicationErrorCode<GetTotalRacesErrorCode, { message: string }>>
> {
void _input;
async execute(
_input: GetTotalRacesInput,
): Promise<Result<GetTotalRacesResult, ApplicationErrorCode<GetTotalRacesErrorCode, { message: string }>>> {
try {
const races = await this.raceRepository.findAll();
const totalRaces = races.length;
this.output.present({ totalRaces: races.length });
return Result.ok(undefined);
} catch (error) {
const err = error as Error;
this.logger.error('Error retrieving total races', err);
return Result.ok({ totalRaces });
} catch (error: unknown) {
const message = error instanceof Error ? error.message : 'Failed to get total races';
return Result.err({
code: 'REPOSITORY_ERROR',
details: { message: err.message ?? 'Failed to compute total races' },
details: { message },
});
}
}
}
}