-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat_api(be):Added Exporter2Pdf and first query exporting warnings li…
…st by company
- Loading branch information
1 parent
a68dcc1
commit 76364a2
Showing
5 changed files
with
54 additions
and
7 deletions.
There are no files selected for viewing
39 changes: 36 additions & 3 deletions
39
...e.Application/Features/Warning/Query/Export/List/ExportWarningsByCompanyCommandHandler.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 14 additions & 1 deletion
15
backend/ElectroManage.Infraestructure/Plugins/Exporters/PdfExporter.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters