Files
hrynco-notification-service/HrynCo.NotificationService.DAL.EF/Repositories/EmailChannelUsageRepository.cs
T
Anatolii Grynchuk 6dcc911fc2 refactor: rename domain types and introduce TransactionBehavior pattern
- Rename Template -> EmailTemplate, Provider -> EmailChannel,
  ProviderSettings -> EmailChannelSettings, ProviderType -> EmailChannelType,
  ProviderUsage -> EmailChannelUsage throughout all layers
- Add Undefined = 0 to EmailChannelType enum for safe default handling
- Remove SaveChangesAsync from EfRepository methods — repositories now only stage changes
- Add SaveChangesAsync to IUnitOfWork and EfUnitOfWork
- Add TransactionBehavior MediatR pipeline: wraps every handler in a transaction,
  saves and commits on success, rolls back on exception
- Add MediatR package reference to Services project

Ref: IT-628

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
2026-05-02 00:16:47 +03:00

44 lines
1.6 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 : EfRepository<EmailChannelUsageEntity>, IEmailChannelUsageRepository
{
public EmailChannelUsageRepository(NotificationDbContext dbContext) : base(dbContext)
{
}
public async Task<int> GetDailyCountAsync(Guid providerId, DateOnly date, CancellationToken ct = default)
{
EmailChannelUsageEntity? entity = await DbSet
.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 DbSet
.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 DbSet
.FirstOrDefaultAsync(x => x.ProviderId == providerId && x.Date == date, ct);
if (entity is null)
await AddAsync(new EmailChannelUsageEntity { ProviderId = providerId, Date = date, SentCount = 1 }, ct);
else
{
entity.SentCount++;
Update(entity);
}
}
}