refactor racing use cases
This commit is contained in:
@@ -1,46 +1,56 @@
|
||||
import type { IRaceRepository } from '../../domain/repositories/IRaceRepository';
|
||||
import type { ILeagueRepository } from '../../domain/repositories/ILeagueRepository';
|
||||
import type { GetAllRacesOutputPort } from '../ports/output/GetAllRacesOutputPort';
|
||||
import type { AsyncUseCase, Logger } from '@core/shared/application';
|
||||
import { Result } from '@core/shared/application/Result';
|
||||
import type { ApplicationErrorCode } from '@core/shared/errors/ApplicationErrorCode';
|
||||
import type { Logger } from '@core/shared/application';
|
||||
import type { UseCaseOutputPort } from '@core/shared/application/UseCaseOutputPort';
|
||||
import type { Race } from '../../domain/entities/Race';
|
||||
import type { League } from '../../domain/entities/League';
|
||||
|
||||
export class GetAllRacesUseCase implements AsyncUseCase<void, GetAllRacesOutputPort, 'REPOSITORY_ERROR'> {
|
||||
export type GetAllRacesInput = {};
|
||||
|
||||
export interface GetAllRacesResult {
|
||||
races: Race[];
|
||||
leagues: League[];
|
||||
totalCount: number;
|
||||
}
|
||||
|
||||
export type GetAllRacesErrorCode = 'REPOSITORY_ERROR';
|
||||
|
||||
export class GetAllRacesUseCase {
|
||||
constructor(
|
||||
private readonly raceRepository: IRaceRepository,
|
||||
private readonly leagueRepository: ILeagueRepository,
|
||||
private readonly logger: Logger,
|
||||
private readonly output: UseCaseOutputPort<GetAllRacesResult>,
|
||||
) {}
|
||||
|
||||
async execute(): Promise<Result<GetAllRacesOutputPort, ApplicationErrorCode<'REPOSITORY_ERROR', { message: string }>>> {
|
||||
async execute(
|
||||
_input: GetAllRacesInput,
|
||||
): Promise<Result<void, ApplicationErrorCode<GetAllRacesErrorCode, { message: string }>>> {
|
||||
this.logger.debug('Executing GetAllRacesUseCase');
|
||||
try {
|
||||
const races = await this.raceRepository.findAll();
|
||||
const leagues = await this.leagueRepository.findAll();
|
||||
const leagueMap = new Map(leagues.map(league => [league.id, league.name]));
|
||||
|
||||
const output: GetAllRacesOutputPort = {
|
||||
races: races.map(race => ({
|
||||
id: race.id,
|
||||
leagueId: race.leagueId,
|
||||
track: race.track,
|
||||
car: race.car,
|
||||
status: race.status as 'scheduled' | 'running' | 'completed' | 'cancelled',
|
||||
scheduledAt: race.scheduledAt.toISOString(),
|
||||
strengthOfField: race.strengthOfField || null,
|
||||
leagueName: (leagueMap.get(race.leagueId) || 'Unknown League').toString(),
|
||||
})),
|
||||
const result: GetAllRacesResult = {
|
||||
races,
|
||||
leagues,
|
||||
totalCount: races.length,
|
||||
};
|
||||
|
||||
this.logger.debug('Successfully retrieved all races.');
|
||||
return Result.ok(output);
|
||||
this.output.present(result);
|
||||
return Result.ok(undefined);
|
||||
} catch (error) {
|
||||
this.logger.error('Error executing GetAllRacesUseCase', error instanceof Error ? error : new Error(String(error)));
|
||||
this.logger.error(
|
||||
'Error executing GetAllRacesUseCase',
|
||||
error instanceof Error ? error : new Error(String(error)),
|
||||
);
|
||||
return Result.err({
|
||||
code: 'REPOSITORY_ERROR',
|
||||
details: { message: error instanceof Error ? error.message : 'Unknown error occurred' },
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user