Files
hrynco-notification-service/HrynCo.NotificationService.DAL.EF/Repositories/EmailChannelUsageRepository.cs
T
Anatolii Grynchuk 50828d23ec refactor: replace internal UnitOfWork with NotificationUnitOfWork and NotificationBaseRepository
- Consolidate unit of work implementation with NotificationUnitOfWork.
- Refactor repositories to use NotificationBaseRepository for consistency.
- Simplify request handlers by removing IUnitOfWork dependency.
- Update related tests and service registration.
2026-05-13 02:08:43 +03:00

46 lines
1.7 KiB
C#

using HrynCo.NotificationService.DAL.Abstract.Repositories;
using HrynCo.NotificationService.DAL.EF.Core;
using HrynCo.NotificationService.DAL.EF.Entities;
using Microsoft.EntityFrameworkCore;
namespace HrynCo.NotificationService.DAL.EF.Repositories;
internal sealed class EmailChannelUsageRepository : NotificationBaseRepository<EmailChannelUsageEntity>, IEmailChannelUsageRepository
{
public EmailChannelUsageRepository(NotificationDbContext dbContext) : base(dbContext)
{
}
public async Task<int> GetDailyCountAsync(Guid providerId, DateOnly date, CancellationToken ct = default)
{
EmailChannelUsageEntity? entity = await EfRepository.Get()
.AsNoTracking()
.FirstOrDefaultAsync(x => x.ProviderId == providerId && x.Date == date, ct);
return entity?.SentCount ?? 0;
}
public async Task<int> GetMonthlyCountAsync(Guid providerId, int year, int month, CancellationToken ct = default)
{
return await EfRepository.Get()
.Where(x => x.ProviderId == providerId
&& x.Date.Year == year
&& x.Date.Month == month)
.SumAsync(x => x.SentCount, ct);
}
public async Task IncrementUsageAsync(Guid providerId, DateOnly date, CancellationToken ct = default)
{
EmailChannelUsageEntity? entity = await EfRepository.Get()
.AsNoTracking()
.FirstOrDefaultAsync(x => x.ProviderId == providerId && x.Date == date, ct);
if (entity is null)
await EfRepository.AddAsync(new EmailChannelUsageEntity { ProviderId = providerId, Date = date, SentCount = 1 });
else
{
entity.SentCount++;
await EfRepository.UpdateAsync(entity);
}
}
}