234 lines
7.9 KiB
TypeScript
234 lines
7.9 KiB
TypeScript
/**
|
|
* Infrastructure Adapter: InMemoryResultRepository
|
|
*
|
|
* In-memory implementation of IResultRepository.
|
|
* Stores data in Map structure with UUID generation.
|
|
*/
|
|
|
|
import { v4 as uuidv4 } from 'uuid';
|
|
import { Result } from '@gridpilot/racing/domain/entities/Result';
|
|
import type { IResultRepository } from '@gridpilot/racing/domain/repositories/IResultRepository';
|
|
import type { IRaceRepository } from '@gridpilot/racing/domain/repositories/IRaceRepository';
|
|
import type { Logger } from '@gridpilot/shared/logging/Logger';
|
|
|
|
export class InMemoryResultRepository implements IResultRepository {
|
|
private results: Map<string, Result>;
|
|
private raceRepository: IRaceRepository | null;
|
|
private readonly logger: Logger;
|
|
|
|
constructor(logger: Logger, seedData?: Result[], raceRepository?: IRaceRepository | null) {
|
|
this.logger = logger;
|
|
this.logger.info('InMemoryResultRepository initialized.');
|
|
this.results = new Map();
|
|
this.raceRepository = raceRepository ?? null;
|
|
|
|
if (seedData) {
|
|
seedData.forEach(result => {
|
|
this.results.set(result.id, result);
|
|
this.logger.debug(`Seeded result: ${result.id}`);
|
|
});
|
|
}
|
|
}
|
|
|
|
async findById(id: string): Promise<Result | null> {
|
|
this.logger.debug(`Finding result by id: ${id}`);
|
|
try {
|
|
const result = this.results.get(id) ?? null;
|
|
if (result) {
|
|
this.logger.info(`Found result with id: ${id}.`);
|
|
} else {
|
|
this.logger.warn(`Result with id ${id} not found.`);
|
|
}
|
|
return result;
|
|
} catch (error) {
|
|
this.logger.error(`Error finding result by id ${id}:`, error);
|
|
throw error;
|
|
}
|
|
}
|
|
|
|
async findAll(): Promise<Result[]> {
|
|
this.logger.debug('Finding all results.');
|
|
try {
|
|
const results = Array.from(this.results.values());
|
|
this.logger.info(`Found ${results.length} results.`);
|
|
return results;
|
|
} catch (error) {
|
|
this.logger.error('Error finding all results:', error);
|
|
throw error;
|
|
}
|
|
}
|
|
|
|
async findByRaceId(raceId: string): Promise<Result[]> {
|
|
this.logger.debug(`Finding results for race id: ${raceId}`);
|
|
try {
|
|
const results = Array.from(this.results.values())
|
|
.filter(result => result.raceId === raceId)
|
|
.sort((a, b) => a.position - b.position);
|
|
this.logger.info(`Found ${results.length} results for race id: ${raceId}.`);
|
|
return results;
|
|
} catch (error) {
|
|
this.logger.error(`Error finding results for race id ${raceId}:`, error);
|
|
throw error;
|
|
}
|
|
}
|
|
|
|
async findByDriverId(driverId: string): Promise<Result[]> {
|
|
this.logger.debug(`Finding results for driver id: ${driverId}`);
|
|
try {
|
|
const results = Array.from(this.results.values())
|
|
.filter(result => result.driverId === driverId);
|
|
this.logger.info(`Found ${results.length} results for driver id: ${driverId}.`);
|
|
return results;
|
|
} catch (error) {
|
|
this.logger.error(`Error finding results for driver id ${driverId}:`, error);
|
|
throw error;
|
|
}
|
|
}
|
|
|
|
async findByDriverIdAndLeagueId(driverId: string, leagueId: string): Promise<Result[]> {
|
|
this.logger.debug(`Finding results for driver id: ${driverId} and league id: ${leagueId}`);
|
|
try {
|
|
if (!this.raceRepository) {
|
|
this.logger.warn('Race repository not provided to InMemoryResultRepository. Skipping league-filtered search.');
|
|
return [];
|
|
}
|
|
|
|
const leagueRaces = await this.raceRepository.findByLeagueId(leagueId);
|
|
const leagueRaceIds = new Set(leagueRaces.map(race => race.id));
|
|
this.logger.debug(`Found ${leagueRaces.length} races in league ${leagueId}.`);
|
|
|
|
const results = Array.from(this.results.values())
|
|
.filter(result =>
|
|
result.driverId === driverId &&
|
|
leagueRaceIds.has(result.raceId)
|
|
);
|
|
this.logger.info(`Found ${results.length} results for driver ${driverId} in league ${leagueId}.`);
|
|
return results;
|
|
} catch (error) {
|
|
this.logger.error(`Error finding results for driver ${driverId} and league ${leagueId}:`, error);
|
|
throw error;
|
|
}
|
|
}
|
|
|
|
async create(result: Result): Promise<Result> {
|
|
this.logger.debug(`Creating result: ${result.id}`);
|
|
try {
|
|
if (await this.exists(result.id)) {
|
|
this.logger.warn(`Result with ID ${result.id} already exists. Throwing error.`);
|
|
throw new Error(`Result with ID ${result.id} already exists`);
|
|
}
|
|
|
|
this.results.set(result.id, result);
|
|
this.logger.info(`Result ${result.id} created successfully.`);
|
|
return result;
|
|
} catch (error) {
|
|
this.logger.error(`Error creating result ${result.id}:`, error);
|
|
throw error;
|
|
}
|
|
}
|
|
|
|
async createMany(results: Result[]): Promise<Result[]> {
|
|
this.logger.debug(`Creating ${results.length} results.`);
|
|
try {
|
|
const created: Result[] = [];
|
|
|
|
for (const result of results) {
|
|
if (await this.exists(result.id)) {
|
|
this.logger.warn(`Result with ID ${result.id} already exists. Skipping creation.`);
|
|
// In a real system, decide if this should throw or log and skip
|
|
continue;
|
|
}
|
|
this.results.set(result.id, result);
|
|
created.push(result);
|
|
}
|
|
this.logger.info(`Created ${created.length} results successfully.`);
|
|
|
|
return created;
|
|
} catch (error) {
|
|
this.logger.error(`Error creating many results:`, error);
|
|
throw error;
|
|
}
|
|
}
|
|
|
|
async update(result: Result): Promise<Result> {
|
|
this.logger.debug(`Updating result: ${result.id}`);
|
|
try {
|
|
if (!await this.exists(result.id)) {
|
|
this.logger.warn(`Result with ID ${result.id} not found for update. Throwing error.`);
|
|
throw new Error(`Result with ID ${result.id} not found`);
|
|
}
|
|
|
|
this.results.set(result.id, result);
|
|
this.logger.info(`Result ${result.id} updated successfully.`);
|
|
return result;
|
|
} catch (error) {
|
|
this.logger.error(`Error updating result ${result.id}:`, error);
|
|
throw error;
|
|
}
|
|
}
|
|
|
|
async delete(id: string): Promise<void> {
|
|
this.logger.debug(`Deleting result: ${id}`);
|
|
try {
|
|
if (!await this.exists(id)) {
|
|
this.logger.warn(`Result with ID ${id} not found for deletion. Throwing error.`);
|
|
throw new Error(`Result with ID ${id} not found`);
|
|
}
|
|
|
|
this.results.delete(id);
|
|
this.logger.info(`Result ${id} deleted successfully.`);
|
|
} catch (error) {
|
|
this.logger.error(`Error deleting result ${id}:`, error);
|
|
throw error;
|
|
}
|
|
}
|
|
|
|
async deleteByRaceId(raceId: string): Promise<void> {
|
|
this.logger.debug(`Deleting results for race id: ${raceId}`);
|
|
try {
|
|
const initialCount = this.results.size;
|
|
const raceResults = Array.from(this.results.values()).filter(
|
|
result => result.raceId === raceId
|
|
);
|
|
raceResults.forEach(result => {
|
|
this.results.delete(result.id);
|
|
});
|
|
this.logger.info(`Deleted ${raceResults.length} results for race id: ${raceId}.`);
|
|
} catch (error) {
|
|
this.logger.error(`Error deleting results for race id ${raceId}:`, error);
|
|
throw error;
|
|
}
|
|
}
|
|
|
|
async exists(id: string): Promise<boolean> {
|
|
this.logger.debug(`Checking existence of result with id: ${id}`);
|
|
try {
|
|
const exists = this.results.has(id);
|
|
this.logger.debug(`Result ${id} exists: ${exists}.`);
|
|
return exists;
|
|
} catch (error) {
|
|
this.logger.error(`Error checking existence of result with id ${id}:`, error);
|
|
throw error;
|
|
}
|
|
}
|
|
|
|
async existsByRaceId(raceId: string): Promise<boolean> {
|
|
this.logger.debug(`Checking existence of results for race id: ${raceId}`);
|
|
try {
|
|
const exists = Array.from(this.results.values()).some(
|
|
result => result.raceId === raceId
|
|
);
|
|
this.logger.debug(`Results for race ${raceId} exist: ${exists}.`);
|
|
return exists;
|
|
} catch (error) {
|
|
this.logger.error(`Error checking existence of results for race id ${raceId}:`, error);
|
|
throw error;
|
|
}
|
|
|
|
/**
|
|
* Utility method to generate a new UUID
|
|
*/
|
|
static generateId(): string {
|
|
return uuidv4();
|
|
}
|
|
} |