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}")
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user