Files
gridpilot.gg/packages/racing/infrastructure/repositories/InMemoryTransactionRepository.ts
2025-12-10 00:38:59 +01:00

53 lines
1.6 KiB
TypeScript

/**
* In-Memory Implementation: ITransactionRepository
*
* Mock repository for testing and development
*/
import type { Transaction, TransactionType } from '../../domain/entities/Transaction';
import type { ITransactionRepository } from '../../domain/repositories/ITransactionRepository';
export class InMemoryTransactionRepository implements ITransactionRepository {
private transactions: Map<string, Transaction> = new Map();
async findById(id: string): Promise<Transaction | null> {
return this.transactions.get(id) ?? null;
}
async findByWalletId(walletId: string): Promise<Transaction[]> {
return Array.from(this.transactions.values()).filter(t => t.walletId === walletId);
}
async findByType(type: TransactionType): Promise<Transaction[]> {
return Array.from(this.transactions.values()).filter(t => t.type === type);
}
async create(transaction: Transaction): Promise<Transaction> {
if (this.transactions.has(transaction.id)) {
throw new Error('Transaction with this ID already exists');
}
this.transactions.set(transaction.id, transaction);
return transaction;
}
async update(transaction: Transaction): Promise<Transaction> {
if (!this.transactions.has(transaction.id)) {
throw new Error('Transaction not found');
}
this.transactions.set(transaction.id, transaction);
return transaction;
}
async delete(id: string): Promise<void> {
this.transactions.delete(id);
}
async exists(id: string): Promise<boolean> {
return this.transactions.has(id);
}
// Test helper
clear(): void {
this.transactions.clear();
}
}