106 lines
4.6 KiB
TypeScript
106 lines
4.6 KiB
TypeScript
import { RaceRegistration } from '@core/racing/domain/entities/RaceRegistration';
|
|
import { RaceRegistrationRepository } from '@core/racing/domain/repositories/RaceRegistrationRepository';
|
|
import { Logger } from '@core/shared/domain';
|
|
|
|
export class InMemoryRaceRegistrationRepository implements RaceRegistrationRepository {
|
|
private registrations: Map<string, RaceRegistration> = new Map(); // Key: `${raceId}:${driverId}`
|
|
|
|
constructor(private readonly logger: Logger) {
|
|
this.logger.info('InMemoryRaceRegistrationRepository initialized.');
|
|
}
|
|
|
|
async isRegistered(raceId: string, driverId: string): Promise<boolean> {
|
|
this.logger.debug(`[InMemoryRaceRegistrationRepository] Checking if driver ${driverId} is registered for race ${raceId}.`);
|
|
const key = `${raceId}:${driverId}`;
|
|
return Promise.resolve(this.registrations.has(key));
|
|
}
|
|
|
|
async getRegisteredDrivers(raceId: string): Promise<string[]> {
|
|
this.logger.debug(`[InMemoryRaceRegistrationRepository] Getting registered drivers for race ${raceId}.`);
|
|
const driverIds: string[] = [];
|
|
for (const registration of this.registrations.values()) {
|
|
if (registration.raceId.toString() === raceId) {
|
|
driverIds.push(registration.driverId.toString());
|
|
}
|
|
}
|
|
this.logger.info(`Found ${driverIds.length} registered drivers for race ${raceId}.`);
|
|
return Promise.resolve(driverIds);
|
|
}
|
|
|
|
async findByRaceId(raceId: string): Promise<RaceRegistration[]> {
|
|
this.logger.debug(`[InMemoryRaceRegistrationRepository] Finding all registrations for race ${raceId}.`);
|
|
const registrations = Array.from(this.registrations.values()).filter(
|
|
reg => reg.raceId.toString() === raceId
|
|
);
|
|
this.logger.info(`Found ${registrations.length} registrations for race ${raceId}.`);
|
|
return Promise.resolve(registrations);
|
|
}
|
|
|
|
async getRegistrationCount(raceId: string): Promise<number> {
|
|
this.logger.debug(`[InMemoryRaceRegistrationRepository] Getting registration count for race ${raceId}.`);
|
|
const count = Array.from(this.registrations.values()).filter(reg => reg.raceId.toString() === raceId).length;
|
|
this.logger.info(`Registration count for race ${raceId}: ${count}.`);
|
|
return Promise.resolve(count);
|
|
}
|
|
|
|
async register(registration: RaceRegistration): Promise<void> {
|
|
const raceId = registration.raceId.toString();
|
|
const driverId = registration.driverId.toString();
|
|
|
|
this.logger.debug(`[InMemoryRaceRegistrationRepository] Registering driver ${driverId} for race ${raceId}.`);
|
|
|
|
if (await this.isRegistered(raceId, driverId)) {
|
|
this.logger.warn(`Driver ${driverId} already registered for race ${raceId}.`);
|
|
throw new Error('Driver already registered for this race');
|
|
}
|
|
|
|
const key = `${raceId}:${driverId}`;
|
|
this.registrations.set(key, registration);
|
|
|
|
this.logger.info(`Driver ${driverId} registered for race ${raceId}.`);
|
|
return Promise.resolve();
|
|
}
|
|
|
|
async withdraw(raceId: string, driverId: string): Promise<void> {
|
|
this.logger.debug(`[InMemoryRaceRegistrationRepository] Withdrawing driver ${driverId} from race ${raceId}.`);
|
|
const key = `${raceId}:${driverId}`;
|
|
if (!this.registrations.has(key)) {
|
|
this.logger.warn(`Driver ${driverId} not registered for race ${raceId}. No withdrawal needed.`);
|
|
throw new Error('Driver not registered for this race');
|
|
}
|
|
this.registrations.delete(key);
|
|
this.logger.info(`Driver ${driverId} withdrawn from race ${raceId}.`);
|
|
return Promise.resolve();
|
|
}
|
|
|
|
async getDriverRegistrations(driverId: string): Promise<string[]> {
|
|
this.logger.debug(`[InMemoryRaceRegistrationRepository] Getting registrations for driver: ${driverId}.`);
|
|
const raceIds: string[] = [];
|
|
for (const registration of this.registrations.values()) {
|
|
if (registration.driverId.toString() === driverId) {
|
|
raceIds.push(registration.raceId.toString());
|
|
}
|
|
}
|
|
this.logger.info(`Found ${raceIds.length} registrations for driver ${driverId}.`);
|
|
return Promise.resolve(raceIds);
|
|
}
|
|
|
|
async clearRaceRegistrations(raceId: string): Promise<void> {
|
|
this.logger.debug(`[InMemoryRaceRegistrationRepository] Clearing all registrations for race: ${raceId}.`);
|
|
|
|
const keysToDelete: string[] = [];
|
|
for (const registration of this.registrations.values()) {
|
|
if (registration.raceId.toString() === raceId) {
|
|
keysToDelete.push(`${registration.raceId.toString()}:${registration.driverId.toString()}`);
|
|
}
|
|
}
|
|
|
|
for (const key of keysToDelete) {
|
|
this.registrations.delete(key);
|
|
}
|
|
|
|
this.logger.info(`Cleared ${keysToDelete.length} registrations for race ${raceId}.`);
|
|
return Promise.resolve();
|
|
}
|
|
}
|