/** * Infrastructure Adapter: InMemoryTeamRepository * * In-memory implementation of ITeamRepository. * Stores data in a Map structure. */ import type { Team } from '@gridpilot/racing/domain/entities/Team'; import type { ITeamRepository } from '@gridpilot/racing/domain/repositories/ITeamRepository'; import type { ILogger } from '@gridpilot/shared/logging/ILogger'; export class InMemoryTeamRepository implements ITeamRepository { private teams: Map; private readonly logger: ILogger; constructor(logger: ILogger, seedData?: Team[]) { this.logger = logger; this.logger.info('InMemoryTeamRepository initialized.'); this.teams = new Map(); if (seedData) { seedData.forEach((team) => { this.teams.set(team.id, team); this.logger.debug(`Seeded team: ${team.id}.`); }); } } async findById(id: string): Promise { this.logger.debug(`Finding team by id: ${id}`); try { const team = this.teams.get(id) ?? null; if (team) { this.logger.info(`Found team: ${id}.`); } else { this.logger.warn(`Team with id ${id} not found.`); } return team; } catch (error) { this.logger.error(`Error finding team by id ${id}:`, error); throw error; } } async findAll(): Promise { this.logger.debug('Finding all teams.'); try { const teams = Array.from(this.teams.values()); this.logger.info(`Found ${teams.length} teams.`); return teams; } catch (error) { this.logger.error('Error finding all teams:', error); throw error; } } async findByLeagueId(leagueId: string): Promise { this.logger.debug(`Finding teams by league id: ${leagueId}`); try { const teams = Array.from(this.teams.values()).filter((team) => team.leagues.includes(leagueId), ); this.logger.info(`Found ${teams.length} teams for league id: ${leagueId}.`); return teams; } catch (error) { this.logger.error(`Error finding teams by league id ${leagueId}:`, error); throw error; } } async create(team: Team): Promise { this.logger.debug(`Creating team: ${team.id}`); try { if (await this.exists(team.id)) { this.logger.warn(`Team with ID ${team.id} already exists.`); throw new Error(`Team with ID ${team.id} already exists`); } this.teams.set(team.id, team); this.logger.info(`Team ${team.id} created successfully.`); return team; } catch (error) { this.logger.error(`Error creating team ${team.id}:`, error); throw error; } } async update(team: Team): Promise { this.logger.debug(`Updating team: ${team.id}`); try { if (!(await this.exists(team.id))) { this.logger.warn(`Team with ID ${team.id} not found for update.`); throw new Error(`Team with ID ${team.id} not found`); } this.teams.set(team.id, team); this.logger.info(`Team ${team.id} updated successfully.`); return team; } catch (error) { this.logger.error(`Error updating team ${team.id}:`, error); throw error; } } async delete(id: string): Promise { this.logger.debug(`Deleting team: ${id}`); try { if (!(await this.exists(id))) { this.logger.warn(`Team with ID ${id} not found for deletion.`); throw new Error(`Team with ID ${id} not found`); } this.teams.delete(id); this.logger.info(`Team ${id} deleted successfully.`); } catch (error) { this.logger.error(`Error deleting team ${id}:`, error); throw error; } } async exists(id: string): Promise { this.logger.debug(`Checking existence of team with id: ${id}`); try { const exists = this.teams.has(id); this.logger.debug(`Team ${id} exists: ${exists}.`); return exists; } catch (error) { this.logger.error(`Error checking existence of team with id ${id}:`, error); throw error; } } }