namespace WalletService.Infrastructure.Repositories;
using Microsoft.EntityFrameworkCore;
using WalletService.Domain.AggregatesModel.WalletAggregate;
using WalletService.Domain.SeedWork;
///
/// EN: Repository implementation for Wallet aggregate
/// VI: Repository implementation cho aggregate Wallet
///
public class WalletRepository : IWalletRepository
{
private readonly WalletServiceContext _context;
public IUnitOfWork UnitOfWork => _context;
public WalletRepository(WalletServiceContext context)
{
_context = context ?? throw new ArgumentNullException(nameof(context));
}
public async Task GetByIdAsync(Guid walletId)
{
return await _context.Wallets
.Include(w => w.Balances)
.FirstOrDefaultAsync(w => w.Id == walletId);
}
public async Task GetByUserIdAsync(Guid userId)
{
return await _context.Wallets
.Include(w => w.Balances)
.FirstOrDefaultAsync(w => w.UserId == userId);
}
public async Task GetByIdWithTransactionsAsync(Guid walletId)
{
return await _context.Wallets
.Include(w => w.Transactions)
.FirstOrDefaultAsync(w => w.Id == walletId);
}
public async Task> GetTransactionsAsync(
Guid walletId,
int page = 1,
int pageSize = 20)
{
return await _context.WalletTransactions
.Where(t => t.WalletId == walletId)
.OrderByDescending(t => t.CreatedAt)
.Skip((page - 1) * pageSize)
.Take(pageSize)
.ToListAsync();
}
public async Task CountTransactionsAsync(Guid walletId)
{
return await _context.WalletTransactions
.CountAsync(t => t.WalletId == walletId);
}
public async Task ExistsByUserIdAsync(Guid userId)
{
return await _context.Wallets
.AnyAsync(w => w.UserId == userId);
}
public Wallet Add(Wallet wallet)
{
return _context.Wallets.Add(wallet).Entity;
}
public void Update(Wallet wallet)
{
_context.Entry(wallet).State = EntityState.Modified;
}
}