This commit is contained in:
2025-12-14 18:11:59 +01:00
parent acc15e8d8d
commit 217337862c
91 changed files with 5919 additions and 1999 deletions

View File

@@ -6,16 +6,21 @@
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(seedData?: Game[]) {
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
@@ -29,33 +34,64 @@ export class InMemoryGameRepository implements IGameRepository {
];
defaultGames.forEach(game => {
this.games.set(game.id, game);
this.logger.debug(`Seeded default game: ${game.id}.`);
});
}
}
async findById(id: string): Promise<Game | null> {
return this.games.get(id) ?? 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[]> {
return Array.from(this.games.values()).sort((a, b) => a.name.localeCompare(b.name));
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> {
if (this.games.has(game.id)) {
throw new Error(`Game with ID ${game.id} already exists`);
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;
}
this.games.set(game.id, game);
return game;
}
/**
* Test helper to clear data
*/
clear(): void {
this.logger.debug('Clearing all games.');
this.games.clear();
this.logger.info('All games cleared.');
}
}