refactor racing use cases
This commit is contained in:
@@ -1,25 +1,41 @@
|
||||
import type { ILeagueRepository } from '../../domain/repositories/ILeagueRepository';
|
||||
import type { GetTotalLeaguesOutputPort } from '../ports/output/GetTotalLeaguesOutputPort';
|
||||
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 { UseCaseOutputPort } from '@core/shared/application';
|
||||
|
||||
export class GetTotalLeaguesUseCase implements AsyncUseCase<void, GetTotalLeaguesOutputPort, 'REPOSITORY_ERROR'>
|
||||
{
|
||||
export type GetTotalLeaguesInput = {};
|
||||
|
||||
export type GetTotalLeaguesResult = {
|
||||
totalLeagues: number;
|
||||
};
|
||||
|
||||
export type GetTotalLeaguesErrorCode = 'REPOSITORY_ERROR';
|
||||
|
||||
export class GetTotalLeaguesUseCase {
|
||||
constructor(
|
||||
private readonly leagueRepository: ILeagueRepository,
|
||||
private readonly logger: Logger,
|
||||
private readonly output: UseCaseOutputPort<GetTotalLeaguesResult>,
|
||||
) {}
|
||||
|
||||
async execute(): Promise<Result<GetTotalLeaguesOutputPort, ApplicationErrorCode<'REPOSITORY_ERROR'>>> {
|
||||
async execute(
|
||||
_input: GetTotalLeaguesInput,
|
||||
): Promise<Result<void, ApplicationErrorCode<GetTotalLeaguesErrorCode, { message: string }>>> {
|
||||
try {
|
||||
const leagues = await this.leagueRepository.findAll();
|
||||
const output: GetTotalLeaguesOutputPort = { totalLeagues: leagues.length };
|
||||
const result: GetTotalLeaguesResult = { totalLeagues: leagues.length };
|
||||
|
||||
return Result.ok(output);
|
||||
} catch (error) {
|
||||
this.logger.error('Error retrieving total leagues', error as Error);
|
||||
return Result.err({ code: 'REPOSITORY_ERROR', details: { message: 'Failed to retrieve total leagues' } });
|
||||
this.output.present(result);
|
||||
|
||||
return Result.ok(undefined);
|
||||
} catch (err) {
|
||||
const error = err as { message?: string } | undefined;
|
||||
|
||||
return Result.err({
|
||||
code: 'REPOSITORY_ERROR',
|
||||
details: {
|
||||
message: error?.message ?? 'Failed to compute total leagues',
|
||||
},
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user