50828d23ec
- 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.
46 lines
1.7 KiB
C#
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);
|
|
}
|
|
}
|
|
} |