97 lines
2.9 KiB
TypeScript
97 lines
2.9 KiB
TypeScript
/**
|
|
* Infrastructure Adapter: InMemoryGameRepository
|
|
*
|
|
* In-memory implementation of IGameRepository.
|
|
*/
|
|
|
|
import { Game } from '../../domain/entities/Game';
|
|
import type { IGameRepository } from '../../domain/repositories/IGameRepository';
|
|
import type { ILogger } from '@gridpilot/shared/logging/ILogger';
|
|
|
|
export class InMemoryGameRepository implements IGameRepository {
|
|
private games: Map<string, Game>;
|
|
private readonly logger: ILogger;
|
|
|
|
constructor(logger: ILogger, seedData?: Game[]) {
|
|
this.logger = logger;
|
|
this.logger.info('InMemoryGameRepository initialized.');
|
|
this.games = new Map();
|
|
|
|
if (seedData) {
|
|
seedData.forEach(game => {
|
|
this.games.set(game.id, game);
|
|
this.logger.debug(`Seeded game: ${game.id}.`);
|
|
});
|
|
} 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);
|
|
this.logger.debug(`Seeded default game: ${game.id}.`);
|
|
});
|
|
}
|
|
}
|
|
|
|
async findById(id: string): Promise<Game | null> {
|
|
this.logger.debug(`Finding game by id: ${id}`);
|
|
try {
|
|
const game = this.games.get(id) ?? null;
|
|
if (game) {
|
|
this.logger.info(`Found game: ${id}.`);
|
|
} else {
|
|
this.logger.warn(`Game with id ${id} not found.`);
|
|
}
|
|
return game;
|
|
} catch (error) {
|
|
this.logger.error(`Error finding game by id ${id}:`, error);
|
|
throw error;
|
|
}
|
|
}
|
|
|
|
async findAll(): Promise<Game[]> {
|
|
this.logger.debug('Finding all games.');
|
|
try {
|
|
const games = Array.from(this.games.values()).sort((a, b) => a.name.localeCompare(b.name));
|
|
this.logger.info(`Found ${games.length} games.`);
|
|
return games;
|
|
} catch (error) {
|
|
this.logger.error('Error finding all games:', error);
|
|
throw error;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Utility method to add a game
|
|
*/
|
|
async create(game: Game): Promise<Game> {
|
|
this.logger.debug(`Creating game: ${game.id}`);
|
|
try {
|
|
if (this.games.has(game.id)) {
|
|
this.logger.warn(`Game with ID ${game.id} already exists.`);
|
|
throw new Error(`Game with ID ${game.id} already exists`);
|
|
}
|
|
this.games.set(game.id, game);
|
|
this.logger.info(`Game ${game.id} created successfully.`);
|
|
return game;
|
|
} catch (error) {
|
|
this.logger.error(`Error creating game ${game.id}:`, error);
|
|
throw error;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Test helper to clear data
|
|
*/
|
|
clear(): void {
|
|
this.logger.debug('Clearing all games.');
|
|
this.games.clear();
|
|
this.logger.info('All games cleared.');
|
|
}
|
|
} |