module creation
This commit is contained in:
@@ -1,207 +1,110 @@
|
||||
/**
|
||||
* Infrastructure Adapter: InMemoryRaceRepository
|
||||
*
|
||||
* In-memory implementation of IRaceRepository.
|
||||
* Stores data in Map structure with UUID generation.
|
||||
*/
|
||||
|
||||
import { v4 as uuidv4 } from 'uuid';
|
||||
import { IRaceRepository } from '@gridpilot/racing/domain/repositories/IRaceRepository';
|
||||
import { Race, RaceStatus } from '@gridpilot/racing/domain/entities/Race';
|
||||
import type { IRaceRepository } from '@gridpilot/racing/domain/repositories/IRaceRepository';
|
||||
import type { ILogger } from '@gridpilot/shared/logging/ILogger';
|
||||
import { ILogger } from '@gridpilot/shared/logging/ILogger';
|
||||
|
||||
export class InMemoryRaceRepository implements IRaceRepository {
|
||||
private races: Map<string, Race>;
|
||||
private readonly logger: ILogger;
|
||||
private races: Map<string, Race> = new Map();
|
||||
|
||||
constructor(logger: ILogger, seedData?: Race[]) {
|
||||
this.logger = logger;
|
||||
constructor(private readonly logger: ILogger, initialRaces: Race[] = []) {
|
||||
this.logger.info('InMemoryRaceRepository initialized.');
|
||||
this.races = new Map();
|
||||
|
||||
if (seedData) {
|
||||
seedData.forEach(race => {
|
||||
this.races.set(race.id, race);
|
||||
this.logger.debug(`Seeded race: ${race.id}.`);
|
||||
});
|
||||
for (const race of initialRaces) {
|
||||
this.races.set(race.id, race);
|
||||
this.logger.debug(`Seeded race: ${race.id} (${race.track}).`);
|
||||
}
|
||||
}
|
||||
|
||||
async findById(id: string): Promise<Race | null> {
|
||||
this.logger.debug(`Finding race by id: ${id}`);
|
||||
try {
|
||||
const race = this.races.get(id) ?? null;
|
||||
if (race) {
|
||||
this.logger.info(`Found race: ${id}.`);
|
||||
} else {
|
||||
this.logger.warn(`Race with id ${id} not found.`);
|
||||
}
|
||||
return race;
|
||||
} catch (error) {
|
||||
this.logger.error(`Error finding race by id ${id}:`, error);
|
||||
throw error;
|
||||
this.logger.debug(`[InMemoryRaceRepository] Finding race by ID: ${id}`);
|
||||
const race = this.races.get(id) ?? null;
|
||||
if (race) {
|
||||
this.logger.info(`Found race by ID: ${id}.`);
|
||||
} else {
|
||||
this.logger.warn(`Race with ID ${id} not found.`);
|
||||
}
|
||||
return Promise.resolve(race);
|
||||
}
|
||||
|
||||
async findAll(): Promise<Race[]> {
|
||||
this.logger.debug('Finding all races.');
|
||||
try {
|
||||
const races = Array.from(this.races.values());
|
||||
this.logger.info(`Found ${races.length} races.`);
|
||||
return races;
|
||||
} catch (error) {
|
||||
this.logger.error('Error finding all races:', error);
|
||||
throw error;
|
||||
}
|
||||
this.logger.debug('[InMemoryRaceRepository] Finding all races.');
|
||||
return Promise.resolve(Array.from(this.races.values()));
|
||||
}
|
||||
|
||||
async findByLeagueId(leagueId: string): Promise<Race[]> {
|
||||
this.logger.debug(`Finding races by league id: ${leagueId}`);
|
||||
try {
|
||||
const races = Array.from(this.races.values())
|
||||
.filter(race => race.leagueId === leagueId)
|
||||
.sort((a, b) => a.scheduledAt.getTime() - b.scheduledAt.getTime());
|
||||
this.logger.info(`Found ${races.length} races for league id: ${leagueId}.`);
|
||||
return races;
|
||||
} catch (error) {
|
||||
this.logger.error(`Error finding races by league id ${leagueId}:`, error);
|
||||
throw error;
|
||||
}
|
||||
this.logger.debug(`[InMemoryRaceRepository] Finding races by league ID: ${leagueId}`);
|
||||
const races = Array.from(this.races.values()).filter(race => race.leagueId === leagueId);
|
||||
this.logger.info(`Found ${races.length} races for league ID: ${leagueId}.`);
|
||||
return Promise.resolve(races);
|
||||
}
|
||||
|
||||
async findUpcomingByLeagueId(leagueId: string): Promise<Race[]> {
|
||||
this.logger.debug(`Finding upcoming races by league id: ${leagueId}`);
|
||||
try {
|
||||
const now = new Date();
|
||||
const races = Array.from(this.races.values())
|
||||
.filter(race =>
|
||||
race.leagueId === leagueId &&
|
||||
race.status === 'scheduled' &&
|
||||
race.scheduledAt > now
|
||||
)
|
||||
.sort((a, b) => a.scheduledAt.getTime() - b.scheduledAt.getTime());
|
||||
this.logger.info(`Found ${races.length} upcoming races for league id: ${leagueId}.`);
|
||||
return races;
|
||||
} catch (error) {
|
||||
this.logger.error(`Error finding upcoming races by league id ${leagueId}:`, error);
|
||||
throw error;
|
||||
}
|
||||
this.logger.debug(`[InMemoryRaceRepository] Finding upcoming races by league ID: ${leagueId}`);
|
||||
const now = new Date();
|
||||
const upcomingRaces = Array.from(this.races.values()).filter(race =>
|
||||
race.leagueId === leagueId && race.status === 'scheduled' && race.scheduledAt > now
|
||||
);
|
||||
this.logger.info(`Found ${upcomingRaces.length} upcoming races for league ID: ${leagueId}.`);
|
||||
return Promise.resolve(upcomingRaces);
|
||||
}
|
||||
|
||||
async findCompletedByLeagueId(leagueId: string): Promise<Race[]> {
|
||||
this.logger.debug(`Finding completed races by league id: ${leagueId}`);
|
||||
try {
|
||||
const races = Array.from(this.races.values())
|
||||
.filter(race =>
|
||||
race.leagueId === leagueId &&
|
||||
race.status === 'completed'
|
||||
)
|
||||
.sort((a, b) => b.scheduledAt.getTime() - a.scheduledAt.getTime());
|
||||
this.logger.info(`Found ${races.length} completed races for league id: ${leagueId}.`);
|
||||
return races;
|
||||
} catch (error) {
|
||||
this.logger.error(`Error finding completed races by league id ${leagueId}:`, error);
|
||||
throw error;
|
||||
}
|
||||
this.logger.debug(`[InMemoryRaceRepository] Finding completed races by league ID: ${leagueId}`);
|
||||
const completedRaces = Array.from(this.races.values()).filter(race =>
|
||||
race.leagueId === leagueId && race.status === 'completed'
|
||||
);
|
||||
this.logger.info(`Found ${completedRaces.length} completed races for league ID: ${leagueId}.`);
|
||||
return Promise.resolve(completedRaces);
|
||||
}
|
||||
|
||||
async findByStatus(status: RaceStatus): Promise<Race[]> {
|
||||
this.logger.debug(`Finding races by status: ${status}`);
|
||||
try {
|
||||
const races = Array.from(this.races.values())
|
||||
.filter(race => race.status === status)
|
||||
.sort((a, b) => a.scheduledAt.getTime() - b.scheduledAt.getTime());
|
||||
this.logger.info(`Found ${races.length} races with status: ${status}.`);
|
||||
return races;
|
||||
} catch (error) {
|
||||
this.logger.error(`Error finding races by status ${status}:`, error);
|
||||
throw error;
|
||||
}
|
||||
this.logger.debug(`[InMemoryRaceRepository] Finding races by status: ${status}.`);
|
||||
const races = Array.from(this.races.values()).filter(race => race.status === status);
|
||||
this.logger.info(`Found ${races.length} races with status: ${status}.`);
|
||||
return Promise.resolve(races);
|
||||
}
|
||||
|
||||
async findByDateRange(startDate: Date, endDate: Date): Promise<Race[]> {
|
||||
this.logger.debug(`Finding races by date range: ${startDate.toISOString()} - ${endDate.toISOString()}`);
|
||||
try {
|
||||
const races = Array.from(this.races.values())
|
||||
.filter(race =>
|
||||
race.scheduledAt >= startDate &&
|
||||
race.scheduledAt <= endDate
|
||||
)
|
||||
.sort((a, b) => a.scheduledAt.getTime() - b.scheduledAt.getTime());
|
||||
this.logger.info(`Found ${races.length} races in date range.`);
|
||||
return races;
|
||||
} catch (error) {
|
||||
this.logger.error(`Error finding races by date range:`, error);
|
||||
throw error;
|
||||
}
|
||||
this.logger.debug(`[InMemoryRaceRepository] Finding races by date range: ${startDate.toISOString()} - ${endDate.toISOString()}.`);
|
||||
const races = Array.from(this.races.values()).filter(race =>
|
||||
race.scheduledAt >= startDate && race.scheduledAt <= endDate
|
||||
);
|
||||
this.logger.info(`Found ${races.length} races within date range.`);
|
||||
return Promise.resolve(races);
|
||||
}
|
||||
|
||||
async create(race: Race): Promise<Race> {
|
||||
this.logger.debug(`Creating race: ${race.id}`);
|
||||
try {
|
||||
if (await this.exists(race.id)) {
|
||||
this.logger.warn(`Race with ID ${race.id} already exists.`);
|
||||
throw new Error(`Race with ID ${race.id} already exists`);
|
||||
}
|
||||
|
||||
this.races.set(race.id, race);
|
||||
this.logger.info(`Race ${race.id} created successfully.`);
|
||||
return race;
|
||||
} catch (error) {
|
||||
this.logger.error(`Error creating race ${race.id}:`, error);
|
||||
throw error;
|
||||
this.logger.debug(`[InMemoryRaceRepository] Creating race: ${race.id} (${race.track}).`);
|
||||
if (this.races.has(race.id)) {
|
||||
this.logger.warn(`Race with ID ${race.id} already exists.`);
|
||||
throw new Error('Race already exists');
|
||||
}
|
||||
this.races.set(race.id, race);
|
||||
this.logger.info(`Race ${race.id} created successfully.`);
|
||||
return Promise.resolve(race);
|
||||
}
|
||||
|
||||
async update(race: Race): Promise<Race> {
|
||||
this.logger.debug(`Updating race: ${race.id}`);
|
||||
try {
|
||||
if (!await this.exists(race.id)) {
|
||||
this.logger.warn(`Race with ID ${race.id} not found for update.`);
|
||||
throw new Error(`Race with ID ${race.id} not found`);
|
||||
}
|
||||
|
||||
this.races.set(race.id, race);
|
||||
this.logger.info(`Race ${race.id} updated successfully.`);
|
||||
return race;
|
||||
} catch (error) {
|
||||
this.logger.error(`Error updating race ${race.id}:`, error);
|
||||
throw error;
|
||||
this.logger.debug(`[InMemoryRaceRepository] Updating race: ${race.id} (${race.track}).`);
|
||||
if (!this.races.has(race.id)) {
|
||||
this.logger.warn(`Race with ID ${race.id} not found for update.`);
|
||||
throw new Error('Race not found');
|
||||
}
|
||||
this.races.set(race.id, race);
|
||||
this.logger.info(`Race ${race.id} updated successfully.`);
|
||||
return Promise.resolve(race);
|
||||
}
|
||||
|
||||
async delete(id: string): Promise<void> {
|
||||
this.logger.debug(`Deleting race: ${id}`);
|
||||
try {
|
||||
if (!await this.exists(id)) {
|
||||
this.logger.warn(`Race with ID ${id} not found for deletion.`);
|
||||
throw new Error(`Race with ID ${id} not found`);
|
||||
}
|
||||
|
||||
this.races.delete(id);
|
||||
this.logger.debug(`[InMemoryRaceRepository] Deleting race with ID: ${id}.`);
|
||||
if (this.races.delete(id)) {
|
||||
this.logger.info(`Race ${id} deleted successfully.`);
|
||||
} catch (error) {
|
||||
this.logger.error(`Error deleting race ${id}:`, error);
|
||||
throw error;
|
||||
} else {
|
||||
this.logger.warn(`Race with ID ${id} not found for deletion.`);
|
||||
}
|
||||
return Promise.resolve();
|
||||
}
|
||||
|
||||
async exists(id: string): Promise<boolean> {
|
||||
this.logger.debug(`Checking existence of race with id: ${id}`);
|
||||
try {
|
||||
const exists = this.races.has(id);
|
||||
this.logger.debug(`Race ${id} exists: ${exists}.`);
|
||||
return exists;
|
||||
} catch (error) {
|
||||
this.logger.error(`Error checking existence of race with id ${id}:`, error);
|
||||
throw error;
|
||||
}
|
||||
this.logger.debug(`[InMemoryRaceRepository] Checking existence of race with ID: ${id}.`);
|
||||
return Promise.resolve(this.races.has(id));
|
||||
}
|
||||
|
||||
/**
|
||||
* Utility method to generate a new UUID
|
||||
*/
|
||||
static generateId(): string {
|
||||
return uuidv4();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user