refactor
This commit is contained in:
@@ -6,13 +6,8 @@ export class InMemoryDriverRepository implements IDriverRepository {
|
||||
private drivers: Map<string, Driver> = new Map();
|
||||
private iracingIdIndex: Map<string, string> = new Map(); // iracingId -> driverId
|
||||
|
||||
constructor(private readonly logger: Logger, initialDrivers: Driver[] = []) {
|
||||
constructor(private readonly logger: Logger) {
|
||||
this.logger.info('InMemoryDriverRepository initialized.');
|
||||
for (const driver of initialDrivers) {
|
||||
this.drivers.set(driver.id, driver);
|
||||
this.iracingIdIndex.set(driver.iracingId, driver.id);
|
||||
this.logger.debug(`Seeded driver: ${driver.id} (iRacing ID: ${driver.iracingId}).`);
|
||||
}
|
||||
}
|
||||
|
||||
async findById(id: string): Promise<Driver | null> {
|
||||
@@ -42,33 +37,33 @@ export class InMemoryDriverRepository implements IDriverRepository {
|
||||
}
|
||||
|
||||
async create(driver: Driver): Promise<Driver> {
|
||||
this.logger.debug(`[InMemoryDriverRepository] Creating driver: ${driver.id} (iRacing ID: ${driver.iracingId})`);
|
||||
this.logger.debug(`[InMemoryDriverRepository] Creating driver: ${driver.id} (iRacing ID: ${driver.iracingId.toString()})`);
|
||||
if (this.drivers.has(driver.id)) {
|
||||
this.logger.warn(`Driver with ID ${driver.id} already exists.`);
|
||||
throw new Error('Driver already exists');
|
||||
}
|
||||
if (this.iracingIdIndex.has(driver.iracingId)) {
|
||||
this.logger.warn(`Driver with iRacing ID ${driver.iracingId} already exists.`);
|
||||
if (this.iracingIdIndex.has(driver.iracingId.toString())) {
|
||||
this.logger.warn(`Driver with iRacing ID ${driver.iracingId.toString()} already exists.`);
|
||||
throw new Error('iRacing ID already registered');
|
||||
}
|
||||
this.drivers.set(driver.id, driver);
|
||||
this.iracingIdIndex.set(driver.iracingId, driver.id);
|
||||
this.iracingIdIndex.set(driver.iracingId.toString(), driver.id);
|
||||
this.logger.info(`Driver ${driver.id} created successfully.`);
|
||||
return Promise.resolve(driver);
|
||||
}
|
||||
|
||||
async update(driver: Driver): Promise<Driver> {
|
||||
this.logger.debug(`[InMemoryDriverRepository] Updating driver: ${driver.id} (iRacing ID: ${driver.iracingId})`);
|
||||
this.logger.debug(`[InMemoryDriverRepository] Updating driver: ${driver.id} (iRacing ID: ${driver.iracingId.toString()})`);
|
||||
if (!this.drivers.has(driver.id)) {
|
||||
this.logger.warn(`Driver with ID ${driver.id} not found for update.`);
|
||||
throw new Error('Driver not found');
|
||||
}
|
||||
const existingDriver = this.drivers.get(driver.id);
|
||||
this.drivers.set(driver.id, driver);
|
||||
// Re-index iRacing ID if it changed
|
||||
const existingDriver = this.drivers.get(driver.id);
|
||||
if (existingDriver && existingDriver.iracingId !== driver.iracingId) {
|
||||
this.iracingIdIndex.delete(existingDriver.iracingId);
|
||||
this.iracingIdIndex.set(driver.iracingId, driver.id);
|
||||
if (existingDriver && existingDriver.iracingId.toString() !== driver.iracingId.toString()) {
|
||||
this.iracingIdIndex.delete(existingDriver.iracingId.toString());
|
||||
this.iracingIdIndex.set(driver.iracingId.toString(), driver.id);
|
||||
}
|
||||
this.logger.info(`Driver ${driver.id} updated successfully.`);
|
||||
return Promise.resolve(driver);
|
||||
@@ -79,7 +74,7 @@ export class InMemoryDriverRepository implements IDriverRepository {
|
||||
const driver = this.drivers.get(id);
|
||||
if (driver) {
|
||||
this.drivers.delete(id);
|
||||
this.iracingIdIndex.delete(driver.iracingId);
|
||||
this.iracingIdIndex.delete(driver.iracingId.toString());
|
||||
this.logger.info(`Driver ${id} deleted successfully.`);
|
||||
} else {
|
||||
this.logger.warn(`Driver with ID ${id} not found for deletion.`);
|
||||
|
||||
Reference in New Issue
Block a user