132 lines
4.3 KiB
TypeScript
132 lines
4.3 KiB
TypeScript
/**
|
|
* Infrastructure Adapter: InMemoryTeamRepository
|
|
*
|
|
* In-memory implementation of ITeamRepository.
|
|
* Stores data in a Map structure.
|
|
*/
|
|
|
|
import type { Team } from '@core/racing/domain/entities/Team';
|
|
import type { ITeamRepository } from '@core/racing/domain/repositories/ITeamRepository';
|
|
import type { Logger } from '@core/shared/application';
|
|
|
|
export class InMemoryTeamRepository implements ITeamRepository {
|
|
private teams: Map<string, Team>;
|
|
private readonly logger: Logger;
|
|
|
|
constructor(logger: Logger, seedData?: Team[]) {
|
|
this.logger = logger;
|
|
this.logger.info('InMemoryTeamRepository initialized.');
|
|
this.teams = new Map();
|
|
|
|
if (seedData) {
|
|
seedData.forEach((team) => {
|
|
this.teams.set(team.id, team);
|
|
this.logger.debug(`Seeded team: ${team.id}.`);
|
|
});
|
|
}
|
|
}
|
|
|
|
async findById(id: string): Promise<Team | null> {
|
|
this.logger.debug(`Finding team by id: ${id}`);
|
|
try {
|
|
const team = this.teams.get(id) ?? null;
|
|
if (team) {
|
|
this.logger.info(`Found team: ${id}.`);
|
|
} else {
|
|
this.logger.warn(`Team with id ${id} not found.`);
|
|
}
|
|
return team;
|
|
} catch (error) {
|
|
this.logger.error(`Error finding team by id ${id}:`, error instanceof Error ? error : new Error(String(error)));
|
|
throw error;
|
|
}
|
|
}
|
|
|
|
async findAll(): Promise<Team[]> {
|
|
this.logger.debug('Finding all teams.');
|
|
try {
|
|
const teams = Array.from(this.teams.values());
|
|
this.logger.info(`Found ${teams.length} teams.`);
|
|
return teams;
|
|
} catch (error) {
|
|
this.logger.error('Error finding all teams:', error instanceof Error ? error : new Error(String(error)));
|
|
throw error;
|
|
}
|
|
}
|
|
|
|
async findByLeagueId(leagueId: string): Promise<Team[]> {
|
|
this.logger.debug(`Finding teams by league id: ${leagueId}`);
|
|
try {
|
|
const teams = Array.from(this.teams.values()).filter((team) =>
|
|
team.leagues.includes(leagueId),
|
|
);
|
|
this.logger.info(`Found ${teams.length} teams for league id: ${leagueId}.`);
|
|
return teams;
|
|
} catch (error) {
|
|
this.logger.error(`Error finding teams by league id ${leagueId}:`, error instanceof Error ? error : new Error(String(error)));
|
|
throw error;
|
|
}
|
|
}
|
|
|
|
async create(team: Team): Promise<Team> {
|
|
this.logger.debug(`Creating team: ${team.id}`);
|
|
try {
|
|
if (await this.exists(team.id)) {
|
|
this.logger.warn(`Team with ID ${team.id} already exists.`);
|
|
throw new Error(`Team with ID ${team.id} already exists`);
|
|
}
|
|
|
|
this.teams.set(team.id, team);
|
|
this.logger.info(`Team ${team.id} created successfully.`);
|
|
return team;
|
|
} catch (error) {
|
|
this.logger.error(`Error creating team ${team.id}:`, error instanceof Error ? error : new Error(String(error)));
|
|
throw error;
|
|
}
|
|
}
|
|
|
|
async update(team: Team): Promise<Team> {
|
|
this.logger.debug(`Updating team: ${team.id}`);
|
|
try {
|
|
if (!(await this.exists(team.id))) {
|
|
this.logger.warn(`Team with ID ${team.id} not found for update.`);
|
|
throw new Error(`Team with ID ${team.id} not found`);
|
|
}
|
|
|
|
this.teams.set(team.id, team);
|
|
this.logger.info(`Team ${team.id} updated successfully.`);
|
|
return team;
|
|
} catch (error) {
|
|
this.logger.error(`Error updating team ${team.id}:`, error instanceof Error ? error : new Error(String(error)));
|
|
throw error;
|
|
}
|
|
}
|
|
|
|
async delete(id: string): Promise<void> {
|
|
this.logger.debug(`Deleting team: ${id}`);
|
|
try {
|
|
if (!(await this.exists(id))) {
|
|
this.logger.warn(`Team with ID ${id} not found for deletion.`);
|
|
throw new Error(`Team with ID ${id} not found`);
|
|
}
|
|
|
|
this.teams.delete(id);
|
|
this.logger.info(`Team ${id} deleted successfully.`);
|
|
} catch (error) {
|
|
this.logger.error(`Error deleting team ${id}:`, error instanceof Error ? error : new Error(String(error)));
|
|
throw error;
|
|
}
|
|
}
|
|
|
|
async exists(id: string): Promise<boolean> {
|
|
this.logger.debug(`Checking existence of team with id: ${id}`);
|
|
try {
|
|
const exists = this.teams.has(id);
|
|
this.logger.debug(`Team ${id} exists: ${exists}.`);
|
|
return exists;
|
|
} catch (error) {
|
|
this.logger.error(`Error checking existence of team with id ${id}:`, error instanceof Error ? error : new Error(String(error)));
|
|
throw error;
|
|
}
|
|
}
|
|
} |