Files
gridpilot.gg/adapters/racing/persistence/inmemory/InMemoryLeagueRepository.ts
2025-12-15 18:49:10 +01:00

155 lines
4.8 KiB
TypeScript

/**
* Infrastructure Adapter: InMemoryLeagueRepository
*
* In-memory implementation of ILeagueRepository.
* Stores data in Map structure with UUID generation.
*/
import { v4 as uuidv4 } from 'uuid';
import { League } from '@gridpilot/racing/domain/entities/League';
import type { ILeagueRepository } from '@gridpilot/racing/domain/repositories/ILeagueRepository';
import type { ILogger } from '@gridpilot/shared/logging/ILogger';
export class InMemoryLeagueRepository implements ILeagueRepository {
private leagues: Map<string, League>;
private readonly logger: ILogger;
constructor(logger: ILogger, seedData?: League[]) {
this.logger = logger;
this.logger.info('InMemoryLeagueRepository initialized.');
this.leagues = new Map();
if (seedData) {
seedData.forEach(league => {
this.leagues.set(league.id, league);
this.logger.debug(`Seeded league: ${league.id}.`);
});
}
}
async findById(id: string): Promise<League | null> {
this.logger.debug(`Finding league by id: ${id}`);
try {
const league = this.leagues.get(id) ?? null;
if (league) {
this.logger.info(`Found league: ${id}.`);
} else {
this.logger.warn(`League with id ${id} not found.`);
}
return league;
} catch (error) {
this.logger.error(`Error finding league by id ${id}:`, error);
throw error;
}
}
async findAll(): Promise<League[]> {
this.logger.debug('Finding all leagues.');
try {
const leagues = Array.from(this.leagues.values());
this.logger.info(`Found ${leagues.length} leagues.`);
return leagues;
} catch (error) {
this.logger.error('Error finding all leagues:', error);
throw error;
}
}
async findByOwnerId(ownerId: string): Promise<League[]> {
this.logger.debug(`Finding leagues by owner id: ${ownerId}`);
try {
const leagues = Array.from(this.leagues.values()).filter(
league => league.ownerId === ownerId
);
this.logger.info(`Found ${leagues.length} leagues for owner id: ${ownerId}.`);
return leagues;
} catch (error) {
this.logger.error(`Error finding leagues by owner id ${ownerId}:`, error);
throw error;
}
}
async create(league: League): Promise<League> {
this.logger.debug(`Creating league: ${league.id}`);
try {
if (await this.exists(league.id)) {
this.logger.warn(`League with ID ${league.id} already exists.`);
throw new Error(`League with ID ${league.id} already exists`);
}
this.leagues.set(league.id, league);
this.logger.info(`League ${league.id} created successfully.`);
return league;
} catch (error) {
this.logger.error(`Error creating league ${league.id}:`, error);
throw error;
}
}
async update(league: League): Promise<League> {
this.logger.debug(`Updating league: ${league.id}`);
try {
if (!await this.exists(league.id)) {
this.logger.warn(`League with ID ${league.id} not found for update.`);
throw new Error(`League with ID ${league.id} not found`);
}
this.leagues.set(league.id, league);
this.logger.info(`League ${league.id} updated successfully.`);
return league;
} catch (error) {
this.logger.error(`Error updating league ${league.id}:`, error);
throw error;
}
}
async delete(id: string): Promise<void> {
this.logger.debug(`Deleting league: ${id}`);
try {
if (!await this.exists(id)) {
this.logger.warn(`League with ID ${id} not found for deletion.`);
throw new Error(`League with ID ${id} not found`);
}
this.leagues.delete(id);
this.logger.info(`League ${id} deleted successfully.`);
} catch (error) {
this.logger.error(`Error deleting league ${id}:`, error);
throw error;
}
}
async exists(id: string): Promise<boolean> {
this.logger.debug(`Checking existence of league with id: ${id}`);
try {
const exists = this.leagues.has(id);
this.logger.debug(`League ${id} exists: ${exists}.`);
return exists;
} catch (error) {
this.logger.error(`Error checking existence of league with id ${id}:`, error);
throw error;
}
}
async searchByName(query: string): Promise<League[]> {
this.logger.debug(`Searching leagues by name query: ${query}`);
try {
const normalizedQuery = query.toLowerCase();
const leagues = Array.from(this.leagues.values()).filter(league =>
league.name.toLowerCase().includes(normalizedQuery)
);
this.logger.info(`Found ${leagues.length} leagues matching search query: ${query}.`);
return leagues;
} catch (error) {
this.logger.error(`Error searching leagues by name query ${query}:`, error);
throw error;
}
}
/**
* Utility method to generate a new UUID
*/
static generateId(): string {
return uuidv4();
}
}