import type { IRaceRepository } from '../../domain/repositories/IRaceRepository'; import type { ILeagueRepository } from '../../domain/repositories/ILeagueRepository'; import { Result } from '@core/shared/application/Result'; import type { ApplicationErrorCode } from '@core/shared/errors/ApplicationErrorCode'; import type { Logger } from '@core/shared/application'; import type { Race } from '../../domain/entities/Race'; import type { League } from '../../domain/entities/League'; 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, ) {} async execute( _input: GetAllRacesInput, ): Promise>> { 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' }, }); } } }