105 lines
2.8 KiB
TypeScript
105 lines
2.8 KiB
TypeScript
/**
|
|
* 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';
|
|
|
|
export class InMemoryCarRepository implements ICarRepository {
|
|
private cars: Map<string, Car>;
|
|
|
|
constructor(seedData?: Car[]) {
|
|
this.cars = new Map();
|
|
|
|
if (seedData) {
|
|
seedData.forEach(car => {
|
|
this.cars.set(car.id, car);
|
|
});
|
|
}
|
|
}
|
|
|
|
async findById(id: string): Promise<Car | null> {
|
|
return this.cars.get(id) ?? null;
|
|
}
|
|
|
|
async findAll(): Promise<Car[]> {
|
|
return Array.from(this.cars.values());
|
|
}
|
|
|
|
async findByGameId(gameId: string): Promise<Car[]> {
|
|
return Array.from(this.cars.values())
|
|
.filter(car => car.gameId === gameId)
|
|
.sort((a, b) => a.name.localeCompare(b.name));
|
|
}
|
|
|
|
async findByClass(carClass: CarClass): Promise<Car[]> {
|
|
return Array.from(this.cars.values())
|
|
.filter(car => car.carClass === carClass)
|
|
.sort((a, b) => a.name.localeCompare(b.name));
|
|
}
|
|
|
|
async findByLicense(license: CarLicense): Promise<Car[]> {
|
|
return Array.from(this.cars.values())
|
|
.filter(car => car.license === license)
|
|
.sort((a, b) => a.name.localeCompare(b.name));
|
|
}
|
|
|
|
async findByManufacturer(manufacturer: string): Promise<Car[]> {
|
|
const lowerManufacturer = manufacturer.toLowerCase();
|
|
return Array.from(this.cars.values())
|
|
.filter(car => car.manufacturer.toLowerCase() === lowerManufacturer)
|
|
.sort((a, b) => a.name.localeCompare(b.name));
|
|
}
|
|
|
|
async searchByName(query: string): Promise<Car[]> {
|
|
const lowerQuery = query.toLowerCase();
|
|
return 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));
|
|
}
|
|
|
|
async create(car: Car): Promise<Car> {
|
|
if (await this.exists(car.id)) {
|
|
throw new Error(`Car with ID ${car.id} already exists`);
|
|
}
|
|
|
|
this.cars.set(car.id, car);
|
|
return car;
|
|
}
|
|
|
|
async update(car: Car): Promise<Car> {
|
|
if (!await this.exists(car.id)) {
|
|
throw new Error(`Car with ID ${car.id} not found`);
|
|
}
|
|
|
|
this.cars.set(car.id, car);
|
|
return car;
|
|
}
|
|
|
|
async delete(id: string): Promise<void> {
|
|
if (!await this.exists(id)) {
|
|
throw new Error(`Car with ID ${id} not found`);
|
|
}
|
|
|
|
this.cars.delete(id);
|
|
}
|
|
|
|
async exists(id: string): Promise<boolean> {
|
|
return this.cars.has(id);
|
|
}
|
|
|
|
/**
|
|
* Utility method to generate a new UUID
|
|
*/
|
|
static generateId(): string {
|
|
return uuidv4();
|
|
}
|
|
} |