Files
gridpilot.gg/packages/racing/infrastructure/repositories/InMemoryLiveryRepository.ts
2025-12-10 12:38:55 +01:00

114 lines
3.5 KiB
TypeScript

/**
* In-Memory Implementation: ILiveryRepository
*
* Mock repository for testing and development
*/
import type { DriverLivery } from '../../domain/entities/DriverLivery';
import type { LiveryTemplate } from '../../domain/entities/LiveryTemplate';
import type { ILiveryRepository } from '../../domain/repositories/ILiveryRepository';
export class InMemoryLiveryRepository implements ILiveryRepository {
private driverLiveries: Map<string, DriverLivery> = new Map();
private templates: Map<string, LiveryTemplate> = new Map();
// DriverLivery operations
async findDriverLiveryById(id: string): Promise<DriverLivery | null> {
return this.driverLiveries.get(id) ?? null;
}
async findDriverLiveriesByDriverId(driverId: string): Promise<DriverLivery[]> {
return Array.from(this.driverLiveries.values()).filter(l => l.driverId === driverId);
}
async findDriverLiveryByDriverAndCar(driverId: string, carId: string): Promise<DriverLivery | null> {
for (const livery of this.driverLiveries.values()) {
if (livery.driverId === driverId && livery.carId === carId) {
return livery;
}
}
return null;
}
async findDriverLiveriesByGameId(gameId: string): Promise<DriverLivery[]> {
return Array.from(this.driverLiveries.values()).filter(l => l.gameId === gameId);
}
async findDriverLiveryByDriverAndGame(driverId: string, gameId: string): Promise<DriverLivery[]> {
return Array.from(this.driverLiveries.values()).filter(
l => l.driverId === driverId && l.gameId === gameId
);
}
async createDriverLivery(livery: DriverLivery): Promise<DriverLivery> {
if (this.driverLiveries.has(livery.id)) {
throw new Error('DriverLivery with this ID already exists');
}
this.driverLiveries.set(livery.id, livery);
return livery;
}
async updateDriverLivery(livery: DriverLivery): Promise<DriverLivery> {
if (!this.driverLiveries.has(livery.id)) {
throw new Error('DriverLivery not found');
}
this.driverLiveries.set(livery.id, livery);
return livery;
}
async deleteDriverLivery(id: string): Promise<void> {
this.driverLiveries.delete(id);
}
// LiveryTemplate operations
async findTemplateById(id: string): Promise<LiveryTemplate | null> {
return this.templates.get(id) ?? null;
}
async findTemplatesBySeasonId(seasonId: string): Promise<LiveryTemplate[]> {
return Array.from(this.templates.values()).filter(t => t.seasonId === seasonId);
}
async findTemplateBySeasonAndCar(seasonId: string, carId: string): Promise<LiveryTemplate | null> {
for (const template of this.templates.values()) {
if (template.seasonId === seasonId && template.carId === carId) {
return template;
}
}
return null;
}
async createTemplate(template: LiveryTemplate): Promise<LiveryTemplate> {
if (this.templates.has(template.id)) {
throw new Error('LiveryTemplate with this ID already exists');
}
this.templates.set(template.id, template);
return template;
}
async updateTemplate(template: LiveryTemplate): Promise<LiveryTemplate> {
if (!this.templates.has(template.id)) {
throw new Error('LiveryTemplate not found');
}
this.templates.set(template.id, template);
return template;
}
async deleteTemplate(id: string): Promise<void> {
this.templates.delete(id);
}
// Test helpers
clearDriverLiveries(): void {
this.driverLiveries.clear();
}
clearTemplates(): void {
this.templates.clear();
}
clear(): void {
this.driverLiveries.clear();
this.templates.clear();
}
}