ce1ef1fea6
Ref: IT-628 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
77 lines
2.7 KiB
C#
77 lines
2.7 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 TemplateRepository : EfRepository<TemplateEntity>, ITemplateRepository
|
|
{
|
|
public TemplateRepository(NotificationDbContext dbContext) : base(dbContext)
|
|
{
|
|
}
|
|
|
|
public async Task<IReadOnlyList<Template>> GetByServiceAsync(string serviceName, CancellationToken ct = default)
|
|
{
|
|
List<TemplateEntity> entities = await DbSet
|
|
.Where(x => x.ServiceName == serviceName)
|
|
.ToListAsync(ct);
|
|
|
|
return entities.Select(MapToDomain).ToList();
|
|
}
|
|
|
|
public async Task<Template?> GetAsync(string serviceName, string key, string languageCode, CancellationToken ct = default)
|
|
{
|
|
TemplateEntity? 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(Template template, CancellationToken ct = default) =>
|
|
base.AddAsync(MapToEntity(template), ct);
|
|
|
|
public Task UpdateAsync(Template template, CancellationToken ct = default)
|
|
{
|
|
TemplateEntity entity = MapToEntity(template);
|
|
entity.Updated = DateTimeOffset.UtcNow;
|
|
return base.UpdateAsync(entity, ct);
|
|
}
|
|
|
|
public async Task DeleteAsync(Template template, CancellationToken ct = default)
|
|
{
|
|
TemplateEntity? entity = await DbSet.FindAsync([template.Id], ct);
|
|
if (entity is not null)
|
|
await base.DeleteAsync(entity, ct);
|
|
}
|
|
|
|
private static Template MapToDomain(TemplateEntity 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 TemplateVariable { Name = v.Name, Required = v.Required }).ToList(),
|
|
Created = e.Created,
|
|
Updated = e.Updated
|
|
};
|
|
|
|
private static TemplateEntity MapToEntity(Template 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 TemplateVariableData { Name = v.Name, Required = v.Required }).ToList(),
|
|
Created = t.Created,
|
|
Updated = t.Updated
|
|
};
|
|
}
|