FillTemplate-Methode als Erweiterung hinzugefügt. Option zum Schreiben von Platzhaltern als Wörterbuch hinzugefügt.
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
using DigitalData.Core.API;
|
||||
using DigitalData.EmailProfilerDispatcher.API.Resources;
|
||||
using DigitalData.EmailProfilerDispatcher.Application;
|
||||
using DigitalData.EmailProfilerDispatcher.Application.Contracts;
|
||||
using DigitalData.EmailProfilerDispatcher.Application.DTOs.EmailOut;
|
||||
using DigitalData.EmailProfilerDispatcher.Application.Services;
|
||||
@@ -39,7 +40,7 @@ namespace DigitalData.EmailProfilerGateway.API.Controllers
|
||||
<br />
|
||||
DokumentenPortal";
|
||||
|
||||
var result = EmailOutService<Resource>.FillTemplate(template, mailData);
|
||||
var result = template.FillTemplate(mailData);
|
||||
|
||||
return Ok(result);
|
||||
}
|
||||
|
||||
@@ -17,35 +17,20 @@ namespace DigitalData.EmailProfilerDispatcher.Application.Services
|
||||
{
|
||||
}
|
||||
|
||||
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);
|
||||
createDto.EmailSubj = createDto.EmailSubj.FillTemplate(models);
|
||||
createDto.EmailBody = createDto.EmailBody.FillTemplate(models);
|
||||
|
||||
return await base.CreateAsync(createDto);
|
||||
}
|
||||
|
||||
public static string FillTemplate(string template, params object[] models)
|
||||
public async Task<DataResult<int>> CreateWithTemplateAsync(EmailOutCreateDto createDto, Dictionary<string, string> placeholders, params object[] models)
|
||||
{
|
||||
foreach(var model in models)
|
||||
{
|
||||
var properties = model.GetType().GetProperties();
|
||||
createDto.EmailSubj = createDto.EmailSubj.FillTemplate(placeholders);
|
||||
createDto.EmailBody = createDto.EmailBody.FillTemplate(placeholders);
|
||||
|
||||
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;
|
||||
return await CreateWithTemplateAsync(createDto, models: models);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
using DigitalData.EmailProfilerDispatcher.Domain.Attributes;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace DigitalData.EmailProfilerDispatcher.Application
|
||||
{
|
||||
public static class TemplateExtensions
|
||||
{
|
||||
public static string FillTemplate(this 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;
|
||||
}
|
||||
|
||||
public static string FillTemplate(this string template, Dictionary<string, string> placeholders)
|
||||
{
|
||||
foreach (var ph in placeholders)
|
||||
template = template.Replace(ph.Key, ph.Value);
|
||||
|
||||
return template;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,8 @@
|
||||
using DigitalData.EmailProfilerDispatcher.Application;
|
||||
using DigitalData.EmailProfilerDispatcher.Application.Services;
|
||||
using DigitalData.EmailProfilerDispatcher.Domain.Attributes;
|
||||
using DigitalData.EmailProfilerDispatcher.Domain.Entities;
|
||||
using System.Diagnostics.Tracing;
|
||||
|
||||
namespace EmailProfilerDispatcher.Tests
|
||||
{
|
||||
@@ -27,7 +29,8 @@ namespace EmailProfilerDispatcher.Tests
|
||||
|
||||
var mailData1 = new MailData1();
|
||||
var mailData2 = new MailData2();
|
||||
var mailData3 = new MailData3();
|
||||
Dictionary<string, string> placeholders = new();
|
||||
placeholders.Add(@"[NAME_PORTAL]", "DokumentenPortal");
|
||||
|
||||
var expectedOutput = @"Guten Tag Tom,<br />
|
||||
<br />
|
||||
@@ -39,7 +42,9 @@ namespace EmailProfilerDispatcher.Tests
|
||||
DokumentenPortal";
|
||||
|
||||
// Act
|
||||
var result = EmailOutService<Resource>.FillTemplate(template, mailData1, mailData2, mailData3);
|
||||
var result = template
|
||||
.FillTemplate(placeholders)
|
||||
.FillTemplate(template, mailData1, mailData2);
|
||||
|
||||
// Assert
|
||||
Assert.That(result, Is.EqualTo(expectedOutput));
|
||||
@@ -58,11 +63,5 @@ namespace EmailProfilerDispatcher.Tests
|
||||
[TemplatePlaceholder("[DOCUMENT_TITLE]")]
|
||||
public string DocumentTitle { get; set; } = "Vertragsdokument";
|
||||
}
|
||||
|
||||
public class MailData3
|
||||
{
|
||||
[TemplatePlaceholder("[NAME_PORTAL]")]
|
||||
public string NamePortal { get; set; } = "DokumentenPortal";
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user