/** * In-Memory Implementation: InMemoryProtestRepository * * Provides an in-memory storage implementation for protests. */ import type { Protest } from '../../domain/entities/Protest'; import type { IProtestRepository } from '../../domain/repositories/IProtestRepository'; export class InMemoryProtestRepository implements IProtestRepository { private protests: Map = new Map(); constructor(initialProtests: Protest[] = []) { initialProtests.forEach(protest => { this.protests.set(protest.id, protest); }); } async findById(id: string): Promise { return this.protests.get(id) || null; } async findByRaceId(raceId: string): Promise { return Array.from(this.protests.values()).filter( protest => protest.raceId === raceId ); } async findByProtestingDriverId(driverId: string): Promise { return Array.from(this.protests.values()).filter( protest => protest.protestingDriverId === driverId ); } async findByAccusedDriverId(driverId: string): Promise { return Array.from(this.protests.values()).filter( protest => protest.accusedDriverId === driverId ); } async findPending(): Promise { return Array.from(this.protests.values()).filter( protest => protest.isPending() ); } async findUnderReviewBy(stewardId: string): Promise { return Array.from(this.protests.values()).filter( protest => protest.reviewedBy === stewardId && protest.isUnderReview() ); } async create(protest: Protest): Promise { if (this.protests.has(protest.id)) { throw new Error(`Protest with ID ${protest.id} already exists`); } this.protests.set(protest.id, protest); } async update(protest: Protest): Promise { if (!this.protests.has(protest.id)) { throw new Error(`Protest with ID ${protest.id} not found`); } this.protests.set(protest.id, protest); } async exists(id: string): Promise { return this.protests.has(id); } }