refactor
This commit is contained in:
@@ -1,22 +1,33 @@
|
||||
import type { ISeasonRepository } from '../../domain/repositories/ISeasonRepository';
|
||||
import type { IGetLeagueSeasonsPresenter, GetLeagueSeasonsResultDTO, GetLeagueSeasonsViewModel } from '../presenters/IGetLeagueSeasonsPresenter';
|
||||
import type { UseCase } from '@core/shared/application/UseCase';
|
||||
import type { GetLeagueSeasonsViewModel } from '../presenters/IGetLeagueSeasonsPresenter';
|
||||
import { Result } from '@core/shared/application/Result';
|
||||
import type { ApplicationErrorCode } from '@core/shared/errors/ApplicationErrorCode';
|
||||
|
||||
export interface GetLeagueSeasonsUseCaseParams {
|
||||
leagueId: string;
|
||||
}
|
||||
|
||||
export interface GetLeagueSeasonsResultDTO {
|
||||
seasons: unknown[];
|
||||
}
|
||||
|
||||
export class GetLeagueSeasonsUseCase implements UseCase<GetLeagueSeasonsUseCaseParams, GetLeagueSeasonsResultDTO, GetLeagueSeasonsViewModel, IGetLeagueSeasonsPresenter> {
|
||||
export class GetLeagueSeasonsUseCase {
|
||||
constructor(private readonly seasonRepository: ISeasonRepository) {}
|
||||
|
||||
async execute(params: GetLeagueSeasonsUseCaseParams, presenter: IGetLeagueSeasonsPresenter): Promise<void> {
|
||||
const seasons = await this.seasonRepository.findByLeagueId(params.leagueId);
|
||||
const dto: GetLeagueSeasonsResultDTO = { seasons };
|
||||
presenter.reset();
|
||||
presenter.present(dto);
|
||||
async execute(params: GetLeagueSeasonsUseCaseParams): Promise<Result<GetLeagueSeasonsViewModel, ApplicationErrorCode<'REPOSITORY_ERROR'>>> {
|
||||
try {
|
||||
const seasons = await this.seasonRepository.findByLeagueId(params.leagueId);
|
||||
const activeCount = seasons.filter(s => s.status === 'active').length;
|
||||
const viewModel: GetLeagueSeasonsViewModel = {
|
||||
seasons: seasons.map(s => ({
|
||||
seasonId: s.id,
|
||||
name: s.name,
|
||||
status: s.status,
|
||||
startDate: s.startDate,
|
||||
endDate: s.endDate,
|
||||
isPrimary: false,
|
||||
isParallelActive: s.status === 'active' && activeCount > 1
|
||||
}))
|
||||
};
|
||||
return Result.ok(viewModel);
|
||||
} catch {
|
||||
return Result.err({ code: 'REPOSITORY_ERROR', message: 'Failed to fetch seasons' });
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user