ce1ef1fea6
Ref: IT-628 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
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 ProviderUsageRepository : EfRepository<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);
|
|
}
|
|
}
|