Files
pos-system/services/mining-service-net/src/MiningService.Infrastructure/Repositories/ReferralRepository.cs

58 lines
1.9 KiB
C#

using Microsoft.EntityFrameworkCore;
using MiningService.Domain.AggregatesModel.ReferralAggregate;
using MiningService.Domain.SeedWork;
namespace MiningService.Infrastructure.Repositories;
/// <summary>
/// EN: Repository implementation for Referral aggregate.
/// VI: Triển khai repository cho Referral aggregate.
/// </summary>
public class ReferralRepository : IReferralRepository
{
private readonly MiningServiceContext _context;
public IUnitOfWork UnitOfWork => _context;
public ReferralRepository(MiningServiceContext context)
{
_context = context ?? throw new ArgumentNullException(nameof(context));
}
public async Task<Referral?> GetByIdAsync(Guid id, CancellationToken cancellationToken = default)
{
return await _context.Referrals
.FirstOrDefaultAsync(r => r.Id == id, cancellationToken);
}
public async Task<List<Referral>> GetByReferrerIdAsync(Guid referrerId, CancellationToken cancellationToken = default)
{
return await _context.Referrals
.Where(r => r.ReferrerId == referrerId)
.OrderByDescending(r => r.CreatedAt)
.ToListAsync(cancellationToken);
}
public async Task<int> GetActiveCountByReferrerIdAsync(Guid referrerId, CancellationToken cancellationToken = default)
{
return await _context.Referrals
.CountAsync(r => r.ReferrerId == referrerId && r.IsActive, cancellationToken);
}
public async Task<Referral?> GetByReferredIdAsync(Guid referredId, CancellationToken cancellationToken = default)
{
return await _context.Referrals
.FirstOrDefaultAsync(r => r.ReferredId == referredId, cancellationToken);
}
public Referral Add(Referral referral)
{
return _context.Referrals.Add(referral).Entity;
}
public void Update(Referral referral)
{
_context.Entry(referral).State = EntityState.Modified;
}
}