70 lines
2.0 KiB
TypeScript
70 lines
2.0 KiB
TypeScript
/**
|
|
* 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<string, Protest> = new Map();
|
|
|
|
constructor(initialProtests: Protest[] = []) {
|
|
initialProtests.forEach(protest => {
|
|
this.protests.set(protest.id, protest);
|
|
});
|
|
}
|
|
|
|
async findById(id: string): Promise<Protest | null> {
|
|
return this.protests.get(id) || null;
|
|
}
|
|
|
|
async findByRaceId(raceId: string): Promise<Protest[]> {
|
|
return Array.from(this.protests.values()).filter(
|
|
protest => protest.raceId === raceId
|
|
);
|
|
}
|
|
|
|
async findByProtestingDriverId(driverId: string): Promise<Protest[]> {
|
|
return Array.from(this.protests.values()).filter(
|
|
protest => protest.protestingDriverId === driverId
|
|
);
|
|
}
|
|
|
|
async findByAccusedDriverId(driverId: string): Promise<Protest[]> {
|
|
return Array.from(this.protests.values()).filter(
|
|
protest => protest.accusedDriverId === driverId
|
|
);
|
|
}
|
|
|
|
async findPending(): Promise<Protest[]> {
|
|
return Array.from(this.protests.values()).filter(
|
|
protest => protest.isPending()
|
|
);
|
|
}
|
|
|
|
async findUnderReviewBy(stewardId: string): Promise<Protest[]> {
|
|
return Array.from(this.protests.values()).filter(
|
|
protest => protest.reviewedBy === stewardId && protest.isUnderReview()
|
|
);
|
|
}
|
|
|
|
async create(protest: Protest): Promise<void> {
|
|
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<void> {
|
|
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<boolean> {
|
|
return this.protests.has(id);
|
|
}
|
|
} |