Files
hrynco-notification-service/HrynCo.NotificationService.DAL.EF/Repositories/ProviderUsageRepository.cs
T
Anatolii Grynchuk 4f573da374 feat: add repository layer with IUnitOfWork and fixed EF base
- ITransaction, IUnitOfWork in DAL.Abstract
- EfTransactionAdapter, EfUnitOfWork<TDbContext>, NotificationUnitOfWork in DAL.EF
- NotificationEfRepository<TEntity>: async-only base, fixed Exists (AnyAsync),
  fixed batch Add (AddRangeAsync), single SaveChangesAsync per operation
- TemplateRepository, ProviderRepository, ProviderUsageRepository
- ProviderUsageRepository.IncrementAsync uses atomic PostgreSQL upsert
- ProviderRepository deserializes settings polymorphically via ProviderType discriminator

Ref: IT-628

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
2026-05-01 23:18:41 +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 ProviderUsageRepository : NotificationEfRepository<ProviderUsageEntity>, IProviderUsageRepository
{
public ProviderUsageRepository(NotificationDbContext dbContext) : base(dbContext)
{
}
public async Task<int> GetDailyCountAsync(Guid providerId, DateOnly date, CancellationToken ct = default)
{
ProviderUsageEntity? 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 IncrementAsync(Guid providerId, DateOnly date, CancellationToken ct = default)
{
DateTimeOffset now = DateTimeOffset.UtcNow;
// Atomic upsert: insert with count=1 or increment existing count.
await DbContext.Database.ExecuteSqlAsync(
$"""
INSERT INTO provider_usage (id, provider_id, date, sent_count, created)
VALUES ({Guid.NewGuid()}, {providerId}, {date}, 1, {now})
ON CONFLICT (provider_id, date) DO UPDATE SET
sent_count = provider_usage.sent_count + 1,
updated = {now}
""", ct);
}
}