82 lines
2.0 KiB
TypeScript
82 lines
2.0 KiB
TypeScript
/**
|
|
* Infrastructure Adapter: InMemoryLeagueRepository
|
|
*
|
|
* In-memory implementation of ILeagueRepository.
|
|
* Stores data in Map structure with UUID generation.
|
|
*/
|
|
|
|
import { v4 as uuidv4 } from 'uuid';
|
|
import { League } from '../../domain/entities/League';
|
|
import { ILeagueRepository } from '../../application/ports/ILeagueRepository';
|
|
|
|
export class InMemoryLeagueRepository implements ILeagueRepository {
|
|
private leagues: Map<string, League>;
|
|
|
|
constructor(seedData?: League[]) {
|
|
this.leagues = new Map();
|
|
|
|
if (seedData) {
|
|
seedData.forEach(league => {
|
|
this.leagues.set(league.id, league);
|
|
});
|
|
}
|
|
}
|
|
|
|
async findById(id: string): Promise<League | null> {
|
|
return this.leagues.get(id) ?? null;
|
|
}
|
|
|
|
async findAll(): Promise<League[]> {
|
|
return Array.from(this.leagues.values());
|
|
}
|
|
|
|
async findByOwnerId(ownerId: string): Promise<League[]> {
|
|
return Array.from(this.leagues.values()).filter(
|
|
league => league.ownerId === ownerId
|
|
);
|
|
}
|
|
|
|
async create(league: League): Promise<League> {
|
|
if (await this.exists(league.id)) {
|
|
throw new Error(`League with ID ${league.id} already exists`);
|
|
}
|
|
|
|
this.leagues.set(league.id, league);
|
|
return league;
|
|
}
|
|
|
|
async update(league: League): Promise<League> {
|
|
if (!await this.exists(league.id)) {
|
|
throw new Error(`League with ID ${league.id} not found`);
|
|
}
|
|
|
|
this.leagues.set(league.id, league);
|
|
return league;
|
|
}
|
|
|
|
async delete(id: string): Promise<void> {
|
|
if (!await this.exists(id)) {
|
|
throw new Error(`League with ID ${id} not found`);
|
|
}
|
|
|
|
this.leagues.delete(id);
|
|
}
|
|
|
|
async exists(id: string): Promise<boolean> {
|
|
return this.leagues.has(id);
|
|
}
|
|
|
|
async searchByName(query: string): Promise<League[]> {
|
|
const normalizedQuery = query.toLowerCase();
|
|
return Array.from(this.leagues.values()).filter(league =>
|
|
league.name.toLowerCase().includes(normalizedQuery)
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Utility method to generate a new UUID
|
|
*/
|
|
static generateId(): string {
|
|
return uuidv4();
|
|
}
|
|
} |