5cf5f888eb
- ServiceCollectionExtensions in DAL.EF: AddNotificationDataAccess (DbContext, UoW, repositories) - ServiceCollectionExtensions in Services: AddNotificationServices (MediatR + TransactionBehavior) - Api/Program.cs: Serilog, OpenAPI, controllers, DI wiring - Worker/Program.cs: Serilog, DI wiring - appsettings.json: Serilog config with Console + Seq sinks, connection string - appsettings.Development.json: Debug log level overrides Ref: IT-628 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
77 lines
2.8 KiB
C#
77 lines
2.8 KiB
C#
using HrynCo.NotificationService.DAL.Abstract.Repositories;
|
|
using HrynCo.NotificationService.DAL.Abstract.Templates;
|
|
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<EmailTemplateEntity>, IEmailTemplateRepository
|
|
{
|
|
public EmailTemplateRepository(NotificationDbContext dbContext) : base(dbContext)
|
|
{
|
|
}
|
|
|
|
public async Task<IReadOnlyList<EmailTemplate>> GetByServiceAsync(string serviceName, CancellationToken ct = default)
|
|
{
|
|
List<EmailTemplateEntity> entities = await DbSet
|
|
.Where(x => x.ServiceName == serviceName)
|
|
.ToListAsync(ct);
|
|
|
|
return entities.Select(MapToDomain).ToList();
|
|
}
|
|
|
|
public async Task<EmailTemplate?> GetAsync(string serviceName, string key, string languageCode, CancellationToken ct = default)
|
|
{
|
|
EmailTemplateEntity? entity = await DbSet.FirstOrDefaultAsync(
|
|
x => x.ServiceName == serviceName && x.Key == key && x.LanguageCode == languageCode, ct);
|
|
|
|
return entity is null ? null : MapToDomain(entity);
|
|
}
|
|
|
|
public Task AddAsync(EmailTemplate EmailTemplate, CancellationToken ct = default) =>
|
|
base.AddAsync(MapToEntity(EmailTemplate), ct);
|
|
|
|
public Task UpdateAsync(EmailTemplate EmailTemplate, CancellationToken ct = default)
|
|
{
|
|
EmailTemplateEntity entity = MapToEntity(EmailTemplate);
|
|
entity.Updated = DateTimeOffset.UtcNow;
|
|
Update(entity);
|
|
return Task.CompletedTask;
|
|
}
|
|
|
|
public async Task DeleteAsync(EmailTemplate EmailTemplate, CancellationToken ct = default)
|
|
{
|
|
EmailTemplateEntity? entity = await DbSet.FindAsync([EmailTemplate.Id], ct);
|
|
if (entity is not null)
|
|
Delete(entity);
|
|
}
|
|
|
|
private static EmailTemplate MapToDomain(EmailTemplateEntity e) => new()
|
|
{
|
|
Id = e.Id,
|
|
ServiceName = e.ServiceName,
|
|
Key = e.Key,
|
|
LanguageCode = e.LanguageCode,
|
|
Subject = e.Subject,
|
|
HtmlBody = e.HtmlBody,
|
|
TextBody = e.TextBody,
|
|
Variables = e.Variables.Select(v => new EmailTemplateVariable { Name = v.Name, Required = v.Required }).ToList(),
|
|
Created = e.Created,
|
|
Updated = e.Updated
|
|
};
|
|
|
|
private static EmailTemplateEntity MapToEntity(EmailTemplate t) => new()
|
|
{
|
|
Id = t.Id,
|
|
ServiceName = t.ServiceName,
|
|
Key = t.Key,
|
|
LanguageCode = t.LanguageCode,
|
|
Subject = t.Subject,
|
|
HtmlBody = t.HtmlBody,
|
|
TextBody = t.TextBody,
|
|
Variables = t.Variables.Select(v => new EmailTemplateVariableData { Name = v.Name, Required = v.Required }).ToList(),
|
|
Created = t.Created,
|
|
Updated = t.Updated
|
|
};
|
|
} |