module creation

This commit is contained in:
2025-12-15 21:44:06 +01:00
parent b834f88bbd
commit 7c7267da72
88 changed files with 12119 additions and 4241 deletions

View File

@@ -1,155 +1,84 @@
/**
* 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';
import { ILeagueRepository } from '@gridpilot/core/racing/domain/repositories/ILeagueRepository';
import { League } from '@gridpilot/core/racing/domain/entities/League';
import { ILogger } from '@gridpilot/shared/logging/ILogger';
export class InMemoryLeagueRepository implements ILeagueRepository {
private leagues: Map<string, League>;
private readonly logger: ILogger;
private leagues: Map<string, League> = new Map();
constructor(logger: ILogger, seedData?: League[]) {
this.logger = logger;
constructor(private readonly logger: ILogger, initialLeagues: League[] = []) {
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}.`);
});
for (const league of initialLeagues) {
this.leagues.set(league.id, league);
this.logger.debug(`Seeded league: ${league.id} (${league.name}).`);
}
}
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;
this.logger.debug(`[InMemoryLeagueRepository] Finding league by ID: ${id}`);
const league = this.leagues.get(id) ?? null;
if (league) {
this.logger.info(`Found league by ID: ${id}.`);
} else {
this.logger.warn(`League with ID ${id} not found.`);
}
return Promise.resolve(league);
}
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;
}
this.logger.debug(`[InMemoryLeagueRepository] Finding leagues by owner ID: ${ownerId}`);
const ownedLeagues = Array.from(this.leagues.values()).filter(league => league.ownerId === ownerId);
this.logger.info(`Found ${ownedLeagues.length} leagues for owner ID: ${ownerId}.`);
return Promise.resolve(ownedLeagues);
}
async searchByName(name: string): Promise<League[]> {
this.logger.debug(`[InMemoryLeagueRepository] Searching leagues by name: ${name}`);
const matchingLeagues = Array.from(this.leagues.values()).filter(league =>
league.name.toLowerCase().includes(name.toLowerCase()),
);
this.logger.info(`Found ${matchingLeagues.length} matching leagues for name search: ${name}.`);
return Promise.resolve(matchingLeagues);
}
async findAll(): Promise<League[]> {
this.logger.debug('[InMemoryLeagueRepository] Finding all leagues.');
return Promise.resolve(Array.from(this.leagues.values()));
}
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;
this.logger.debug(`[InMemoryLeagueRepository] Creating league: ${league.id} (${league.name})`);
if (this.leagues.has(league.id)) {
this.logger.warn(`League with ID ${league.id} already exists.`);
throw new Error('League already exists');
}
this.leagues.set(league.id, league);
this.logger.info(`League ${league.id} created successfully.`);
return Promise.resolve(league);
}
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;
this.logger.debug(`[InMemoryLeagueRepository] Updating league: ${league.id} (${league.name})`);
if (!this.leagues.has(league.id)) {
this.logger.warn(`League with ID ${league.id} not found for update.`);
throw new Error('League not found');
}
this.leagues.set(league.id, league);
this.logger.info(`League ${league.id} updated successfully.`);
return Promise.resolve(league);
}
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.debug(`[InMemoryLeagueRepository] Deleting league with ID: ${id}`);
if (this.leagues.delete(id)) {
this.logger.info(`League ${id} deleted successfully.`);
} catch (error) {
this.logger.error(`Error deleting league ${id}:`, error);
throw error;
} else {
this.logger.warn(`League with ID ${id} not found for deletion.`);
}
return Promise.resolve();
}
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;
}
this.logger.debug(`[InMemoryLeagueRepository] Checking existence of league with ID: ${id}`);
return Promise.resolve(this.leagues.has(id));
}
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();
}
}
}