Files
gridpilot.gg/packages/racing/infrastructure/repositories/InMemoryGameRepository.ts
2025-12-10 12:38:55 +01:00

61 lines
1.6 KiB
TypeScript

/**
* Infrastructure Adapter: InMemoryGameRepository
*
* In-memory implementation of IGameRepository.
*/
import { Game } from '../../domain/entities/Game';
import type { IGameRepository } from '../../domain/repositories/IGameRepository';
export class InMemoryGameRepository implements IGameRepository {
private games: Map<string, Game>;
constructor(seedData?: Game[]) {
this.games = new Map();
if (seedData) {
seedData.forEach(game => {
this.games.set(game.id, game);
});
} else {
// Default seed data for common sim racing games
const defaultGames = [
Game.create({ id: 'iracing', name: 'iRacing' }),
Game.create({ id: 'acc', name: 'Assetto Corsa Competizione' }),
Game.create({ id: 'ac', name: 'Assetto Corsa' }),
Game.create({ id: 'rf2', name: 'rFactor 2' }),
Game.create({ id: 'ams2', name: 'Automobilista 2' }),
Game.create({ id: 'lmu', name: 'Le Mans Ultimate' }),
];
defaultGames.forEach(game => {
this.games.set(game.id, game);
});
}
}
async findById(id: string): Promise<Game | null> {
return this.games.get(id) ?? null;
}
async findAll(): Promise<Game[]> {
return Array.from(this.games.values()).sort((a, b) => a.name.localeCompare(b.name));
}
/**
* Utility method to add a game
*/
async create(game: Game): Promise<Game> {
if (this.games.has(game.id)) {
throw new Error(`Game with ID ${game.id} already exists`);
}
this.games.set(game.id, game);
return game;
}
/**
* Test helper to clear data
*/
clear(): void {
this.games.clear();
}
}