refactor
This commit is contained in:
@@ -1,11 +1,8 @@
|
||||
import type { IStandingRepository } from '../../domain/repositories/IStandingRepository';
|
||||
import type { IDriverRepository } from '../../domain/repositories/IDriverRepository';
|
||||
import type {
|
||||
ILeagueStandingsPresenter,
|
||||
LeagueStandingsResultDTO,
|
||||
LeagueStandingsViewModel,
|
||||
} from '../presenters/ILeagueStandingsPresenter';
|
||||
import type { UseCase } from '@core/shared/application/UseCase';
|
||||
import type { LeagueStandingsViewModel } from '../presenters/ILeagueStandingsPresenter';
|
||||
import { Result } from '@core/shared/application/Result';
|
||||
import type { ApplicationErrorCode } from '@core/shared/errors/ApplicationErrorCode';
|
||||
|
||||
export interface GetLeagueStandingsUseCaseParams {
|
||||
leagueId: string;
|
||||
@@ -13,12 +10,8 @@ export interface GetLeagueStandingsUseCaseParams {
|
||||
|
||||
/**
|
||||
* Use Case for retrieving league standings.
|
||||
* Orchestrates domain logic and delegates presentation to the presenter.
|
||||
*/
|
||||
export class GetLeagueStandingsUseCase
|
||||
implements
|
||||
UseCase<GetLeagueStandingsUseCaseParams, LeagueStandingsResultDTO, LeagueStandingsViewModel, ILeagueStandingsPresenter>
|
||||
{
|
||||
export class GetLeagueStandingsUseCase {
|
||||
constructor(
|
||||
private readonly standingRepository: IStandingRepository,
|
||||
private readonly driverRepository: IDriverRepository,
|
||||
@@ -26,17 +19,25 @@ export class GetLeagueStandingsUseCase
|
||||
|
||||
async execute(
|
||||
params: GetLeagueStandingsUseCaseParams,
|
||||
presenter: ILeagueStandingsPresenter,
|
||||
): Promise<void> {
|
||||
const standings = await this.standingRepository.findByLeagueId(params.leagueId);
|
||||
const driverIds = [...new Set(standings.map(s => s.driverId))];
|
||||
const drivers = await this.driverRepository.findByIds(driverIds);
|
||||
const driverMap = new Map(drivers.map(d => [d.id, { id: d.id, name: d.name }]));
|
||||
const dto: LeagueStandingsResultDTO = {
|
||||
standings,
|
||||
drivers: Array.from(driverMap.values()),
|
||||
};
|
||||
presenter.reset();
|
||||
presenter.present(dto);
|
||||
): Promise<Result<LeagueStandingsViewModel, ApplicationErrorCode<'REPOSITORY_ERROR'>>> {
|
||||
try {
|
||||
const standings = await this.standingRepository.findByLeagueId(params.leagueId);
|
||||
const driverIds = [...new Set(standings.map(s => s.driverId))];
|
||||
const driverPromises = driverIds.map(id => this.driverRepository.findById(id));
|
||||
const driverResults = await Promise.all(driverPromises);
|
||||
const drivers = driverResults.filter((d): d is NonNullable<typeof d> => d !== null);
|
||||
const driverMap = new Map(drivers.map(d => [d.id, { id: d.id, name: d.name }]));
|
||||
const viewModel: LeagueStandingsViewModel = {
|
||||
standings: standings.map(s => ({
|
||||
driverId: s.driverId,
|
||||
driver: driverMap.get(s.driverId)!,
|
||||
points: s.points,
|
||||
rank: s.position,
|
||||
})),
|
||||
};
|
||||
return Result.ok(viewModel);
|
||||
} catch {
|
||||
return Result.err({ code: 'REPOSITORY_ERROR', message: 'Failed to fetch league standings' });
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user