Skip to content

Commit 899f6c2

Browse files
committed
Refactor template engine handling and improve comments
- Updated `JsonDocumentExtensions.cs` for comment clarity. - Enhanced null checks and error handling in `PdfService.cs` for template engine and time zone management. - Allowed nullable model parameters in `HandlebarsTemplateEngine.cs`, `RazorTemplateEngine.cs`, and `ScribanTemplateEngine.cs`. - Updated `ITemplateEngine.cs` interface for consistency with nullable model parameters. - Renamed constant in `ScribanTemplateEngine.cs` for improved clarity.
1 parent b064ecf commit 899f6c2

File tree

6 files changed

+27
-37
lines changed

6 files changed

+27
-37
lines changed

src/PdfSmith.BusinessLayer/Extensions/JsonDocumentExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public static class JsonDocumentExtensions
2727
return element.EnumerateArray().Select(e => ConvertValue(e, timeZoneInfo)).ToList();
2828
}
2929

30-
// For all other cases (including Null), delegate to ConvertValue which handles them properly
30+
// For all other cases (including Null), delegate to ConvertValue which handles them properly.
3131
return ConvertValue(element, timeZoneInfo);
3232
}
3333

src/PdfSmith.BusinessLayer/Services/PdfService.cs

Lines changed: 21 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -16,47 +16,39 @@ public class PdfService(IServiceProvider serviceProvider, IPdfGenerator pdfGener
1616
{
1717
public async Task<Result<StreamFileContent>> GeneratePdfAsync(PdfGenerationRequest request, CancellationToken cancellationToken)
1818
{
19-
string? content;
19+
var templateEngine = serviceProvider.GetKeyedService<ITemplateEngine>(request.TemplateEngine!.ToLowerInvariant().Trim());
2020

21-
if (request.Model is not null)
21+
if (templateEngine is null)
2222
{
23-
var templateEngine = serviceProvider.GetKeyedService<ITemplateEngine>(request.TemplateEngine!.ToLowerInvariant().Trim());
24-
25-
if (templateEngine is null)
26-
{
27-
return Result.Fail(FailureReasons.ClientError, "Unable to render the template", $"The template engine '{request.TemplateEngine}' has not been registered");
28-
}
23+
return Result.Fail(FailureReasons.ClientError, "Unable to render the template", $"The template engine '{request.TemplateEngine}' has not been registered");
24+
}
2925

30-
var timeZoneInfo = timeZoneService.GetTimeZone();
26+
var timeZoneInfo = timeZoneService.GetTimeZone();
3127

32-
if (timeZoneInfo is null)
28+
if (timeZoneInfo is null)
29+
{
30+
var timeZoneId = timeZoneService.GetTimeZoneHeaderValue();
31+
if (timeZoneId is not null)
3332
{
34-
var timeZoneId = timeZoneService.GetTimeZoneHeaderValue();
35-
if (timeZoneId is not null)
36-
{
37-
// If timeZoneInfo is null, but timeZoneId has a value, it means that the time zone specified in the header is invalid.
38-
return Result.Fail(FailureReasons.ClientError, "Unable to find the time zone", $"The time zone '{timeZoneId}' is invalid or is not available on the system");
39-
}
33+
// If timeZoneInfo is null, but timeZoneId has a value, it means that the time zone specified in the header is invalid.
34+
return Result.Fail(FailureReasons.ClientError, "Unable to find the time zone", $"The time zone '{timeZoneId}' is invalid or is not available on the system");
4035
}
36+
}
4137

42-
try
43-
{
44-
var model = request.Model.ToExpandoObject(timeZoneInfo);
38+
string? content;
39+
try
40+
{
41+
var model = request.Model?.ToExpandoObject(timeZoneInfo);
4542

46-
cancellationToken.ThrowIfCancellationRequested();
43+
cancellationToken.ThrowIfCancellationRequested();
4744

48-
content = await templateEngine.RenderAsync(request.Template, model, CultureInfo.CurrentCulture, cancellationToken);
45+
content = await templateEngine.RenderAsync(request.Template, model, CultureInfo.CurrentCulture, cancellationToken);
4946

50-
cancellationToken.ThrowIfCancellationRequested();
51-
}
52-
catch (TemplateEngineException ex)
53-
{
54-
return Result.Fail(FailureReasons.ClientError, "Unable to render the template", ex.Message);
55-
}
47+
cancellationToken.ThrowIfCancellationRequested();
5648
}
57-
else
49+
catch (TemplateEngineException ex)
5850
{
59-
content = request.Template;
51+
return Result.Fail(FailureReasons.ClientError, "Unable to render the template", ex.Message);
6052
}
6153

6254
var output = await pdfGenerator.CreateAsync(content, request.Options, cancellationToken);

src/PdfSmith.BusinessLayer/Templating/HandlebarsTemplateEngine.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ public class HandlebarsTemplateEngine(TimeZoneTimeProvider timeZoneTimeProvider)
1010
{
1111
private readonly Lazy<IHandlebars> handlebarsInstance = new(() => CreateHandlebarsInstance(timeZoneTimeProvider));
1212

13-
public Task<string> RenderAsync(string template, object model, CultureInfo culture, CancellationToken cancellationToken = default)
13+
public Task<string> RenderAsync(string template, object? model, CultureInfo culture, CancellationToken cancellationToken = default)
1414
{
1515
try
1616
{

src/PdfSmith.BusinessLayer/Templating/Interfaces/ITemplateEngine.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,5 @@ namespace PdfSmith.BusinessLayer.Templating.Interfaces;
44

55
public interface ITemplateEngine
66
{
7-
Task<string> RenderAsync(string template, object model, CultureInfo culture, CancellationToken cancellationToken = default);
7+
Task<string> RenderAsync(string template, object? model, CultureInfo culture, CancellationToken cancellationToken = default);
88
}

src/PdfSmith.BusinessLayer/Templating/RazorTemplateEngine.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ namespace PdfSmith.BusinessLayer.Templating;
99

1010
public partial class RazorTemplateEngine(IRazorLightEngine engine) : ITemplateEngine
1111
{
12-
public async Task<string> RenderAsync(string template, object model, CultureInfo culture, CancellationToken cancellationToken = default)
12+
public async Task<string> RenderAsync(string template, object? model, CultureInfo culture, CancellationToken cancellationToken = default)
1313
{
1414
try
1515
{
@@ -20,9 +20,7 @@ public async Task<string> RenderAsync(string template, object model, CultureInfo
2020
@using System
2121
@using System.Collections.Generic
2222
@using System.Linq
23-
2423
@inject PdfSmith.BusinessLayer.Services.TimeZoneTimeProvider timeZoneTimeProvider
25-
2624
{sanitizedTemplate}
2725
""";
2826

src/PdfSmith.BusinessLayer/Templating/ScribanTemplateEngine.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ namespace PdfSmith.BusinessLayer.Templating;
1010

1111
public partial class ScribanTemplateEngine(TimeZoneTimeProvider timeZoneTimeProvider) : ITemplateEngine
1212
{
13-
private const string DateTimeZonePlaceholder = "date_time_zone";
13+
private const string DateTimeZonePlaceholder = "datetime_withzone";
1414

15-
public async Task<string> RenderAsync(string text, object model, CultureInfo culture, CancellationToken cancellationToken = default)
15+
public async Task<string> RenderAsync(string text, object? model, CultureInfo culture, CancellationToken cancellationToken = default)
1616
{
1717
var sanitizedText = DateNowRegex.Replace(text, DateTimeZonePlaceholder);
1818

0 commit comments

Comments
 (0)