refactor adapters
This commit is contained in:
@@ -0,0 +1,121 @@
|
||||
/**
|
||||
* In-Memory Implementation: ILeagueWalletRepository
|
||||
*
|
||||
* Mock repository for testing and development
|
||||
*/
|
||||
|
||||
import type { LeagueWallet } from '../../domain/entities/LeagueWallet';
|
||||
import type { ILeagueWalletRepository } from '../../domain/repositories/ILeagueWalletRepository';
|
||||
import type { ILogger } from '@gridpilot/shared/logging/ILogger';
|
||||
|
||||
export class InMemoryLeagueWalletRepository implements ILeagueWalletRepository {
|
||||
private wallets: Map<string, LeagueWallet> = new Map();
|
||||
private readonly logger: ILogger;
|
||||
|
||||
constructor(logger: ILogger, seedData?: LeagueWallet[]) {
|
||||
this.logger = logger;
|
||||
this.logger.info('InMemoryLeagueWalletRepository initialized.');
|
||||
if (seedData) {
|
||||
seedData.forEach(wallet => this.wallets.set(wallet.id, wallet));
|
||||
this.logger.debug(`Seeded ${seedData.length} league wallets.`);
|
||||
}
|
||||
}
|
||||
|
||||
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);
|
||||
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 === 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);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
async create(wallet: LeagueWallet): Promise<LeagueWallet> {
|
||||
this.logger.debug(`Creating league wallet: ${wallet.id}`);
|
||||
try {
|
||||
if (this.wallets.has(wallet.id)) {
|
||||
this.logger.warn(`LeagueWallet with ID ${wallet.id} already exists.`);
|
||||
throw new Error('LeagueWallet with this ID already exists');
|
||||
}
|
||||
this.wallets.set(wallet.id, wallet);
|
||||
this.logger.info(`LeagueWallet ${wallet.id} created successfully.`);
|
||||
return wallet;
|
||||
} catch (error) {
|
||||
this.logger.error(`Error creating league wallet ${wallet.id}:`, error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
async update(wallet: LeagueWallet): Promise<LeagueWallet> {
|
||||
this.logger.debug(`Updating league wallet: ${wallet.id}`);
|
||||
try {
|
||||
if (!this.wallets.has(wallet.id)) {
|
||||
this.logger.warn(`LeagueWallet with ID ${wallet.id} not found for update.`);
|
||||
throw new Error('LeagueWallet not found');
|
||||
}
|
||||
this.wallets.set(wallet.id, wallet);
|
||||
this.logger.info(`LeagueWallet ${wallet.id} updated successfully.`);
|
||||
return wallet;
|
||||
} catch (error) {
|
||||
this.logger.error(`Error updating league wallet ${wallet.id}:`, 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);
|
||||
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);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
// Test helper
|
||||
clear(): void {
|
||||
this.logger.debug('Clearing all league wallets.');
|
||||
this.wallets.clear();
|
||||
this.logger.info('All league wallets cleared.');
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user