Files
gridpilot.gg/core/racing/application/use-cases/GetAllRacesUseCase.ts
2026-01-16 19:46:49 +01:00

54 lines
1.7 KiB
TypeScript

import type { Logger } from '@core/shared/domain/Logger';
import { Result } from '@core/shared/domain/Result';
import type { ApplicationErrorCode } from '@core/shared/errors/ApplicationErrorCode';
import type { League } from '../../domain/entities/League';
import type { Race } from '../../domain/entities/Race';
import { LeagueRepository } from '../../domain/repositories/LeagueRepository';
import { RaceRepository } from '../../domain/repositories/RaceRepository';
export type GetAllRacesInput = {};
export interface GetAllRacesResult {
races: Race[];
leagues: League[];
totalCount: number;
}
export type GetAllRacesErrorCode = 'REPOSITORY_ERROR';
export class GetAllRacesUseCase {
constructor(
private readonly raceRepository: RaceRepository,
private readonly leagueRepository: LeagueRepository,
private readonly logger: Logger,
) {}
async execute(
_input: GetAllRacesInput,
): Promise<Result<GetAllRacesResult, ApplicationErrorCode<GetAllRacesErrorCode, { message: string }>>> {
void _input;
this.logger.debug('Executing GetAllRacesUseCase');
try {
const races = await this.raceRepository.findAll();
const leagues = await this.leagueRepository.findAll();
const result: GetAllRacesResult = {
races,
leagues,
totalCount: races.length,
};
this.logger.debug('Successfully retrieved all races.');
return Result.ok(result);
} catch (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' },
});
}
}
}