|
1 | | -using System.Globalization; |
2 | | -using System.Net.Mime; |
3 | | -using Microsoft.Extensions.DependencyInjection; |
| 1 | +using System.Net.Mime; |
4 | 2 | using OperationResults; |
5 | | -using PdfSmith.BusinessLayer.Exceptions; |
6 | | -using PdfSmith.BusinessLayer.Extensions; |
7 | 3 | using PdfSmith.BusinessLayer.Generators.Interfaces; |
8 | 4 | using PdfSmith.BusinessLayer.Services.Interfaces; |
9 | | -using PdfSmith.BusinessLayer.Templating.Interfaces; |
10 | 5 | using PdfSmith.Shared.Models; |
11 | 6 | using TinyHelpers.Extensions; |
12 | 7 |
|
13 | 8 | namespace PdfSmith.BusinessLayer.Services; |
14 | 9 |
|
15 | | -public class PdfService(IServiceProvider serviceProvider, IPdfGenerator pdfGenerator, ITimeZoneService timeZoneService) : IPdfService |
| 10 | +public class PdfService(ITemplateService templateService, IPdfGenerator pdfGenerator) : IPdfService |
16 | 11 | { |
17 | 12 | public async Task<Result<StreamFileContent>> GeneratePdfAsync(PdfGenerationRequest request, CancellationToken cancellationToken) |
18 | 13 | { |
19 | | - var templateEngine = serviceProvider.GetKeyedService<ITemplateEngine>(request.TemplateEngine!.ToLowerInvariant().Trim()); |
| 14 | + var templateResponse = await templateService.CreateAsync(request, cancellationToken); |
20 | 15 |
|
21 | | - if (templateEngine is null) |
| 16 | + if (!templateResponse.Success) |
22 | 17 | { |
23 | | - return Result.Fail(FailureReasons.ClientError, "Unable to render the template", $"The template engine '{request.TemplateEngine}' has not been registered"); |
| 18 | + return Result.Fail(templateResponse.FailureReason, templateResponse.ErrorMessage!, templateResponse.ErrorDetail!, templateResponse.ValidationErrors); |
24 | 19 | } |
25 | 20 |
|
26 | | - var timeZoneInfo = timeZoneService.GetTimeZone(); |
27 | | - |
28 | | - if (timeZoneInfo is null) |
29 | | - { |
30 | | - var timeZoneId = timeZoneService.GetTimeZoneHeaderValue(); |
31 | | - if (timeZoneId is not null) |
32 | | - { |
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"); |
35 | | - } |
36 | | - } |
37 | | - |
38 | | - string? content; |
39 | | - try |
40 | | - { |
41 | | - var model = request.Model?.ToExpandoObject(timeZoneInfo); |
42 | | - |
43 | | - cancellationToken.ThrowIfCancellationRequested(); |
44 | | - |
45 | | - content = await templateEngine.RenderAsync(request.Template, model, CultureInfo.CurrentCulture, cancellationToken); |
46 | | - |
47 | | - cancellationToken.ThrowIfCancellationRequested(); |
48 | | - } |
49 | | - catch (TemplateEngineException ex) |
50 | | - { |
51 | | - return Result.Fail(FailureReasons.ClientError, "Unable to render the template", ex.Message); |
52 | | - } |
53 | | - |
54 | | - var output = await pdfGenerator.CreateAsync(content, request.Options, cancellationToken); |
| 21 | + var output = await pdfGenerator.CreateAsync(templateResponse.Content.Result, request.Options, cancellationToken); |
55 | 22 |
|
56 | 23 | cancellationToken.ThrowIfCancellationRequested(); |
57 | 24 |
|
|
0 commit comments