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