module creation

This commit is contained in:
2025-12-15 21:44:06 +01:00
parent b834f88bbd
commit 7c7267da72
88 changed files with 12119 additions and 4241 deletions

View File

@@ -1,149 +1,98 @@
/**
* 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';
import { ISponsorRepository } from '@gridpilot/core/racing/domain/repositories/ISponsorRepository';
import { Sponsor } from '@gridpilot/core/racing/domain/entities/Sponsor';
import { ILogger } from '@gridpilot/shared/logging/ILogger';
export class InMemorySponsorRepository implements ISponsorRepository {
private sponsors: Map<string, Sponsor> = new Map();
private readonly logger: ILogger;
private emailIndex: Map<string, string> = new Map(); // contactEmail -> sponsorId
constructor(logger: ILogger, seedData?: Sponsor[]) {
this.logger = logger;
constructor(private readonly logger: ILogger, initialSponsors: Sponsor[] = []) {
this.logger.info('InMemorySponsorRepository initialized.');
if (seedData) {
this.seed(seedData);
for (const sponsor of initialSponsors) {
this.sponsors.set(sponsor.id, sponsor);
this.emailIndex.set(sponsor.contactEmail.toLowerCase(), sponsor.id);
this.logger.debug(`Seeded sponsor: ${sponsor.id} (${sponsor.name}).`);
}
}
async findById(id: string): Promise<Sponsor | null> {
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;
this.logger.debug(`[InMemorySponsorRepository] Finding sponsor by ID: ${id}`);
const sponsor = this.sponsors.get(id) ?? null;
if (sponsor) {
this.logger.info(`Found sponsor by ID: ${id}.`);
} else {
this.logger.warn(`Sponsor with ID ${id} not found.`);
}
return Promise.resolve(sponsor);
}
async findAll(): Promise<Sponsor[]> {
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;
}
this.logger.debug('[InMemorySponsorRepository] Finding all sponsors.');
return Promise.resolve(Array.from(this.sponsors.values()));
}
async findByEmail(email: string): Promise<Sponsor | null> {
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.debug(`[InMemorySponsorRepository] Finding sponsor by email: ${email}`);
const sponsorId = this.emailIndex.get(email.toLowerCase());
if (!sponsorId) {
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;
return Promise.resolve(null);
}
return this.findById(sponsorId);
}
async create(sponsor: Sponsor): Promise<Sponsor> {
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;
this.logger.debug(`[InMemorySponsorRepository] Creating sponsor: ${sponsor.id} (${sponsor.name})`);
if (this.sponsors.has(sponsor.id)) {
this.logger.warn(`Sponsor with ID ${sponsor.id} already exists.`);
throw new Error('Sponsor already exists');
}
if (this.emailIndex.has(sponsor.contactEmail.toLowerCase())) {
this.logger.warn(`Sponsor with email ${sponsor.contactEmail} already exists.`);
throw new Error('Sponsor with this email already exists');
}
this.sponsors.set(sponsor.id, sponsor);
this.emailIndex.set(sponsor.contactEmail.toLowerCase(), sponsor.id);
this.logger.info(`Sponsor ${sponsor.id} (${sponsor.name}) created successfully.`);
return Promise.resolve(sponsor);
}
async update(sponsor: Sponsor): Promise<Sponsor> {
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;
this.logger.debug(`[InMemorySponsorRepository] Updating sponsor: ${sponsor.id} (${sponsor.name})`);
if (!this.sponsors.has(sponsor.id)) {
this.logger.warn(`Sponsor with ID ${sponsor.id} not found for update.`);
throw new Error('Sponsor not found');
}
const existingSponsor = this.sponsors.get(sponsor.id);
// If email changed, update index
if (existingSponsor && existingSponsor.contactEmail.toLowerCase() !== sponsor.contactEmail.toLowerCase()) {
if (this.emailIndex.has(sponsor.contactEmail.toLowerCase()) && this.emailIndex.get(sponsor.contactEmail.toLowerCase()) !== sponsor.id) {
this.logger.warn(`Cannot update sponsor ${sponsor.id} to email ${sponsor.contactEmail} as it's already taken.`);
throw new Error('Sponsor with this email already exists');
}
this.emailIndex.delete(existingSponsor.contactEmail.toLowerCase());
this.emailIndex.set(sponsor.contactEmail.toLowerCase(), sponsor.id);
}
this.sponsors.set(sponsor.id, sponsor);
this.logger.info(`Sponsor ${sponsor.id} (${sponsor.name}) updated successfully.`);
return Promise.resolve(sponsor);
}
async delete(id: string): Promise<void> {
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;
this.logger.debug(`[InMemorySponsorRepository] Deleting sponsor with ID: ${id}`);
const sponsor = this.sponsors.get(id);
if (sponsor) {
this.sponsors.delete(id);
this.emailIndex.delete(sponsor.contactEmail.toLowerCase());
this.logger.info(`Sponsor ${id} deleted successfully.`);
} else {
this.logger.warn(`Sponsor with ID ${id} not found for deletion.`);
}
return Promise.resolve();
}
async exists(id: string): Promise<boolean> {
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;
}
this.logger.debug(`[InMemorySponsorRepository] Checking existence of sponsor with ID: ${id}`);
return Promise.resolve(this.sponsors.has(id));
}
/**
* 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.');
}
}
}