refactor
This commit is contained in:
@@ -5,12 +5,8 @@ import { Logger } from '@core/shared/application';
|
||||
export class InMemoryRaceRegistrationRepository implements IRaceRegistrationRepository {
|
||||
private registrations: Map<string, RaceRegistration> = new Map(); // Key: `${raceId}:${driverId}`
|
||||
|
||||
constructor(private readonly logger: Logger, initialRegistrations: RaceRegistration[] = []) {
|
||||
constructor(private readonly logger: Logger) {
|
||||
this.logger.info('InMemoryRaceRegistrationRepository initialized.');
|
||||
for (const reg of initialRegistrations) {
|
||||
this.registrations.set(reg.id, reg);
|
||||
this.logger.debug(`Seeded registration: ${reg.id}.`);
|
||||
}
|
||||
}
|
||||
|
||||
async isRegistered(raceId: string, driverId: string): Promise<boolean> {
|
||||
@@ -23,8 +19,8 @@ export class InMemoryRaceRegistrationRepository implements IRaceRegistrationRepo
|
||||
this.logger.debug(`[InMemoryRaceRegistrationRepository] Getting registered drivers for race ${raceId}.`);
|
||||
const driverIds: string[] = [];
|
||||
for (const registration of this.registrations.values()) {
|
||||
if (registration.raceId === raceId) {
|
||||
driverIds.push(registration.driverId);
|
||||
if (registration.raceId.toString() === raceId) {
|
||||
driverIds.push(registration.driverId.toString());
|
||||
}
|
||||
}
|
||||
this.logger.info(`Found ${driverIds.length} registered drivers for race ${raceId}.`);
|
||||
@@ -33,19 +29,19 @@ export class InMemoryRaceRegistrationRepository implements IRaceRegistrationRepo
|
||||
|
||||
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 === raceId).length;
|
||||
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> {
|
||||
this.logger.debug(`[InMemoryRaceRegistrationRepository] Registering driver ${registration.driverId} for race ${registration.raceId}.`);
|
||||
if (await this.isRegistered(registration.raceId, registration.driverId)) {
|
||||
this.logger.warn(`Driver ${registration.driverId} already registered for race ${registration.raceId}.`);
|
||||
this.logger.debug(`[InMemoryRaceRegistrationRepository] Registering driver ${registration.driverId.toString()} for race ${registration.raceId.toString()}.`);
|
||||
if (await this.isRegistered(registration.raceId.toString(), registration.driverId.toString())) {
|
||||
this.logger.warn(`Driver ${registration.driverId.toString()} already registered for race ${registration.raceId.toString()}.`);
|
||||
throw new Error('Driver already registered for this race');
|
||||
}
|
||||
this.registrations.set(registration.id, registration);
|
||||
this.logger.info(`Driver ${registration.driverId} registered for race ${registration.raceId}.`);
|
||||
this.logger.info(`Driver ${registration.driverId.toString()} registered for race ${registration.raceId.toString()}.`);
|
||||
return Promise.resolve();
|
||||
}
|
||||
|
||||
@@ -65,8 +61,8 @@ export class InMemoryRaceRegistrationRepository implements IRaceRegistrationRepo
|
||||
this.logger.debug(`[InMemoryRaceRegistrationRepository] Getting registrations for driver: ${driverId}.`);
|
||||
const raceIds: string[] = [];
|
||||
for (const registration of this.registrations.values()) {
|
||||
if (registration.driverId === driverId) {
|
||||
raceIds.push(registration.raceId);
|
||||
if (registration.driverId.toString() === driverId) {
|
||||
raceIds.push(registration.raceId.toString());
|
||||
}
|
||||
}
|
||||
this.logger.info(`Found ${raceIds.length} registrations for driver ${driverId}.`);
|
||||
@@ -77,7 +73,7 @@ export class InMemoryRaceRegistrationRepository implements IRaceRegistrationRepo
|
||||
this.logger.debug(`[InMemoryRaceRegistrationRepository] Clearing all registrations for race: ${raceId}.`);
|
||||
const registrationsToDelete: string[] = [];
|
||||
for (const registration of this.registrations.values()) {
|
||||
if (registration.raceId === raceId) {
|
||||
if (registration.raceId.toString() === raceId) {
|
||||
registrationsToDelete.push(registration.id);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user