refactor racing use cases
This commit is contained in:
@@ -1,25 +1,41 @@
|
||||
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';
|
||||
import type { Logger, UseCaseOutputPort } from '@core/shared/application';
|
||||
|
||||
export class GetTotalRacesUseCase implements AsyncUseCase<void, GetTotalRacesOutputPort, 'REPOSITORY_ERROR'>
|
||||
{
|
||||
export type GetTotalRacesInput = {};
|
||||
|
||||
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>,
|
||||
) {}
|
||||
|
||||
async execute(): Promise<Result<GetTotalRacesOutputPort, ApplicationErrorCode<'REPOSITORY_ERROR'>>> {
|
||||
async execute(_input: GetTotalRacesInput): Promise<
|
||||
Result<void, ApplicationErrorCode<GetTotalRacesErrorCode, { message: string }>>
|
||||
> {
|
||||
try {
|
||||
const races = await this.raceRepository.findAll();
|
||||
const output: GetTotalRacesOutputPort = { totalRaces: races.length };
|
||||
|
||||
return Result.ok(output);
|
||||
this.output.present({ totalRaces: races.length });
|
||||
|
||||
return Result.ok(undefined);
|
||||
} 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' } });
|
||||
const err = error as Error;
|
||||
|
||||
this.logger.error('Error retrieving total races', err);
|
||||
|
||||
return Result.err({
|
||||
code: 'REPOSITORY_ERROR',
|
||||
details: { message: err.message ?? 'Failed to compute total races' },
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user