89 lines
3.7 KiB
TypeScript
89 lines
3.7 KiB
TypeScript
import { ISeasonRepository } from '@gridpilot/racing/domain/repositories/ISeasonRepository';
|
|
import { Season } from '@gridpilot/racing/domain/entities/Season';
|
|
import { Logger } from '@gridpilot/shared/logging/Logger';
|
|
|
|
export class InMemorySeasonRepository implements ISeasonRepository {
|
|
private seasons: Map<string, Season> = new Map(); // Key: seasonId
|
|
|
|
constructor(private readonly logger: Logger, initialSeasons: Season[] = []) {
|
|
this.logger.info('InMemorySeasonRepository initialized.');
|
|
for (const season of initialSeasons) {
|
|
this.seasons.set(season.id, season);
|
|
this.logger.debug(`Seeded season: ${season.id} (${season.name}).`);
|
|
}
|
|
}
|
|
|
|
async findById(id: string): Promise<Season | null> {
|
|
this.logger.debug(`[InMemorySeasonRepository] Finding season by ID: ${id}`);
|
|
const season = this.seasons.get(id) ?? null;
|
|
if (season) {
|
|
this.logger.info(`Found season by ID: ${id}.`);
|
|
} else {
|
|
this.logger.warn(`Season with ID ${id} not found.`);
|
|
}
|
|
return Promise.resolve(season);
|
|
}
|
|
|
|
async findByLeagueId(leagueId: string): Promise<Season[]> {
|
|
this.logger.debug(`[InMemorySeasonRepository] Finding seasons by league ID: ${leagueId}`);
|
|
const seasons = Array.from(this.seasons.values()).filter(season => season.leagueId === leagueId);
|
|
this.logger.info(`Found ${seasons.length} seasons for league ID: ${leagueId}.`);
|
|
return Promise.resolve(seasons);
|
|
}
|
|
|
|
async create(season: Season): Promise<Season> {
|
|
this.logger.debug(`[InMemorySeasonRepository] Creating season: ${season.id} (${season.name})`);
|
|
if (this.seasons.has(season.id)) {
|
|
this.logger.warn(`Season with ID ${season.id} already exists.`);
|
|
throw new Error('Season already exists');
|
|
}
|
|
this.seasons.set(season.id, season);
|
|
this.logger.info(`Season ${season.id} created successfully.`);
|
|
return Promise.resolve(season);
|
|
}
|
|
|
|
async add(season: Season): Promise<void> {
|
|
this.logger.debug(`[InMemorySeasonRepository] Adding season: ${season.id} (${season.name})`);
|
|
if (this.seasons.has(season.id)) {
|
|
this.logger.warn(`Season with ID ${season.id} already exists (add method).`);
|
|
throw new Error('Season already exists');
|
|
}
|
|
this.seasons.set(season.id, season);
|
|
this.logger.info(`Season ${season.id} added successfully.`);
|
|
return Promise.resolve();
|
|
}
|
|
|
|
async update(season: Season): Promise<void> {
|
|
this.logger.debug(`[InMemorySeasonRepository] Updating season: ${season.id} (${season.name})`);
|
|
if (!this.seasons.has(season.id)) {
|
|
this.logger.warn(`Season with ID ${season.id} not found for update.`);
|
|
throw new Error('Season not found');
|
|
}
|
|
this.seasons.set(season.id, season);
|
|
this.logger.info(`Season ${season.id} updated successfully.`);
|
|
return Promise.resolve();
|
|
}
|
|
|
|
async delete(id: string): Promise<void> {
|
|
this.logger.debug(`[InMemorySeasonRepository] Deleting season with ID: ${id}`);
|
|
if (this.seasons.delete(id)) {
|
|
this.logger.info(`Season ${id} deleted successfully.`);
|
|
} else {
|
|
this.logger.warn(`Season with ID ${id} not found for deletion.`);
|
|
}
|
|
return Promise.resolve();
|
|
}
|
|
|
|
async listByLeague(leagueId: string): Promise<Season[]> {
|
|
this.logger.debug(`[InMemorySeasonRepository] Listing seasons by league ID: ${leagueId}`);
|
|
const seasons = Array.from(this.seasons.values()).filter(season => season.leagueId === leagueId);
|
|
return Promise.resolve(seasons);
|
|
}
|
|
|
|
async listActiveByLeague(leagueId: string): Promise<Season[]> {
|
|
this.logger.debug(`[InMemorySeasonRepository] Listing active seasons by league ID: ${leagueId}`);
|
|
const activeSeasons = Array.from(this.seasons.values()).filter(season => season.leagueId === leagueId && season.status === 'active');
|
|
return Promise.resolve(activeSeasons);
|
|
}
|
|
}
|