Skip to content

Commit

Permalink
feat_api(be):Added Exporter2Pdf and first query exporting warnings li…
Browse files Browse the repository at this point in the history
…st by company
  • Loading branch information
Javi111003 committed Feb 2, 2025
1 parent a68dcc1 commit 76364a2
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -1,25 +1,58 @@
using ElectroManage.Domain.DataAccess.Abstractions;
using ElectroManage.Infraestructure.Plugins;
using ElectroManage.Infraestructure.Services;
using Microsoft.Extensions.Logging;
using System.Linq.Expressions;

namespace ElectroManage.Application.Features.Warning.Query.Export.List;
public class ExportWarningsByCompanyCommandHandler : CoreQueryHandler<ExportWarningsByCompanyCommand, byte[]>
{
readonly ILogger<ExportWarningsByCompanyCommandHandler> _logger;
readonly ITemplateService _templateService;
readonly PluginLoader _pluginLoader;
public ExportWarningsByCompanyCommandHandler(
ILogger<ExportWarningsByCompanyCommandHandler> logger,
ITemplateService templateService,
IExporter exporter,
PluginLoader pluginLoader,
IUnitOfWork unitOfWork) : base(unitOfWork)
{
_pluginLoader = pluginLoader;
_logger = logger;
_templateService = templateService;
}
public override Task<byte[]> ExecuteAsync(ExportWarningsByCompanyCommand command, CancellationToken ct = default)
public override async Task<byte[]> ExecuteAsync(ExportWarningsByCompanyCommand command, CancellationToken ct = default)
{
_logger.LogInformation($"{nameof(ExecuteAsync)} | Execution started");
var userRepo = UnitOfWork!.DbRepository<Domain.Entites.Identity.AppUser>();
var userInclude = new List<Expression<Func<Domain.Entites.Identity.AppUser, object>>>
{
x => x.Company
};
var companyRepo = UnitOfWork!.DbRepository<Domain.Entites.Sucursal.Company>();
var companyInclude = new List<Expression<Func<Domain.Entites.Sucursal.Company, object>>>
{
x => x.Warnings
};
var user = await userRepo.FirstAsync(filters: u => u.Id == command.UserId, includes: userInclude);
if(user == null)
{
_logger.LogError($"{nameof(ExecuteAsync)} | User with id : {command.UserId} not found");
ThrowError($"User with id : {command.UserId} not found", 404);
}
var company = await companyRepo.FirstAsync(filters: c => c.Id == command.CompanyId, includes: companyInclude);
if(company == null)
{
_logger.LogError($"{nameof(ExecuteAsync)} | Company with id : {command.CompanyId} not found");
ThrowError($" Company with id : {command.CompanyId} not found", 404);
}
var html = _templateService.GetAlertsTemplate(user, company);
if(!_pluginLoader.TryGetExporter(command.Format, out var exporter))
{
_logger.LogError($"{nameof(ExecuteAsync)} | Exporter for format: {command.Format} is not implemented");
ThrowError($" Exporter for format: {command.Format} is not implemented", 404);
}
var pdf = exporter!.Export(html);
_logger.LogInformation($"{nameof(ExecuteAsync)} | Execution completed");
throw new NotImplementedException();
return pdf;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public string GetAlertsTemplate(User user, Domain.Entites.Sucursal.Company compa
private string GetHeaderTemplate(User user)
{
var html = File.ReadAllText(HeaderTemplateUrl);
html = html.Replace("{{user_name}}", user.UserName)
html = html.Replace("{{username}}", user.UserName)
.Replace("{{email}}", user.Email)
.Replace("{{companyName}}", user.Company.Name)
.Replace("{{logo}}", LogoUrl);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
<ItemGroup>
<PackageReference Include="FastEndpoints" Version="5.30.0" />
<PackageReference Include="Microsoft.IdentityModel.Tokens" Version="8.3.0" />
<PackageReference Include="Select.HtmlToPdf.NetCore" Version="24.1.0" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,24 @@
using ElectroManage.Infraestructure.Services;
using SelectPdf;

namespace ElectroManage.Infraestructure.Plugins.Exporters;
public class PdfExporter : IExporter
{
public string Format => "pdf";
public byte[] Export(string data)
{
throw new NotImplementedException();
// Crear una instancia del convertidor HTML a PDF
HtmlToPdf converter = new HtmlToPdf();

// Convertir el HTML a un documento PDF
PdfDocument doc = converter.ConvertHtmlString(data);

// Guardar el documento PDF en un array de bytes
byte[] pdf = doc.Save();

// Cerrar el documento para liberar recursos
doc.Close();

return pdf;
}
}
4 changes: 2 additions & 2 deletions backend/ElectroManage.WebAPI/ServiceExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ public static void AddCustomServicesExtension(this IServiceCollection services)
services.AddScoped<IFileWriterService, FileWriterService>();
services.AddScoped<ICostCalculator, CostCalculator>();
services.AddScoped<IProyectionService, ProyectionService>();
services.AddScoped<IExporter, PdfExporter>();
//services.AddScoped<IExporter, PdfExporter>();
services.AddScoped<ITemplateService, TemplateService>();
//services.AddSingleton<PluginLoader>();
services.AddSingleton<PluginLoader>();
services.AddSingleton<IEmailSender<AppUser>, IdentityEmailSender>();
}
public static void AddGenericRepositoryExtension(this IServiceCollection services)
Expand Down

0 comments on commit 76364a2

Please sign in to comment.