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,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