67 lines
1.6 KiB
TypeScript
67 lines
1.6 KiB
TypeScript
/**
|
|
* In-Memory Implementation: ISponsorRepository
|
|
*
|
|
* Mock repository for testing and development
|
|
*/
|
|
|
|
import type { Sponsor } from '../../domain/entities/Sponsor';
|
|
import type { ISponsorRepository } from '../../domain/repositories/ISponsorRepository';
|
|
|
|
export class InMemorySponsorRepository implements ISponsorRepository {
|
|
private sponsors: Map<string, Sponsor> = new Map();
|
|
|
|
async findById(id: string): Promise<Sponsor | null> {
|
|
return this.sponsors.get(id) ?? null;
|
|
}
|
|
|
|
async findAll(): Promise<Sponsor[]> {
|
|
return Array.from(this.sponsors.values());
|
|
}
|
|
|
|
async findByEmail(email: string): Promise<Sponsor | null> {
|
|
for (const sponsor of this.sponsors.values()) {
|
|
if (sponsor.contactEmail === email) {
|
|
return sponsor;
|
|
}
|
|
}
|
|
return null;
|
|
}
|
|
|
|
async create(sponsor: Sponsor): Promise<Sponsor> {
|
|
if (this.sponsors.has(sponsor.id)) {
|
|
throw new Error('Sponsor with this ID already exists');
|
|
}
|
|
this.sponsors.set(sponsor.id, sponsor);
|
|
return sponsor;
|
|
}
|
|
|
|
async update(sponsor: Sponsor): Promise<Sponsor> {
|
|
if (!this.sponsors.has(sponsor.id)) {
|
|
throw new Error('Sponsor not found');
|
|
}
|
|
this.sponsors.set(sponsor.id, sponsor);
|
|
return sponsor;
|
|
}
|
|
|
|
async delete(id: string): Promise<void> {
|
|
this.sponsors.delete(id);
|
|
}
|
|
|
|
async exists(id: string): Promise<boolean> {
|
|
return this.sponsors.has(id);
|
|
}
|
|
|
|
/**
|
|
* Seed initial data
|
|
*/
|
|
seed(sponsors: Sponsor[]): void {
|
|
for (const sponsor of sponsors) {
|
|
this.sponsors.set(sponsor.id, sponsor);
|
|
}
|
|
}
|
|
|
|
// Test helper
|
|
clear(): void {
|
|
this.sponsors.clear();
|
|
}
|
|
} |