feat: E-Mail-Vorlagenverarbeitung hinzufügen
- Methode `FillTemplate` für Platzhalterersetzung hinzugefügt. - `TemplatePlaceholderAttribute` eingeführt. - `EmailOutService` mit Vorlagenmethoden aktualisiert. - Unit-Tests für Vorlagenverarbeitung hinzugefügt.
This commit is contained in:
@@ -1,10 +1,13 @@
|
||||
using AutoMapper;
|
||||
using DigitalData.Core.Application;
|
||||
using DigitalData.Core.DTO;
|
||||
using DigitalData.EmailProfilerDispatcher.Application.Contracts;
|
||||
using DigitalData.EmailProfilerDispatcher.Application.DTOs.EmailOut;
|
||||
using DigitalData.EmailProfilerDispatcher.Domain.Attributes;
|
||||
using DigitalData.EmailProfilerDispatcher.Domain.Entities;
|
||||
using DigitalData.EmailProfilerDispatcher.Infrastructure.Contracts;
|
||||
using Microsoft.Extensions.Localization;
|
||||
using System.Reflection;
|
||||
|
||||
namespace DigitalData.EmailProfilerDispatcher.Application.Services
|
||||
{
|
||||
@@ -13,5 +16,36 @@ namespace DigitalData.EmailProfilerDispatcher.Application.Services
|
||||
public EmailOutService(IEmailOutRepository repository, IStringLocalizer<TResource> localizer, IMapper mapper) : base(repository, localizer, mapper)
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
public string ApplyTemplate(string template, params object[] models) => FillTemplate(template: template, models: models);
|
||||
|
||||
public async Task<DataResult<int>> CreateWithTemplateAsync(EmailOutCreateDto createDto, params object[] models)
|
||||
{
|
||||
createDto.EmailSubj = FillTemplate(createDto.EmailSubj, models);
|
||||
createDto.EmailBody = FillTemplate(createDto.EmailBody, models);
|
||||
|
||||
return await base.CreateAsync(createDto);
|
||||
}
|
||||
|
||||
public static string FillTemplate(string template, params object[] models)
|
||||
{
|
||||
foreach(var model in models)
|
||||
{
|
||||
var properties = model.GetType().GetProperties();
|
||||
|
||||
foreach (var property in properties)
|
||||
{
|
||||
var attribute = property.GetCustomAttribute<TemplatePlaceholderAttribute>();
|
||||
|
||||
if (attribute != null)
|
||||
{
|
||||
var value = property.GetValue(model)?.ToString();
|
||||
template = template.Replace(attribute.Placeholder, value);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return template;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user