import { ILeagueRepository } from '@core/racing/domain/repositories/ILeagueRepository'; import { League } from '@core/racing/domain/entities/League'; import { Logger } from '@core/shared/application'; export class InMemoryLeagueRepository implements ILeagueRepository { private leagues: Map = new Map(); private readonly logger: Logger; constructor(logger: Logger) { this.logger = logger; this.leagues = new Map(); this.logger.info('InMemoryLeagueRepository initialized'); } async findById(id: string): Promise { this.logger.debug(`Attempting to find league with ID: ${id}.`); try { const league = this.leagues.get(id) ?? null; if (league) { this.logger.info(`Successfully found league with ID: ${id}.`); } else { this.logger.warn(`League with ID: ${id} not found.`); } return league; } catch (error) { this.logger.error(`Error finding league by ID ${id}:`, error instanceof Error ? error : new Error(String(error))); throw error; } } async findByOwnerId(ownerId: string): Promise { this.logger.debug(`Attempting to find leagues by owner ID: ${ownerId}.`); try { const ownedLeagues = Array.from(this.leagues.values()).filter(league => league.ownerId.toString() === ownerId); this.logger.info(`Successfully found ${ownedLeagues.length} leagues for owner ID: ${ownerId}.`); return ownedLeagues; } catch (error) { this.logger.error(`Error finding leagues by owner ID ${ownerId}:`, error instanceof Error ? error : new Error(String(error))); throw error; } } async searchByName(name: string): Promise { this.logger.debug(`Attempting to search leagues by name query: ${name}.`); try { const matchingLeagues = Array.from(this.leagues.values()).filter(league => league.name.toString().toLowerCase().includes(name.toLowerCase()), ); this.logger.info(`Successfully found ${matchingLeagues.length} leagues matching search query: ${name}.`); return matchingLeagues; } catch (error) { this.logger.error(`Error searching leagues by name query ${name}:`, error instanceof Error ? error : new Error(String(error))); throw error; } } async findAll(): Promise { this.logger.debug('Attempting to find all leagues.'); try { const leagues = Array.from(this.leagues.values()); this.logger.info(`Successfully found ${leagues.length} leagues.`); return leagues; } catch (error) { this.logger.error('Error finding all leagues:', error instanceof Error ? error : new Error(String(error))); throw error; } } async create(league: League): Promise { this.logger.debug(`Attempting to create league: ${league.id}.`); try { if (await this.exists(league.id.toString())) { this.logger.warn(`League with ID ${league.id} already exists; creation aborted.`); throw new Error(`League with ID ${league.id} already exists`); } this.leagues.set(league.id.toString(), league); this.logger.info(`League ${league.id} created successfully.`); return league; } catch (error) { this.logger.error(`Error creating league ${league.id}:`, error instanceof Error ? error : new Error(String(error))); throw error; } } async update(league: League): Promise { this.logger.debug(`Attempting to update league with ID: ${league.id}.`); try { if (!(await this.exists(league.id.toString()))) { this.logger.warn(`League with ID ${league.id} not found for update; update aborted.`); throw new Error(`League with ID ${league.id} not found`); } this.leagues.set(league.id.toString(), league); this.logger.info(`League ${league.id} updated successfully.`); return league; } catch (error) { this.logger.error(`Error updating league ${league.id}:`, error instanceof Error ? error : new Error(String(error))); throw error; } } async delete(id: string): Promise { this.logger.debug(`Attempting to delete league with ID: ${id}.`); try { if (!(await this.exists(id))) { this.logger.warn(`League with ID ${id} not found for deletion; deletion aborted.`); throw new Error(`League with ID ${id} not found`); } this.leagues.delete(id); this.logger.info(`League ${id} deleted successfully.`); } catch (error) { this.logger.error(`Error deleting league ${id}:`, error instanceof Error ? error : new Error(String(error))); throw error; } } async exists(id: string): Promise { this.logger.debug(`Checking existence of league with ID: ${id}.`); try { const exists = this.leagues.has(id); this.logger.info(`League with ID ${id} existence check: ${exists}.`); return exists; } catch (error) { this.logger.error(`Error checking existence of league with ID ${id}:`, error instanceof Error ? error : new Error(String(error))); throw error; } } }