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>
This commit is contained in:
@@ -0,0 +1,85 @@
|
||||
using System.Text.Json;
|
||||
using HrynCo.NotificationService.DAL.Abstract.Providers;
|
||||
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 ProviderRepository : NotificationEfRepository<ProviderEntity>, IProviderRepository
|
||||
{
|
||||
public ProviderRepository(NotificationDbContext dbContext) : base(dbContext)
|
||||
{
|
||||
}
|
||||
|
||||
public async Task<IReadOnlyList<Provider>> GetByServiceAsync(string serviceName, CancellationToken ct = default)
|
||||
{
|
||||
List<ProviderEntity> entities = await DbSet
|
||||
.Where(x => x.ServiceName == serviceName)
|
||||
.OrderBy(x => x.Priority)
|
||||
.ToListAsync(ct);
|
||||
|
||||
return entities.Select(MapToDomain).ToList();
|
||||
}
|
||||
|
||||
public async Task<Provider?> GetByIdAsync(Guid id, CancellationToken ct = default)
|
||||
{
|
||||
ProviderEntity? entity = await DbSet.FindAsync([id], ct);
|
||||
return entity is null ? null : MapToDomain(entity);
|
||||
}
|
||||
|
||||
public Task AddAsync(Provider provider, CancellationToken ct = default) =>
|
||||
base.AddAsync(MapToEntity(provider), ct);
|
||||
|
||||
public Task UpdateAsync(Provider provider, CancellationToken ct = default)
|
||||
{
|
||||
ProviderEntity entity = MapToEntity(provider);
|
||||
entity.Updated = DateTimeOffset.UtcNow;
|
||||
return base.UpdateAsync(entity, ct);
|
||||
}
|
||||
|
||||
public async Task DeleteAsync(Provider provider, CancellationToken ct = default)
|
||||
{
|
||||
ProviderEntity? entity = await DbSet.FindAsync([provider.Id], ct);
|
||||
if (entity is not null)
|
||||
await base.DeleteAsync(entity, ct);
|
||||
}
|
||||
|
||||
private static Provider MapToDomain(ProviderEntity e) => new()
|
||||
{
|
||||
Id = e.Id,
|
||||
ServiceName = e.ServiceName,
|
||||
Priority = e.Priority,
|
||||
ProviderType = e.ProviderType,
|
||||
Settings = DeserializeSettings(e.ProviderType, e.SettingsJson),
|
||||
DailyLimit = e.DailyLimit,
|
||||
MonthlyLimit = e.MonthlyLimit,
|
||||
WarnThresholdPercent = e.WarnThresholdPercent,
|
||||
IsActive = e.IsActive,
|
||||
Created = e.Created,
|
||||
Updated = e.Updated
|
||||
};
|
||||
|
||||
private static ProviderEntity MapToEntity(Provider p) => new()
|
||||
{
|
||||
Id = p.Id,
|
||||
ServiceName = p.ServiceName,
|
||||
Priority = p.Priority,
|
||||
ProviderType = p.ProviderType,
|
||||
SettingsJson = JsonSerializer.Serialize(p.Settings),
|
||||
DailyLimit = p.DailyLimit,
|
||||
MonthlyLimit = p.MonthlyLimit,
|
||||
WarnThresholdPercent = p.WarnThresholdPercent,
|
||||
IsActive = p.IsActive,
|
||||
Created = p.Created,
|
||||
Updated = p.Updated
|
||||
};
|
||||
|
||||
private static ProviderSettings DeserializeSettings(ProviderType type, string json) => type switch
|
||||
{
|
||||
ProviderType.Smtp => JsonSerializer.Deserialize<SmtpProviderSettings>(json)
|
||||
?? throw new InvalidOperationException("Failed to deserialize SMTP provider settings."),
|
||||
_ => throw new InvalidOperationException($"Unknown provider type: {type}")
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user