refactor adapters
This commit is contained in:
@@ -0,0 +1,170 @@
|
||||
/**
|
||||
* In-Memory Implementation: ISeasonSponsorshipRepository
|
||||
*
|
||||
* Mock repository for testing and development
|
||||
*/
|
||||
|
||||
import type { SeasonSponsorship, SponsorshipTier } from '../../domain/entities/SeasonSponsorship';
|
||||
import type { ISeasonSponsorshipRepository } from '../../domain/repositories/ISeasonSponsorshipRepository';
|
||||
import type { ILogger } from '@gridpilot/shared/logging/ILogger';
|
||||
|
||||
export class InMemorySeasonSponsorshipRepository implements ISeasonSponsorshipRepository {
|
||||
private sponsorships: Map<string, SeasonSponsorship> = new Map();
|
||||
private readonly logger: ILogger;
|
||||
|
||||
constructor(logger: ILogger, 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);
|
||||
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);
|
||||
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);
|
||||
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);
|
||||
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);
|
||||
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);
|
||||
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);
|
||||
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);
|
||||
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);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
// Test helper
|
||||
clear(): void {
|
||||
this.logger.debug('Clearing all season sponsorships.');
|
||||
this.sponsorships.clear();
|
||||
this.logger.info('All season sponsorships cleared.');
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user