/** * In-Memory Implementation: ISponsorRepository * * Mock repository for testing and development */ import type { Sponsor } from '../../domain/entities/Sponsor'; import type { ISponsorRepository } from '../../domain/repositories/ISponsorRepository'; import type { ILogger } from '@gridpilot/shared/logging/ILogger'; export class InMemorySponsorRepository implements ISponsorRepository { private sponsors: Map = new Map(); private readonly logger: ILogger; constructor(logger: ILogger, seedData?: Sponsor[]) { this.logger = logger; this.logger.info('InMemorySponsorRepository initialized.'); if (seedData) { this.seed(seedData); } } async findById(id: string): Promise { this.logger.debug(`Finding sponsor by id: ${id}`); try { const sponsor = this.sponsors.get(id) ?? null; if (sponsor) { this.logger.info(`Found sponsor: ${id}.`); } else { this.logger.warn(`Sponsor with id ${id} not found.`); } return sponsor; } catch (error) { this.logger.error(`Error finding sponsor by id ${id}:`, error); throw error; } } async findAll(): Promise { this.logger.debug('Finding all sponsors.'); try { const sponsors = Array.from(this.sponsors.values()); this.logger.info(`Found ${sponsors.length} sponsors.`); return sponsors; } catch (error) { this.logger.error('Error finding all sponsors:', error); throw error; } } async findByEmail(email: string): Promise { this.logger.debug(`Finding sponsor by email: ${email}`); try { for (const sponsor of this.sponsors.values()) { if (sponsor.contactEmail === email) { this.logger.info(`Found sponsor with email: ${email}.`); return sponsor; } } this.logger.warn(`Sponsor with email ${email} not found.`); return null; } catch (error) { this.logger.error(`Error finding sponsor by email ${email}:`, error); throw error; } } async create(sponsor: Sponsor): Promise { this.logger.debug(`Creating sponsor: ${sponsor.id}`); try { if (this.sponsors.has(sponsor.id)) { this.logger.warn(`Sponsor with ID ${sponsor.id} already exists.`); throw new Error('Sponsor with this ID already exists'); } this.sponsors.set(sponsor.id, sponsor); this.logger.info(`Sponsor ${sponsor.id} created successfully.`); return sponsor; } catch (error) { this.logger.error(`Error creating sponsor ${sponsor.id}:`, error); throw error; } } async update(sponsor: Sponsor): Promise { this.logger.debug(`Updating sponsor: ${sponsor.id}`); try { if (!this.sponsors.has(sponsor.id)) { this.logger.warn(`Sponsor with ID ${sponsor.id} not found for update.`); throw new Error('Sponsor not found'); } this.sponsors.set(sponsor.id, sponsor); this.logger.info(`Sponsor ${sponsor.id} updated successfully.`); return sponsor; } catch (error) { this.logger.error(`Error updating sponsor ${sponsor.id}:`, error); throw error; } } async delete(id: string): Promise { this.logger.debug(`Deleting sponsor: ${id}`); try { if (this.sponsors.delete(id)) { this.logger.info(`Sponsor ${id} deleted successfully.`); } else { this.logger.warn(`Sponsor with id ${id} not found for deletion.`); } } catch (error) { this.logger.error(`Error deleting sponsor ${id}:`, error); throw error; } } async exists(id: string): Promise { this.logger.debug(`Checking existence of sponsor with id: ${id}`); try { const exists = this.sponsors.has(id); this.logger.debug(`Sponsor ${id} exists: ${exists}.`); return exists; } catch (error) { this.logger.error(`Error checking existence of sponsor with id ${id}:`, error); throw error; } } /** * Seed initial data */ seed(sponsors: Sponsor[]): void { this.logger.debug(`Seeding ${sponsors.length} sponsors.`); try { for (const sponsor of sponsors) { this.sponsors.set(sponsor.id, sponsor); this.logger.debug(`Seeded sponsor: ${sponsor.id}.`); } this.logger.info(`Successfully seeded ${sponsors.length} sponsors.`); } catch (error) { this.logger.error(`Error seeding sponsors:`, error); throw error; } } // Test helper clear(): void { this.logger.debug('Clearing all sponsors.'); this.sponsors.clear(); this.logger.info('All sponsors cleared.'); } }