refactor
This commit is contained in:
@@ -6,7 +6,9 @@
|
||||
*/
|
||||
|
||||
import { v4 as uuidv4 } from 'uuid';
|
||||
import { Car, CarClass, CarLicense } from '@core/racing/domain/entities/Car';
|
||||
import { Car } from '@core/racing/domain/entities/Car';
|
||||
import { CarClass } from '@core/racing/domain/entities/CarClass';
|
||||
import { CarLicense } from '@core/racing/domain/entities/CarLicense';
|
||||
import type { ICarRepository } from '@core/racing/domain/repositories/ICarRepository';
|
||||
import type { Logger } from '@core/shared/application';
|
||||
|
||||
@@ -14,19 +16,11 @@ export class InMemoryCarRepository implements ICarRepository {
|
||||
private cars: Map<string, Car>;
|
||||
private readonly logger: Logger;
|
||||
|
||||
constructor(logger: Logger, seedData?: Car[]) {
|
||||
constructor(logger: Logger) {
|
||||
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.`);
|
||||
});
|
||||
}
|
||||
this.logger.info('InMemoryCarRepository initialized');
|
||||
}
|
||||
|
||||
async findById(id: string): Promise<Car | null> {
|
||||
@@ -61,8 +55,8 @@ export class InMemoryCarRepository implements ICarRepository {
|
||||
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));
|
||||
.filter(car => car.gameId.toString() === gameId)
|
||||
.sort((a, b) => a.name.toString().localeCompare(b.name.toString()));
|
||||
this.logger.info(`Successfully found ${cars.length} cars for game ID: ${gameId}.`);
|
||||
return cars;
|
||||
} catch (error) {
|
||||
@@ -75,8 +69,8 @@ export class InMemoryCarRepository implements ICarRepository {
|
||||
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));
|
||||
.filter(car => car.carClass.equals(carClass))
|
||||
.sort((a, b) => a.name.toString().localeCompare(b.name.toString()));
|
||||
this.logger.info(`Successfully found ${cars.length} cars for class: ${carClass}.`);
|
||||
return cars;
|
||||
} catch (error) {
|
||||
@@ -89,23 +83,23 @@ export class InMemoryCarRepository implements ICarRepository {
|
||||
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));
|
||||
.filter(car => car.license.equals(license))
|
||||
.sort((a, b) => a.name.toString().localeCompare(b.name.toString()));
|
||||
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 instanceof Error ? error : new Error(String(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));
|
||||
.filter(car => car.manufacturer.toString().toLowerCase() === lowerManufacturer)
|
||||
.sort((a, b) => a.name.toString().localeCompare(b.name.toString()));
|
||||
this.logger.info(`Successfully found ${cars.length} cars for manufacturer: ${manufacturer}.`);
|
||||
return cars;
|
||||
} catch (error) {
|
||||
@@ -120,11 +114,11 @@ export class InMemoryCarRepository implements ICarRepository {
|
||||
const lowerQuery = query.toLowerCase();
|
||||
const cars = Array.from(this.cars.values())
|
||||
.filter(car =>
|
||||
car.name.toLowerCase().includes(lowerQuery) ||
|
||||
car.name.toString().toLowerCase().includes(lowerQuery) ||
|
||||
car.shortName.toLowerCase().includes(lowerQuery) ||
|
||||
car.manufacturer.toLowerCase().includes(lowerQuery)
|
||||
car.manufacturer.toString().toLowerCase().includes(lowerQuery)
|
||||
)
|
||||
.sort((a, b) => a.name.localeCompare(b.name));
|
||||
.sort((a, b) => a.name.toString().localeCompare(b.name.toString()));
|
||||
this.logger.info(`Successfully found ${cars.length} cars matching search query: ${query}.`);
|
||||
return cars;
|
||||
} catch (error) {
|
||||
@@ -136,12 +130,12 @@ export class InMemoryCarRepository implements ICarRepository {
|
||||
async create(car: Car): Promise<Car> {
|
||||
this.logger.debug(`Attempting to create car: ${car.id}.`);
|
||||
try {
|
||||
if (await this.exists(car.id)) {
|
||||
if (await this.exists(car.id.toString())) {
|
||||
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.cars.set(car.id.toString(), car);
|
||||
this.logger.info(`Car ${car.id} created successfully.`);
|
||||
return car;
|
||||
} catch (error) {
|
||||
@@ -153,12 +147,12 @@ export class InMemoryCarRepository implements ICarRepository {
|
||||
async update(car: Car): Promise<Car> {
|
||||
this.logger.debug(`Attempting to update car with ID: ${car.id}.`);
|
||||
try {
|
||||
if (!await this.exists(car.id)) {
|
||||
if (!await this.exists(car.id.toString())) {
|
||||
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.cars.set(car.id.toString(), car);
|
||||
this.logger.info(`Car ${car.id} updated successfully.`);
|
||||
return car;
|
||||
} catch (error) {
|
||||
|
||||
Reference in New Issue
Block a user