Files
gridpilot.gg/adapters/racing/persistence/inmemory/InMemoryLeagueWalletRepository.ts
2025-12-17 12:05:00 +01:00

117 lines
4.3 KiB
TypeScript

/**
* In-Memory Implementation: ILeagueWalletRepository
*
* Mock repository for testing and development
*/
import { LeagueWallet } from '@core/racing/domain/entities/league-wallet/LeagueWallet';
import type { ILeagueWalletRepository } from '@core/racing/domain/repositories/ILeagueWalletRepository';
import type { Logger } from '@core/shared/application';
export class InMemoryLeagueWalletRepository implements ILeagueWalletRepository {
private wallets: Map<string, LeagueWallet> = new Map();
private readonly logger: Logger;
constructor(logger: Logger) {
this.logger = logger;
this.logger.info('InMemoryLeagueWalletRepository initialized.');
}
async findById(id: string): Promise<LeagueWallet | null> {
this.logger.debug(`Finding league wallet by id: ${id}`);
try {
const wallet = this.wallets.get(id) ?? null;
if (wallet) {
this.logger.info(`Found league wallet: ${id}.`);
} else {
this.logger.warn(`League wallet with id ${id} not found.`);
}
return wallet;
} catch (error) {
this.logger.error(`Error finding league wallet by id ${id}:`, error instanceof Error ? error : new Error(String(error)));
throw error;
}
}
async findByLeagueId(leagueId: string): Promise<LeagueWallet | null> {
this.logger.debug(`Finding league wallet by league id: ${leagueId}`);
try {
for (const wallet of this.wallets.values()) {
if (wallet.leagueId.toString() === leagueId) {
this.logger.info(`Found league wallet for league id: ${leagueId}.`);
return wallet;
}
}
this.logger.warn(`No league wallet found for league id: ${leagueId}.`);
return null;
} catch (error) {
this.logger.error(`Error finding league wallet by league id ${leagueId}:`, error instanceof Error ? error : new Error(String(error)));
throw error;
}
}
async create(wallet: LeagueWallet): Promise<LeagueWallet> {
this.logger.debug(`Creating league wallet: ${wallet.id}`);
try {
if (this.wallets.has(wallet.id.toString())) {
this.logger.warn(`LeagueWallet with ID ${wallet.id} already exists.`);
throw new Error('LeagueWallet with this ID already exists');
}
this.wallets.set(wallet.id.toString(), wallet);
this.logger.info(`LeagueWallet ${wallet.id} created successfully.`);
return wallet;
} catch (error) {
this.logger.error(`Error creating league wallet ${wallet.id}:`, error instanceof Error ? error : new Error(String(error)));
throw error;
}
}
async update(wallet: LeagueWallet): Promise<LeagueWallet> {
this.logger.debug(`Updating league wallet: ${wallet.id}`);
try {
if (!this.wallets.has(wallet.id.toString())) {
this.logger.warn(`LeagueWallet with ID ${wallet.id} not found for update.`);
throw new Error('LeagueWallet not found');
}
this.wallets.set(wallet.id.toString(), wallet);
this.logger.info(`LeagueWallet ${wallet.id} updated successfully.`);
return wallet;
} catch (error) {
this.logger.error(`Error updating league wallet ${wallet.id}:`, error instanceof Error ? error : new Error(String(error)));
throw error;
}
}
async delete(id: string): Promise<void> {
this.logger.debug(`Deleting league wallet: ${id}`);
try {
if (this.wallets.delete(id)) {
this.logger.info(`LeagueWallet ${id} deleted successfully.`);
} else {
this.logger.warn(`LeagueWallet with id ${id} not found for deletion.`);
}
} catch (error) {
this.logger.error(`Error deleting league wallet ${id}:`, error instanceof Error ? error : new Error(String(error)));
throw error;
}
}
async exists(id: string): Promise<boolean> {
this.logger.debug(`Checking existence of league wallet with id: ${id}`);
try {
const exists = this.wallets.has(id);
this.logger.debug(`LeagueWallet ${id} exists: ${exists}.`);
return exists;
} catch (error) {
this.logger.error(`Error checking existence of league wallet with id ${id}:`, error instanceof Error ? error : new Error(String(error)));
throw error;
}
}
// Test helper
clear(): void {
this.logger.debug('Clearing all league wallets.');
this.wallets.clear();
this.logger.info('All league wallets cleared.');
}
}