/** * Infrastructure Adapter: InMemoryRaceRepository * * In-memory implementation of IRaceRepository. * Stores data in Map structure with UUID generation. */ import { v4 as uuidv4 } from 'uuid'; import { Race, RaceStatus } from '@gridpilot/racing/domain/entities/Race'; import type { IRaceRepository } from '@gridpilot/racing/domain/repositories/IRaceRepository'; import type { ILogger } from '@gridpilot/shared/logging/ILogger'; export class InMemoryRaceRepository implements IRaceRepository { private races: Map; private readonly logger: ILogger; constructor(logger: ILogger, seedData?: Race[]) { this.logger = logger; this.logger.info('InMemoryRaceRepository initialized.'); this.races = new Map(); if (seedData) { seedData.forEach(race => { this.races.set(race.id, race); this.logger.debug(`Seeded race: ${race.id}.`); }); } } async findById(id: string): Promise { this.logger.debug(`Finding race by id: ${id}`); try { const race = this.races.get(id) ?? null; if (race) { this.logger.info(`Found race: ${id}.`); } else { this.logger.warn(`Race with id ${id} not found.`); } return race; } catch (error) { this.logger.error(`Error finding race by id ${id}:`, error); throw error; } } async findAll(): Promise { this.logger.debug('Finding all races.'); try { const races = Array.from(this.races.values()); this.logger.info(`Found ${races.length} races.`); return races; } catch (error) { this.logger.error('Error finding all races:', error); throw error; } } async findByLeagueId(leagueId: string): Promise { this.logger.debug(`Finding races by league id: ${leagueId}`); try { const races = Array.from(this.races.values()) .filter(race => race.leagueId === leagueId) .sort((a, b) => a.scheduledAt.getTime() - b.scheduledAt.getTime()); this.logger.info(`Found ${races.length} races for league id: ${leagueId}.`); return races; } catch (error) { this.logger.error(`Error finding races by league id ${leagueId}:`, error); throw error; } } async findUpcomingByLeagueId(leagueId: string): Promise { this.logger.debug(`Finding upcoming races by league id: ${leagueId}`); try { const now = new Date(); const races = Array.from(this.races.values()) .filter(race => race.leagueId === leagueId && race.status === 'scheduled' && race.scheduledAt > now ) .sort((a, b) => a.scheduledAt.getTime() - b.scheduledAt.getTime()); this.logger.info(`Found ${races.length} upcoming races for league id: ${leagueId}.`); return races; } catch (error) { this.logger.error(`Error finding upcoming races by league id ${leagueId}:`, error); throw error; } } async findCompletedByLeagueId(leagueId: string): Promise { this.logger.debug(`Finding completed races by league id: ${leagueId}`); try { const races = Array.from(this.races.values()) .filter(race => race.leagueId === leagueId && race.status === 'completed' ) .sort((a, b) => b.scheduledAt.getTime() - a.scheduledAt.getTime()); this.logger.info(`Found ${races.length} completed races for league id: ${leagueId}.`); return races; } catch (error) { this.logger.error(`Error finding completed races by league id ${leagueId}:`, error); throw error; } } async findByStatus(status: RaceStatus): Promise { this.logger.debug(`Finding races by status: ${status}`); try { const races = Array.from(this.races.values()) .filter(race => race.status === status) .sort((a, b) => a.scheduledAt.getTime() - b.scheduledAt.getTime()); this.logger.info(`Found ${races.length} races with status: ${status}.`); return races; } catch (error) { this.logger.error(`Error finding races by status ${status}:`, error); throw error; } } async findByDateRange(startDate: Date, endDate: Date): Promise { this.logger.debug(`Finding races by date range: ${startDate.toISOString()} - ${endDate.toISOString()}`); try { const races = Array.from(this.races.values()) .filter(race => race.scheduledAt >= startDate && race.scheduledAt <= endDate ) .sort((a, b) => a.scheduledAt.getTime() - b.scheduledAt.getTime()); this.logger.info(`Found ${races.length} races in date range.`); return races; } catch (error) { this.logger.error(`Error finding races by date range:`, error); throw error; } } async create(race: Race): Promise { this.logger.debug(`Creating race: ${race.id}`); try { if (await this.exists(race.id)) { this.logger.warn(`Race with ID ${race.id} already exists.`); throw new Error(`Race with ID ${race.id} already exists`); } this.races.set(race.id, race); this.logger.info(`Race ${race.id} created successfully.`); return race; } catch (error) { this.logger.error(`Error creating race ${race.id}:`, error); throw error; } } async update(race: Race): Promise { this.logger.debug(`Updating race: ${race.id}`); try { if (!await this.exists(race.id)) { this.logger.warn(`Race with ID ${race.id} not found for update.`); throw new Error(`Race with ID ${race.id} not found`); } this.races.set(race.id, race); this.logger.info(`Race ${race.id} updated successfully.`); return race; } catch (error) { this.logger.error(`Error updating race ${race.id}:`, error); throw error; } } async delete(id: string): Promise { this.logger.debug(`Deleting race: ${id}`); try { if (!await this.exists(id)) { this.logger.warn(`Race with ID ${id} not found for deletion.`); throw new Error(`Race with ID ${id} not found`); } this.races.delete(id); this.logger.info(`Race ${id} deleted successfully.`); } catch (error) { this.logger.error(`Error deleting race ${id}:`, error); throw error; } } async exists(id: string): Promise { this.logger.debug(`Checking existence of race with id: ${id}`); try { const exists = this.races.has(id); this.logger.debug(`Race ${id} exists: ${exists}.`); return exists; } catch (error) { this.logger.error(`Error checking existence of race with id ${id}:`, error); throw error; } } /** * Utility method to generate a new UUID */ static generateId(): string { return uuidv4(); } }