33 lines
1.0 KiB
TypeScript
33 lines
1.0 KiB
TypeScript
import type { ApplicationErrorCode } from '@core/shared/errors/ApplicationErrorCode';
|
|
import { Result } from '@core/shared/application/Result';
|
|
import type { ILeagueRepository } from '../../domain/repositories/ILeagueRepository';
|
|
|
|
export interface GetTotalLeaguesInput {}
|
|
|
|
export type GetTotalLeaguesErrorCode = 'REPOSITORY_ERROR';
|
|
|
|
export interface GetTotalLeaguesResult {
|
|
totalLeagues: number;
|
|
}
|
|
|
|
export class GetTotalLeaguesUseCase {
|
|
constructor(private readonly leagueRepository: ILeagueRepository) {}
|
|
|
|
async execute(
|
|
_input: GetTotalLeaguesInput,
|
|
): Promise<Result<GetTotalLeaguesResult, ApplicationErrorCode<GetTotalLeaguesErrorCode, { message: string }>>> {
|
|
try {
|
|
const leagues = await this.leagueRepository.findAll();
|
|
const totalLeagues = leagues.length;
|
|
|
|
return Result.ok({ totalLeagues });
|
|
} catch (error: unknown) {
|
|
const message = error instanceof Error ? error.message : 'Failed to get total leagues';
|
|
|
|
return Result.err({
|
|
code: 'REPOSITORY_ERROR',
|
|
details: { message },
|
|
});
|
|
}
|
|
}
|
|
} |