refactor: replace internal UnitOfWork with NotificationUnitOfWork and NotificationBaseRepository
- 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.
This commit is contained in:
@@ -1,13 +1,13 @@
|
||||
namespace HrynCo.NotificationService.DAL.EF.Repositories;
|
||||
|
||||
using System.Text.Json;
|
||||
using HrynCo.NotificationService.DAL.Abstract.Providers;
|
||||
using HrynCo.NotificationService.DAL.Abstract.Repositories;
|
||||
using HrynCo.DAL.EF.Core;
|
||||
using HrynCo.NotificationService.DAL.EF.Core;
|
||||
using HrynCo.NotificationService.DAL.EF.Entities;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace HrynCo.NotificationService.DAL.EF.Repositories;
|
||||
|
||||
internal sealed class EmailChannelRepository : EfRepository<NotificationDbContext, EmailChannelEntity>, IEmailChannelRepository
|
||||
internal sealed class EmailChannelRepository : NotificationBaseRepository<EmailChannelEntity>, IEmailChannelRepository
|
||||
{
|
||||
public EmailChannelRepository(NotificationDbContext dbContext) : base(dbContext)
|
||||
{
|
||||
@@ -15,20 +15,14 @@ internal sealed class EmailChannelRepository : EfRepository<NotificationDbContex
|
||||
|
||||
public async Task<IReadOnlyList<EmailChannel>> GetAllAsync(CancellationToken ct = default)
|
||||
{
|
||||
var entities = await DbSet
|
||||
.AsNoTracking()
|
||||
.OrderBy(x => x.ServiceName)
|
||||
.ThenBy(x => x.Priority)
|
||||
.ToListAsync(ct);
|
||||
var entities = await EfRepository.Get().ToListAsync(ct);
|
||||
|
||||
return entities.Select(MapToDomain).ToList();
|
||||
}
|
||||
|
||||
public async Task<IReadOnlyList<EmailChannel>> GetByServiceAsync(string serviceName, CancellationToken ct = default)
|
||||
{
|
||||
var entities = await DbSet
|
||||
.AsNoTracking()
|
||||
.Where(x => x.ServiceName == serviceName)
|
||||
var entities = await EfRepository.Get(x => x.ServiceName == serviceName)
|
||||
.OrderBy(x => x.Priority)
|
||||
.ToListAsync(ct);
|
||||
|
||||
@@ -38,8 +32,7 @@ internal sealed class EmailChannelRepository : EfRepository<NotificationDbContex
|
||||
public async Task<IReadOnlyList<ChannelWithUsage>> GetAllWithUsageSummaryAsync(
|
||||
DateOnly today, CancellationToken ct = default)
|
||||
{
|
||||
var rows = await DbSet
|
||||
.AsNoTracking()
|
||||
var rows = await EfRepository.Get()
|
||||
.OrderBy(c => c.ServiceName)
|
||||
.ThenBy(c => c.Priority)
|
||||
.Select(c => new
|
||||
@@ -61,30 +54,24 @@ internal sealed class EmailChannelRepository : EfRepository<NotificationDbContex
|
||||
|
||||
public async Task<EmailChannel?> GetByIdAsync(Guid id, CancellationToken ct = default)
|
||||
{
|
||||
EmailChannelEntity? entity = await DbSet.AsNoTracking().FirstOrDefaultAsync(x => x.Id == id, ct);
|
||||
EmailChannelEntity? entity = await EfRepository.GetByIdAsync(id);
|
||||
return entity is null ? null : MapToDomain(entity);
|
||||
}
|
||||
|
||||
public Task AddAsync(EmailChannel channel, CancellationToken ct = default)
|
||||
{
|
||||
return base.AddAsync(MapToEntity(channel), ct);
|
||||
return EfRepository.AddAsync(MapToEntity(channel));
|
||||
}
|
||||
|
||||
public Task UpdateAsync(EmailChannel channel, CancellationToken ct = default)
|
||||
public async Task UpdateAsync(EmailChannel channel, CancellationToken ct = default)
|
||||
{
|
||||
EmailChannelEntity entity = MapToEntity(channel);
|
||||
entity.Updated = DateTimeOffset.UtcNow;
|
||||
Update(entity);
|
||||
return Task.CompletedTask;
|
||||
await EfRepository.UpdateAsync(entity);
|
||||
}
|
||||
|
||||
public async Task DeleteAsync(EmailChannel channel, CancellationToken ct = default)
|
||||
{
|
||||
EmailChannelEntity? entity = await DbSet.FindAsync([channel.Id], ct);
|
||||
if (entity is not null)
|
||||
{
|
||||
Delete(entity);
|
||||
}
|
||||
await EfRepository.DeleteAsync(channel.Id);
|
||||
}
|
||||
|
||||
private static EmailChannel MapToDomain(EmailChannelEntity e)
|
||||
@@ -128,8 +115,8 @@ internal sealed class EmailChannelRepository : EfRepository<NotificationDbContex
|
||||
return type switch
|
||||
{
|
||||
EmailChannelType.Smtp => JsonSerializer.Deserialize<SmtpChannelSettings>(json)
|
||||
?? throw new InvalidOperationException(
|
||||
"Failed to deserialize SMTP EmailChannel settings."),
|
||||
?? throw new InvalidOperationException(
|
||||
"Failed to deserialize SMTP EmailChannel settings."),
|
||||
_ => throw new InvalidOperationException($"Unknown or undefined email channel type: {type}")
|
||||
};
|
||||
}
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
using HrynCo.NotificationService.DAL.Abstract.Repositories;
|
||||
using HrynCo.DAL.EF.Core;
|
||||
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<NotificationDbContext, EmailChannelUsageEntity>, IEmailChannelUsageRepository
|
||||
internal sealed class EmailChannelUsageRepository : NotificationBaseRepository<EmailChannelUsageEntity>, IEmailChannelUsageRepository
|
||||
{
|
||||
public EmailChannelUsageRepository(NotificationDbContext dbContext) : base(dbContext)
|
||||
{
|
||||
@@ -13,7 +13,8 @@ internal sealed class EmailChannelUsageRepository : EfRepository<NotificationDbC
|
||||
|
||||
public async Task<int> GetDailyCountAsync(Guid providerId, DateOnly date, CancellationToken ct = default)
|
||||
{
|
||||
EmailChannelUsageEntity? entity = await DbSet
|
||||
EmailChannelUsageEntity? entity = await EfRepository.Get()
|
||||
.AsNoTracking()
|
||||
.FirstOrDefaultAsync(x => x.ProviderId == providerId && x.Date == date, ct);
|
||||
|
||||
return entity?.SentCount ?? 0;
|
||||
@@ -21,7 +22,7 @@ internal sealed class EmailChannelUsageRepository : EfRepository<NotificationDbC
|
||||
|
||||
public async Task<int> GetMonthlyCountAsync(Guid providerId, int year, int month, CancellationToken ct = default)
|
||||
{
|
||||
return await DbSet
|
||||
return await EfRepository.Get()
|
||||
.Where(x => x.ProviderId == providerId
|
||||
&& x.Date.Year == year
|
||||
&& x.Date.Month == month)
|
||||
@@ -30,15 +31,16 @@ internal sealed class EmailChannelUsageRepository : EfRepository<NotificationDbC
|
||||
|
||||
public async Task IncrementUsageAsync(Guid providerId, DateOnly date, CancellationToken ct = default)
|
||||
{
|
||||
EmailChannelUsageEntity? entity = await DbSet
|
||||
EmailChannelUsageEntity? entity = await EfRepository.Get()
|
||||
.AsNoTracking()
|
||||
.FirstOrDefaultAsync(x => x.ProviderId == providerId && x.Date == date, ct);
|
||||
|
||||
if (entity is null)
|
||||
await AddAsync(new EmailChannelUsageEntity { ProviderId = providerId, Date = date, SentCount = 1 }, ct);
|
||||
await EfRepository.AddAsync(new EmailChannelUsageEntity { ProviderId = providerId, Date = date, SentCount = 1 });
|
||||
else
|
||||
{
|
||||
entity.SentCount++;
|
||||
Update(entity);
|
||||
await EfRepository.UpdateAsync(entity);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,12 +1,13 @@
|
||||
using HrynCo.NotificationService.DAL.Abstract.Repositories;
|
||||
using HrynCo.NotificationService.DAL.Abstract.Templates;
|
||||
using HrynCo.DAL.EF.Core;
|
||||
using HrynCo.NotificationService.DAL.EF.Core;
|
||||
using HrynCo.NotificationService.DAL.EF.Entities;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace HrynCo.NotificationService.DAL.EF.Repositories;
|
||||
|
||||
internal sealed class EmailTemplateRepository : EfRepository<NotificationDbContext, EmailTemplateEntity>, IEmailTemplateRepository
|
||||
internal sealed class EmailTemplateRepository
|
||||
: NotificationBaseRepository<EmailTemplateEntity>, IEmailTemplateRepository
|
||||
{
|
||||
public EmailTemplateRepository(NotificationDbContext dbContext) : base(dbContext)
|
||||
{
|
||||
@@ -14,7 +15,7 @@ internal sealed class EmailTemplateRepository : EfRepository<NotificationDbConte
|
||||
|
||||
public async Task<IReadOnlyList<EmailTemplate>> GetAllAsync(CancellationToken ct = default)
|
||||
{
|
||||
List<EmailTemplateEntity> entities = await DbSet
|
||||
List<EmailTemplateEntity> entities = await EfRepository.Get()
|
||||
.AsNoTracking()
|
||||
.ToListAsync(ct);
|
||||
return entities.Select(MapToDomain).ToList();
|
||||
@@ -22,7 +23,7 @@ internal sealed class EmailTemplateRepository : EfRepository<NotificationDbConte
|
||||
|
||||
public async Task<IReadOnlyList<EmailTemplate>> GetByServiceAsync(string serviceName, CancellationToken ct = default)
|
||||
{
|
||||
List<EmailTemplateEntity> entities = await DbSet
|
||||
List<EmailTemplateEntity> entities = await EfRepository.Get()
|
||||
.AsNoTracking()
|
||||
.Where(x => x.ServiceName == serviceName)
|
||||
.ToListAsync(ct);
|
||||
@@ -32,7 +33,7 @@ internal sealed class EmailTemplateRepository : EfRepository<NotificationDbConte
|
||||
|
||||
public async Task<EmailTemplate?> GetAsync(string serviceName, string key, string languageCode, CancellationToken ct = default)
|
||||
{
|
||||
EmailTemplateEntity? entity = await DbSet
|
||||
EmailTemplateEntity? entity = await EfRepository.Get()
|
||||
.AsNoTracking()
|
||||
.FirstOrDefaultAsync(
|
||||
x => x.ServiceName == serviceName && x.Key == key && x.LanguageCode == languageCode, ct);
|
||||
@@ -40,22 +41,24 @@ internal sealed class EmailTemplateRepository : EfRepository<NotificationDbConte
|
||||
return entity is null ? null : MapToDomain(entity);
|
||||
}
|
||||
|
||||
public Task AddAsync(EmailTemplate EmailTemplate, CancellationToken ct = default) =>
|
||||
base.AddAsync(MapToEntity(EmailTemplate), ct);
|
||||
public Task AddAsync(EmailTemplate EmailTemplate, CancellationToken ct = default)
|
||||
{
|
||||
return EfRepository.AddAsync(MapToEntity(EmailTemplate));
|
||||
}
|
||||
|
||||
public Task UpdateAsync(EmailTemplate EmailTemplate, CancellationToken ct = default)
|
||||
{
|
||||
EmailTemplateEntity entity = MapToEntity(EmailTemplate);
|
||||
entity.Updated = DateTimeOffset.UtcNow;
|
||||
Update(entity);
|
||||
return Task.CompletedTask;
|
||||
return EfRepository.UpdateAsync(entity);
|
||||
}
|
||||
|
||||
public async Task DeleteAsync(EmailTemplate EmailTemplate, CancellationToken ct = default)
|
||||
{
|
||||
EmailTemplateEntity? entity = await DbSet.FindAsync([EmailTemplate.Id], ct);
|
||||
EmailTemplateEntity? entity = await EfRepository.Get()
|
||||
.FirstOrDefaultAsync(x => x.Id == EmailTemplate.Id, ct);
|
||||
if (entity is not null)
|
||||
Delete(entity);
|
||||
await EfRepository.DeleteAsync(entity);
|
||||
}
|
||||
|
||||
private static EmailTemplate MapToDomain(EmailTemplateEntity e) => new()
|
||||
|
||||
Reference in New Issue
Block a user