-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEmailTemplates.cs
72 lines (60 loc) · 2.28 KB
/
EmailTemplates.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
using System;
using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;
using Fluid;
using Microsoft.AspNetCore.WebUtilities;
using Microsoft.Extensions.Configuration;
namespace VLO_BOARDS
{
public class EmailTemplates
{
public readonly Dictionary<string, string> Templates = new Dictionary<string, string>();
public readonly IConfiguration _configuration;
public EmailTemplates(IConfiguration configuration)
{
_configuration = configuration;
try
{
string path = Directory.GetCurrentDirectory();
List<string> files = new List<string>(Directory.GetFiles(Path.Combine(path, "fluidTemplates")));
foreach (var filePath in files)
{
string contents = File.ReadAllText(filePath);
Templates.Add(Path.GetFileName(filePath), contents);
}
}
catch
{
throw new FileLoadException("Failed to load templates");
}
}
public async Task<string> RenderFluid(string templateName, Object model)
{
var parser = new FluidParser();
var parserResult = parser.TryParse(Templates[templateName],
out var template, out var error);
if (!parserResult)
{
Console.WriteLine($"Eror: {error}");
throw new ApplicationException("Error rendering email");
}
var renderContext = new TemplateContext(model);
var renderedTemplate = await template.RenderAsync(renderContext);
var body = PreMailer.Net.PreMailer.MoveCssInline(renderedTemplate).Html;
return body;
}
public Uri GenerateUrl(string route, Dictionary<string, string> qp = null)
{
var BaseOrigin = _configuration["BaseOrigin"];
if (!route.StartsWith("/"))
{
route = "/" + route;
}
var url = BaseOrigin + route;
qp ??= new Dictionary<string, string>();
var genUrl = new Uri(QueryHelpers.AddQueryString(url, qp));
return genUrl;
}
}
}