163 lines
5.1 KiB
TypeScript
163 lines
5.1 KiB
TypeScript
/**
|
|
* Infrastructure Adapter: InMemoryDriverRepository
|
|
*
|
|
* In-memory implementation of IDriverRepository.
|
|
* Stores data in Map structure with UUID generation.
|
|
*/
|
|
|
|
import { v4 as uuidv4 } from 'uuid';
|
|
import { Driver } from '@gridpilot/racing/domain/entities/Driver';
|
|
import type { IDriverRepository } from '@gridpilot/racing/domain/repositories/IDriverRepository';
|
|
import type { ILogger } from '@gridpilot/shared/logging/ILogger';
|
|
|
|
export class InMemoryDriverRepository implements IDriverRepository {
|
|
private drivers: Map<string, Driver>;
|
|
private readonly logger: ILogger;
|
|
|
|
constructor(logger: ILogger, seedData?: Driver[]) {
|
|
this.logger = logger;
|
|
this.logger.info('InMemoryDriverRepository initialized.');
|
|
this.drivers = new Map();
|
|
|
|
if (seedData) {
|
|
seedData.forEach(driver => {
|
|
this.drivers.set(driver.id, driver);
|
|
this.logger.debug(`Seeded driver: ${driver.id}.`);
|
|
});
|
|
}
|
|
}
|
|
|
|
async findById(id: string): Promise<Driver | null> {
|
|
this.logger.debug(`Finding driver by id: ${id}`);
|
|
try {
|
|
const driver = this.drivers.get(id) ?? null;
|
|
if (driver) {
|
|
this.logger.info(`Found driver: ${id}.`);
|
|
} else {
|
|
this.logger.warn(`Driver with id ${id} not found.`);
|
|
}
|
|
return driver;
|
|
} catch (error) {
|
|
this.logger.error(`Error finding driver by id ${id}:`, error);
|
|
throw error;
|
|
}
|
|
}
|
|
|
|
async findByIRacingId(iracingId: string): Promise<Driver | null> {
|
|
this.logger.debug(`Finding driver by iRacing id: ${iracingId}`);
|
|
try {
|
|
const driver = Array.from(this.drivers.values()).find(
|
|
d => d.iracingId === iracingId
|
|
);
|
|
if (driver) {
|
|
this.logger.info(`Found driver with iRacing id: ${iracingId}.`);
|
|
} else {
|
|
this.logger.warn(`Driver with iRacing id ${iracingId} not found.`);
|
|
}
|
|
return driver ?? null;
|
|
} catch (error) {
|
|
this.logger.error(`Error finding driver by iRacing id ${iracingId}:`, error);
|
|
throw error;
|
|
}
|
|
}
|
|
|
|
async findAll(): Promise<Driver[]> {
|
|
this.logger.debug('Finding all drivers.');
|
|
try {
|
|
const drivers = Array.from(this.drivers.values());
|
|
this.logger.info(`Found ${drivers.length} drivers.`);
|
|
return drivers;
|
|
} catch (error) {
|
|
this.logger.error('Error finding all drivers:', error);
|
|
throw error;
|
|
}
|
|
}
|
|
|
|
async create(driver: Driver): Promise<Driver> {
|
|
this.logger.debug(`Creating driver: ${driver.id}`);
|
|
try {
|
|
if (await this.exists(driver.id)) {
|
|
this.logger.warn(`Driver with ID ${driver.id} already exists.`);
|
|
throw new Error(`Driver with ID ${driver.id} already exists`);
|
|
}
|
|
|
|
if (await this.existsByIRacingId(driver.iracingId)) {
|
|
this.logger.warn(`Driver with iRacing ID ${driver.iracingId} already exists.`);
|
|
throw new Error(`Driver with iRacing ID ${driver.iracingId} already exists`);
|
|
}
|
|
|
|
this.drivers.set(driver.id, driver);
|
|
this.logger.info(`Driver ${driver.id} created successfully.`);
|
|
return driver;
|
|
} catch (error) {
|
|
this.logger.error(`Error creating driver ${driver.id}:`, error);
|
|
throw error;
|
|
}
|
|
}
|
|
|
|
async update(driver: Driver): Promise<Driver> {
|
|
this.logger.debug(`Updating driver: ${driver.id}`);
|
|
try {
|
|
if (!await this.exists(driver.id)) {
|
|
this.logger.warn(`Driver with ID ${driver.id} not found for update.`);
|
|
throw new Error(`Driver with ID ${driver.id} not found`);
|
|
}
|
|
|
|
this.drivers.set(driver.id, driver);
|
|
this.logger.info(`Driver ${driver.id} updated successfully.`);
|
|
return driver;
|
|
} catch (error) {
|
|
this.logger.error(`Error updating driver ${driver.id}:`, error);
|
|
throw error;
|
|
}
|
|
}
|
|
|
|
async delete(id: string): Promise<void> {
|
|
this.logger.debug(`Deleting driver: ${id}`);
|
|
try {
|
|
if (!await this.exists(id)) {
|
|
this.logger.warn(`Driver with ID ${id} not found for deletion.`);
|
|
throw new Error(`Driver with ID ${id} not found`);
|
|
}
|
|
|
|
this.drivers.delete(id);
|
|
this.logger.info(`Driver ${id} deleted successfully.`);
|
|
} catch (error) {
|
|
this.logger.error(`Error deleting driver ${id}:`, error);
|
|
throw error;
|
|
}
|
|
}
|
|
|
|
async exists(id: string): Promise<boolean> {
|
|
this.logger.debug(`Checking existence of driver with id: ${id}`);
|
|
try {
|
|
const exists = this.drivers.has(id);
|
|
this.logger.debug(`Driver ${id} exists: ${exists}.`);
|
|
return exists;
|
|
} catch (error) {
|
|
this.logger.error(`Error checking existence of driver with id ${id}:`, error);
|
|
throw error;
|
|
}
|
|
}
|
|
|
|
async existsByIRacingId(iracingId: string): Promise<boolean> {
|
|
this.logger.debug(`Checking existence of driver with iRacing id: ${iracingId}`);
|
|
try {
|
|
const exists = Array.from(this.drivers.values()).some(
|
|
d => d.iracingId === iracingId
|
|
);
|
|
this.logger.debug(`Driver with iRacing id ${iracingId} exists: ${exists}.`);
|
|
return exists;
|
|
} catch (error) {
|
|
this.logger.error(`Error checking existence of driver with iRacing id ${iracingId}:`, error);
|
|
throw error;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Utility method to generate a new UUID
|
|
*/
|
|
static generateId(): string {
|
|
return uuidv4();
|
|
}
|
|
} |