Files
gridpilot.gg/packages/racing/infrastructure/repositories/InMemoryTeamRepository.ts
2025-12-04 23:31:55 +01:00

67 lines
1.6 KiB
TypeScript

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