Files
gridpilot.gg/packages/racing/infrastructure/repositories/InMemoryPenaltyRepository.ts
2025-12-09 10:32:59 +01:00

70 lines
2.0 KiB
TypeScript

/**
* In-Memory Implementation: InMemoryPenaltyRepository
*
* Provides an in-memory storage implementation for penalties.
*/
import type { Penalty } from '../../domain/entities/Penalty';
import type { IPenaltyRepository } from '../../domain/repositories/IPenaltyRepository';
export class InMemoryPenaltyRepository implements IPenaltyRepository {
private penalties: Map<string, Penalty> = new Map();
constructor(initialPenalties: Penalty[] = []) {
initialPenalties.forEach(penalty => {
this.penalties.set(penalty.id, penalty);
});
}
async findById(id: string): Promise<Penalty | null> {
return this.penalties.get(id) || null;
}
async findByRaceId(raceId: string): Promise<Penalty[]> {
return Array.from(this.penalties.values()).filter(
penalty => penalty.raceId === raceId
);
}
async findByDriverId(driverId: string): Promise<Penalty[]> {
return Array.from(this.penalties.values()).filter(
penalty => penalty.driverId === driverId
);
}
async findByProtestId(protestId: string): Promise<Penalty[]> {
return Array.from(this.penalties.values()).filter(
penalty => penalty.protestId === protestId
);
}
async findPending(): Promise<Penalty[]> {
return Array.from(this.penalties.values()).filter(
penalty => penalty.isPending()
);
}
async findIssuedBy(stewardId: string): Promise<Penalty[]> {
return Array.from(this.penalties.values()).filter(
penalty => penalty.issuedBy === stewardId
);
}
async create(penalty: Penalty): Promise<void> {
if (this.penalties.has(penalty.id)) {
throw new Error(`Penalty with ID ${penalty.id} already exists`);
}
this.penalties.set(penalty.id, penalty);
}
async update(penalty: Penalty): Promise<void> {
if (!this.penalties.has(penalty.id)) {
throw new Error(`Penalty with ID ${penalty.id} not found`);
}
this.penalties.set(penalty.id, penalty);
}
async exists(id: string): Promise<boolean> {
return this.penalties.has(id);
}
}