25fb48ccf0
- Extract email template handling, rendering, and sending code into `Worker.Services` project. - Introduce `EmailTemplateService`, `EmailTemplateRenderingService`, and `SendEmailService`. - Simplify consumer logic by delegating to scoped services. - Update project dependencies and package references accordingly.
25 lines
879 B
C#
25 lines
879 B
C#
namespace HrynCo.NotificationService.Worker.Services.EmailProcessing;
|
|
|
|
using System.Text;
|
|
using HrynCo.NotificationService.Contracts.Messages;
|
|
using HrynCo.NotificationService.DAL.Abstract.Templates;
|
|
|
|
internal sealed class EmailTemplateRenderingService : IEmailTemplateRenderingService
|
|
{
|
|
public RenderedEmail Render(EmailTemplate template, SendEmailMessageData data)
|
|
{
|
|
return new RenderedEmail(
|
|
Interpolate(template.Subject, data.Variables),
|
|
Interpolate(template.HtmlBody, data.Variables),
|
|
Interpolate(template.TextBody, data.Variables));
|
|
}
|
|
|
|
private static string Interpolate(string text, IReadOnlyDictionary<string, string> variables)
|
|
{
|
|
var sb = new StringBuilder(text);
|
|
foreach (var (key, value) in variables)
|
|
sb.Replace($"{{{{{key}}}}}", value);
|
|
return sb.ToString();
|
|
}
|
|
}
|