/** * 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 = new Map(); constructor(initialPenalties: Penalty[] = []) { initialPenalties.forEach(penalty => { this.penalties.set(penalty.id, penalty); }); } async findById(id: string): Promise { return this.penalties.get(id) || null; } async findByRaceId(raceId: string): Promise { return Array.from(this.penalties.values()).filter( penalty => penalty.raceId === raceId ); } async findByDriverId(driverId: string): Promise { return Array.from(this.penalties.values()).filter( penalty => penalty.driverId === driverId ); } async findByProtestId(protestId: string): Promise { return Array.from(this.penalties.values()).filter( penalty => penalty.protestId === protestId ); } async findPending(): Promise { return Array.from(this.penalties.values()).filter( penalty => penalty.isPending() ); } async findIssuedBy(stewardId: string): Promise { return Array.from(this.penalties.values()).filter( penalty => penalty.issuedBy === stewardId ); } async create(penalty: Penalty): Promise { 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 { 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 { return this.penalties.has(id); } }