refactor adapters
This commit is contained in:
204
adapters/racing/persistence/inmemory/InMemoryCarRepository.ts
Normal file
204
adapters/racing/persistence/inmemory/InMemoryCarRepository.ts
Normal file
@@ -0,0 +1,204 @@
|
||||
/**
|
||||
* Infrastructure Adapter: InMemoryCarRepository
|
||||
*
|
||||
* In-memory implementation of ICarRepository.
|
||||
* Stores data in Map structure with UUID generation.
|
||||
*/
|
||||
|
||||
import { v4 as uuidv4 } from 'uuid';
|
||||
import { Car, CarClass, CarLicense } from '@gridpilot/racing/domain/entities/Car';
|
||||
import type { ICarRepository } from '@gridpilot/racing/domain/repositories/ICarRepository';
|
||||
import type { ILogger } from '@gridpilot/shared/logging/ILogger';
|
||||
|
||||
export class InMemoryCarRepository implements ICarRepository {
|
||||
private cars: Map<string, Car>;
|
||||
private readonly logger: ILogger;
|
||||
|
||||
constructor(logger: ILogger, seedData?: Car[]) {
|
||||
this.logger = logger;
|
||||
this.cars = new Map();
|
||||
|
||||
this.logger.info('InMemoryCarRepository initialized');
|
||||
|
||||
if (seedData) {
|
||||
this.logger.debug(`Seeding ${seedData.length} cars.`);
|
||||
seedData.forEach(car => {
|
||||
this.cars.set(car.id, car);
|
||||
this.logger.debug(`Car ${car.id} seeded.`);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
async findById(id: string): Promise<Car | null> {
|
||||
this.logger.debug(`Attempting to find car with ID: ${id}.`);
|
||||
try {
|
||||
const car = this.cars.get(id) ?? null;
|
||||
if (car) {
|
||||
this.logger.info(`Successfully found car with ID: ${id}.`);
|
||||
} else {
|
||||
this.logger.warn(`Car with ID: ${id} not found.`);
|
||||
}
|
||||
return car;
|
||||
} catch (error) {
|
||||
this.logger.error(`Error finding car by ID ${id}:`, error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
async findAll(): Promise<Car[]> {
|
||||
this.logger.debug('Attempting to find all cars.');
|
||||
try {
|
||||
const cars = Array.from(this.cars.values());
|
||||
this.logger.info(`Successfully found ${cars.length} cars.`);
|
||||
return cars;
|
||||
} catch (error) {
|
||||
this.logger.error('Error finding all cars:', error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
async findByGameId(gameId: string): Promise<Car[]> {
|
||||
this.logger.debug(`Attempting to find cars by game ID: ${gameId}.`);
|
||||
try {
|
||||
const cars = Array.from(this.cars.values())
|
||||
.filter(car => car.gameId === gameId)
|
||||
.sort((a, b) => a.name.localeCompare(b.name));
|
||||
this.logger.info(`Successfully found ${cars.length} cars for game ID: ${gameId}.`);
|
||||
return cars;
|
||||
} catch (error) {
|
||||
this.logger.error(`Error finding cars by game ID ${gameId}:`, error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
async findByClass(carClass: CarClass): Promise<Car[]> {
|
||||
this.logger.debug(`Attempting to find cars by class: ${carClass}.`);
|
||||
try {
|
||||
const cars = Array.from(this.cars.values())
|
||||
.filter(car => car.carClass === carClass)
|
||||
.sort((a, b) => a.name.localeCompare(b.name));
|
||||
this.logger.info(`Successfully found ${cars.length} cars for class: ${carClass}.`);
|
||||
return cars;
|
||||
} catch (error) {
|
||||
this.logger.error(`Error finding cars by class ${carClass}:`, error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
async findByLicense(license: CarLicense): Promise<Car[]> {
|
||||
this.logger.debug(`Attempting to find cars by license: ${license}.`);
|
||||
try {
|
||||
const cars = Array.from(this.cars.values())
|
||||
.filter(car => car.license === license)
|
||||
.sort((a, b) => a.name.localeCompare(b.name));
|
||||
this.logger.info(`Successfully found ${cars.length} cars for license: ${license}.`);
|
||||
return cars;
|
||||
} catch (error) {
|
||||
this.logger.error(`Error finding cars by license ${license}:`, error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
async findByManufacturer(manufacturer: string): Promise<Car[]> {
|
||||
this.logger.debug(`Attempting to find cars by manufacturer: ${manufacturer}.`);
|
||||
try {
|
||||
const lowerManufacturer = manufacturer.toLowerCase();
|
||||
const cars = Array.from(this.cars.values())
|
||||
.filter(car => car.manufacturer.toLowerCase() === lowerManufacturer)
|
||||
.sort((a, b) => a.name.localeCompare(b.name));
|
||||
this.logger.info(`Successfully found ${cars.length} cars for manufacturer: ${manufacturer}.`);
|
||||
return cars;
|
||||
} catch (error) {
|
||||
this.logger.error(`Error finding cars by manufacturer ${manufacturer}:`, error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
async searchByName(query: string): Promise<Car[]> {
|
||||
this.logger.debug(`Attempting to search cars by name query: ${query}.`);
|
||||
try {
|
||||
const lowerQuery = query.toLowerCase();
|
||||
const cars = Array.from(this.cars.values())
|
||||
.filter(car =>
|
||||
car.name.toLowerCase().includes(lowerQuery) ||
|
||||
car.shortName.toLowerCase().includes(lowerQuery) ||
|
||||
car.manufacturer.toLowerCase().includes(lowerQuery)
|
||||
)
|
||||
.sort((a, b) => a.name.localeCompare(b.name));
|
||||
this.logger.info(`Successfully found ${cars.length} cars matching search query: ${query}.`);
|
||||
return cars;
|
||||
} catch (error) {
|
||||
this.logger.error(`Error searching cars by name query ${query}:`, error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
async create(car: Car): Promise<Car> {
|
||||
this.logger.debug(`Attempting to create car: ${car.id}.`);
|
||||
try {
|
||||
if (await this.exists(car.id)) {
|
||||
this.logger.warn(`Car with ID ${car.id} already exists; creation aborted.`);
|
||||
throw new Error(`Car with ID ${car.id} already exists`);
|
||||
}
|
||||
|
||||
this.cars.set(car.id, car);
|
||||
this.logger.info(`Car ${car.id} created successfully.`);
|
||||
return car;
|
||||
} catch (error) {
|
||||
this.logger.error(`Error creating car ${car.id}:`, error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
async update(car: Car): Promise<Car> {
|
||||
this.logger.debug(`Attempting to update car with ID: ${car.id}.`);
|
||||
try {
|
||||
if (!await this.exists(car.id)) {
|
||||
this.logger.warn(`Car with ID ${car.id} not found for update; update aborted.`);
|
||||
throw new Error(`Car with ID ${car.id} not found`);
|
||||
}
|
||||
|
||||
this.cars.set(car.id, car);
|
||||
this.logger.info(`Car ${car.id} updated successfully.`);
|
||||
return car;
|
||||
} catch (error) {
|
||||
this.logger.error(`Error updating car ${car.id}:`, error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
async delete(id: string): Promise<void> {
|
||||
this.logger.debug(`Attempting to delete car with ID: ${id}.`);
|
||||
try {
|
||||
if (!await this.exists(id)) {
|
||||
this.logger.warn(`Car with ID ${id} not found for deletion; deletion aborted.`);
|
||||
throw new Error(`Car with ID ${id} not found`);
|
||||
}
|
||||
|
||||
this.cars.delete(id);
|
||||
this.logger.info(`Car ${id} deleted successfully.`);
|
||||
} catch (error) {
|
||||
this.logger.error(`Error deleting car ${id}:`, error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
async exists(id: string): Promise<boolean> {
|
||||
this.logger.debug(`Checking existence of car with ID: ${id}.`);
|
||||
try {
|
||||
const exists = this.cars.has(id);
|
||||
this.logger.info(`Car with ID ${id} existence check: ${exists}.`);
|
||||
return exists;
|
||||
} catch (error) {
|
||||
this.logger.error(`Error checking existence of car with ID ${id}:`, error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Utility method to generate a new UUID
|
||||
*/
|
||||
static generateId(): string {
|
||||
return uuidv4();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user