league service
This commit is contained in:
@@ -0,0 +1,58 @@
|
||||
import type { IRaceRepository } from '../../domain/repositories/IRaceRepository';
|
||||
import type { IProtestRepository } from '../../domain/repositories/IProtestRepository';
|
||||
import type { IDriverRepository } from '../../domain/repositories/IDriverRepository';
|
||||
import type { IGetLeagueProtestsPresenter, GetLeagueProtestsResultDTO, GetLeagueProtestsViewModel } from '../presenters/IGetLeagueProtestsPresenter';
|
||||
import type { UseCase } from '@gridpilot/shared/application/UseCase';
|
||||
|
||||
export interface GetLeagueProtestsUseCaseParams {
|
||||
leagueId: string;
|
||||
}
|
||||
|
||||
export interface GetLeagueProtestsResultDTO {
|
||||
protests: any[];
|
||||
races: any[];
|
||||
drivers: { id: string; name: string }[];
|
||||
}
|
||||
|
||||
export class GetLeagueProtestsUseCase implements UseCase<GetLeagueProtestsUseCaseParams, GetLeagueProtestsResultDTO, GetLeagueProtestsViewModel, IGetLeagueProtestsPresenter> {
|
||||
constructor(
|
||||
private readonly raceRepository: IRaceRepository,
|
||||
private readonly protestRepository: IProtestRepository,
|
||||
private readonly driverRepository: IDriverRepository,
|
||||
) {}
|
||||
|
||||
async execute(params: GetLeagueProtestsUseCaseParams, presenter: IGetLeagueProtestsPresenter): Promise<void> {
|
||||
const races = await this.raceRepository.findByLeagueId(params.leagueId);
|
||||
const protests = [];
|
||||
const raceMap = new Map();
|
||||
const driverIds = new Set<string>();
|
||||
|
||||
for (const race of races) {
|
||||
raceMap.set(race.id, { id: race.id, name: race.name, date: race.scheduledAt.toISOString() });
|
||||
const raceProtests = await this.protestRepository.findByRaceId(race.id);
|
||||
for (const protest of raceProtests) {
|
||||
protests.push({
|
||||
id: protest.id,
|
||||
raceId: protest.raceId,
|
||||
protestingDriverId: protest.protestingDriverId,
|
||||
accusedDriverId: protest.accusedDriverId,
|
||||
submittedAt: protest.filedAt,
|
||||
description: protest.comment || '',
|
||||
status: protest.status,
|
||||
});
|
||||
driverIds.add(protest.protestingDriverId);
|
||||
driverIds.add(protest.accusedDriverId);
|
||||
}
|
||||
}
|
||||
|
||||
const drivers = await this.driverRepository.findByIds(Array.from(driverIds));
|
||||
const driverMap = new Map(drivers.map(d => [d.id, { id: d.id, name: d.name }]));
|
||||
const dto: GetLeagueProtestsResultDTO = {
|
||||
protests,
|
||||
races: Array.from(raceMap.values()),
|
||||
drivers: Array.from(driverMap.values()),
|
||||
};
|
||||
presenter.reset();
|
||||
presenter.present(dto);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user