/** * 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'; export class InMemoryTeamRepository implements ITeamRepository { private teams: Map; constructor(seedData?: Team[]) { this.teams = new Map(); if (seedData) { seedData.forEach((team) => { this.teams.set(team.id, team); }); } } async findById(id: string): Promise { return this.teams.get(id) ?? null; } async findAll(): Promise { return Array.from(this.teams.values()); } async findByLeagueId(leagueId: string): Promise { return Array.from(this.teams.values()).filter((team) => team.leagues.includes(leagueId), ); } async create(team: Team): Promise { if (await this.exists(team.id)) { throw new Error(`Team with ID ${team.id} already exists`); } this.teams.set(team.id, team); return team; } async update(team: Team): Promise { if (!(await this.exists(team.id))) { throw new Error(`Team with ID ${team.id} not found`); } this.teams.set(team.id, team); return team; } async delete(id: string): Promise { if (!(await this.exists(id))) { throw new Error(`Team with ID ${id} not found`); } this.teams.delete(id); } async exists(id: string): Promise { return this.teams.has(id); } }