refactor
This commit is contained in:
@@ -1,28 +1,37 @@
|
||||
import type { ILeagueMembershipRepository } from '../../domain/repositories/ILeagueMembershipRepository';
|
||||
import type { IRaceRepository } from '../../domain/repositories/IRaceRepository';
|
||||
import type { ILeagueStatsPresenter, LeagueStatsResultDTO, LeagueStatsViewModel } from '../presenters/ILeagueStatsPresenter';
|
||||
import type { UseCase } from '@core/shared/application/UseCase';
|
||||
import type { LeagueStatsViewModel } from '../presenters/ILeagueStatsPresenter';
|
||||
import type { DriverRatingProvider } from '../ports/DriverRatingProvider';
|
||||
import { Result } from '@core/shared/application/Result';
|
||||
import type { ApplicationErrorCode } from '@core/shared/errors/ApplicationErrorCode';
|
||||
|
||||
export interface GetLeagueStatsUseCaseParams {
|
||||
leagueId: string;
|
||||
}
|
||||
|
||||
export class GetLeagueStatsUseCase implements UseCase<GetLeagueStatsUseCaseParams, LeagueStatsResultDTO, LeagueStatsViewModel, ILeagueStatsPresenter> {
|
||||
export class GetLeagueStatsUseCase {
|
||||
constructor(
|
||||
private readonly leagueMembershipRepository: ILeagueMembershipRepository,
|
||||
private readonly raceRepository: IRaceRepository,
|
||||
private readonly driverRatingProvider: DriverRatingProvider,
|
||||
) {}
|
||||
|
||||
async execute(params: GetLeagueStatsUseCaseParams, presenter: ILeagueStatsPresenter): Promise<void> {
|
||||
const memberships = await this.leagueMembershipRepository.getLeagueMembers(params.leagueId);
|
||||
const races = await this.raceRepository.findByLeagueId(params.leagueId);
|
||||
// TODO: Implement average rating calculation from driver ratings
|
||||
const dto: LeagueStatsResultDTO = {
|
||||
totalMembers: memberships.length,
|
||||
totalRaces: races.length,
|
||||
averageRating: 0,
|
||||
};
|
||||
presenter.reset();
|
||||
presenter.present(dto);
|
||||
async execute(params: GetLeagueStatsUseCaseParams): Promise<Result<LeagueStatsViewModel, ApplicationErrorCode<'REPOSITORY_ERROR'>>> {
|
||||
try {
|
||||
const memberships = await this.leagueMembershipRepository.getLeagueMembers(params.leagueId);
|
||||
const races = await this.raceRepository.findByLeagueId(params.leagueId);
|
||||
const driverIds = memberships.map(m => m.driverId);
|
||||
const ratings = this.driverRatingProvider.getRatings(driverIds);
|
||||
const validRatings = Array.from(ratings.values()).filter(r => r !== null) as number[];
|
||||
const averageRating = validRatings.length > 0 ? Math.round(validRatings.reduce((sum, r) => sum + r, 0) / validRatings.length) : 0;
|
||||
const viewModel: LeagueStatsViewModel = {
|
||||
totalMembers: memberships.length,
|
||||
totalRaces: races.length,
|
||||
averageRating,
|
||||
};
|
||||
return Result.ok(viewModel);
|
||||
} catch {
|
||||
return Result.err({ code: 'REPOSITORY_ERROR', message: 'Failed to fetch league stats' });
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user