Files
gridpilot.gg/adapters/racing/persistence/inmemory/InMemorySeasonSponsorshipRepository.ts
2025-12-16 13:53:23 +01:00

170 lines
7.0 KiB
TypeScript

/**
* In-Memory Implementation: ISeasonSponsorshipRepository
*
* Mock repository for testing and development
*/
import type { SeasonSponsorship, SponsorshipTier } from '@core/racing/domain/entities/SeasonSponsorship';
import type { ISeasonSponsorshipRepository } from '@core/racing/domain/repositories/ISeasonSponsorshipRepository';
import type { Logger } from '@core/shared/application';
export class InMemorySeasonSponsorshipRepository implements ISeasonSponsorshipRepository {
private sponsorships: Map<string, SeasonSponsorship> = new Map();
private readonly logger: Logger;
constructor(logger: Logger, seedData?: SeasonSponsorship[]) {
this.logger = logger;
this.logger.info('InMemorySeasonSponsorshipRepository initialized.');
if (seedData) {
this.seed(seedData);
}
}
async findById(id: string): Promise<SeasonSponsorship | null> {
this.logger.debug(`Finding season sponsorship by id: ${id}`);
try {
const sponsorship = this.sponsorships.get(id) ?? null;
if (sponsorship) {
this.logger.info(`Found season sponsorship: ${id}.`);
} else {
this.logger.warn(`Season sponsorship with id ${id} not found.`);
}
return sponsorship;
} catch (error) {
this.logger.error(`Error finding season sponsorship by id ${id}:`, error instanceof Error ? error : new Error(String(error)));
throw error;
}
}
async findBySeasonId(seasonId: string): Promise<SeasonSponsorship[]> {
this.logger.debug(`Finding season sponsorships by season id: ${seasonId}`);
try {
const sponsorships = Array.from(this.sponsorships.values()).filter(s => s.seasonId === seasonId);
this.logger.info(`Found ${sponsorships.length} season sponsorships for season id: ${seasonId}.`);
return sponsorships;
} catch (error) {
this.logger.error(`Error finding season sponsorships by season id ${seasonId}:`, error instanceof Error ? error : new Error(String(error)));
throw error;
}
}
async findByLeagueId(leagueId: string): Promise<SeasonSponsorship[]> {
this.logger.debug(`Finding season sponsorships by league id: ${leagueId}`);
try {
const sponsorships = Array.from(this.sponsorships.values()).filter(s => s.leagueId === leagueId);
this.logger.info(`Found ${sponsorships.length} season sponsorships for league id: ${leagueId}.`);
return sponsorships;
} catch (error) {
this.logger.error(`Error finding season sponsorships by league id ${leagueId}:`, error instanceof Error ? error : new Error(String(error)));
throw error;
}
}
async findBySponsorId(sponsorId: string): Promise<SeasonSponsorship[]> {
this.logger.debug(`Finding season sponsorships by sponsor id: ${sponsorId}`);
try {
const sponsorships = Array.from(this.sponsorships.values()).filter(s => s.sponsorId === sponsorId);
this.logger.info(`Found ${sponsorships.length} season sponsorships for sponsor id: ${sponsorId}.`);
return sponsorships;
} catch (error) {
this.logger.error(`Error finding season sponsorships by sponsor id ${sponsorId}:`, error instanceof Error ? error : new Error(String(error)));
throw error;
}
}
async findBySeasonAndTier(seasonId: string, tier: SponsorshipTier): Promise<SeasonSponsorship[]> {
this.logger.debug(`Finding season sponsorships by season id: ${seasonId} and tier: ${tier}`);
try {
const sponsorships = Array.from(this.sponsorships.values()).filter(
s => s.seasonId === seasonId && s.tier === tier
);
this.logger.info(`Found ${sponsorships.length} season sponsorships for season id: ${seasonId}, tier: ${tier}.`);
return sponsorships;
} catch (error) {
this.logger.error(`Error finding season sponsorships by season id ${seasonId}, tier ${tier}:`, error);
throw error;
}
}
async create(sponsorship: SeasonSponsorship): Promise<SeasonSponsorship> {
this.logger.debug(`Creating season sponsorship: ${sponsorship.id}`);
try {
if (this.sponsorships.has(sponsorship.id)) {
this.logger.warn(`SeasonSponsorship with ID ${sponsorship.id} already exists.`);
throw new Error('SeasonSponsorship with this ID already exists');
}
this.sponsorships.set(sponsorship.id, sponsorship);
this.logger.info(`SeasonSponsorship ${sponsorship.id} created successfully.`);
return sponsorship;
} catch (error) {
this.logger.error(`Error creating season sponsorship ${sponsorship.id}:`, error instanceof Error ? error : new Error(String(error)));
throw error;
}
}
async update(sponsorship: SeasonSponsorship): Promise<SeasonSponsorship> {
this.logger.debug(`Updating season sponsorship: ${sponsorship.id}`);
try {
if (!this.sponsorships.has(sponsorship.id)) {
this.logger.warn(`SeasonSponsorship with ID ${sponsorship.id} not found for update.`);
throw new Error('SeasonSponsorship not found');
}
this.sponsorships.set(sponsorship.id, sponsorship);
this.logger.info(`SeasonSponsorship ${sponsorship.id} updated successfully.`);
return sponsorship;
} catch (error) {
this.logger.error(`Error updating season sponsorship ${sponsorship.id}:`, error instanceof Error ? error : new Error(String(error)));
throw error;
}
}
async delete(id: string): Promise<void> {
this.logger.debug(`Deleting season sponsorship: ${id}`);
try {
if (this.sponsorships.delete(id)) {
this.logger.info(`SeasonSponsorship ${id} deleted successfully.`);
} else {
this.logger.warn(`SeasonSponsorship with id ${id} not found for deletion.`);
}
} catch (error) {
this.logger.error(`Error deleting season sponsorship ${id}:`, error instanceof Error ? error : new Error(String(error)));
throw error;
}
}
async exists(id: string): Promise<boolean> {
this.logger.debug(`Checking existence of season sponsorship with id: ${id}`);
try {
const exists = this.sponsorships.has(id);
this.logger.debug(`SeasonSponsorship ${id} exists: ${exists}.`);
return exists;
} catch (error) {
this.logger.error(`Error checking existence of season sponsorship with id ${id}:`, error instanceof Error ? error : new Error(String(error)));
throw error;
}
}
/**
* Seed initial data
*/
seed(sponsorships: SeasonSponsorship[]): void {
this.logger.debug(`Seeding ${sponsorships.length} season sponsorships.`);
try {
for (const sponsorship of sponsorships) {
this.sponsorships.set(sponsorship.id, sponsorship);
this.logger.debug(`Seeded season sponsorship: ${sponsorship.id}.`);
}
this.logger.info(`Successfully seeded ${sponsorships.length} season sponsorships.`);
} catch (error) {
this.logger.error(`Error seeding season sponsorships:`, error instanceof Error ? error : new Error(String(error)));
throw error;
}
}
// Test helper
clear(): void {
this.logger.debug('Clearing all season sponsorships.');
this.sponsorships.clear();
this.logger.info('All season sponsorships cleared.');
}
}