Refactor ReportCreator: remove logging and simplify logic
Removed ILogger dependency and all related logging from ReportCreator. Inlined DoCreateReport into CreateReportAsync and eliminated the try-catch block, allowing exceptions to propagate naturally. Improved the error message for missing report data. The class is now more focused and streamlined.
This commit is contained in:
@@ -3,54 +3,28 @@ using DigitalData.Core.Abstraction.Application.Repository;
|
||||
using EnvelopeGenerator.Domain.Constants;
|
||||
using EnvelopeGenerator.Domain.Entities;
|
||||
using EnvelopeGenerator.ServiceHost.Exceptions;
|
||||
using EnvelopeGenerator.ServiceHost.Extensions;
|
||||
|
||||
namespace EnvelopeGenerator.ServiceHost.Jobs.FinalizeDocument;
|
||||
|
||||
public class ReportCreator(ILogger<ReportCreator> Logger, IRepository<EnvelopeReport> reportRepo)
|
||||
public class ReportCreator(IRepository<EnvelopeReport> reportRepo)
|
||||
{
|
||||
public async Task<byte[]> CreateReportAsync(Envelope envelope, CancellationToken cancel = default)
|
||||
{
|
||||
try
|
||||
{
|
||||
Logger.LogDebug("Loading report data..");
|
||||
var reports = await reportRepo.Where(r => r.EnvelopeId == envelope.Id).ToListAsync(cancel);
|
||||
var reports = await reportRepo.Where(r => r.EnvelopeId == envelope.Id).ToListAsync(cancel);
|
||||
|
||||
if (reports.Count == 0)
|
||||
{
|
||||
throw new CreateReportException("No report data found!");
|
||||
}
|
||||
if (reports.Count == 0)
|
||||
throw new CreateReportException("Could not prepare report data! No report data found!");
|
||||
|
||||
var items = reports
|
||||
.Select(r => ToReportItem(r, envelope))
|
||||
.OrderByDescending(r => r.ItemDate)
|
||||
.ToList();
|
||||
var items = reports
|
||||
.Select(r => ToReportItem(r, envelope))
|
||||
.OrderByDescending(r => r.ItemDate)
|
||||
.ToList();
|
||||
|
||||
var buffer = DoCreateReport(items);
|
||||
Logger.LogDebug("Report created!");
|
||||
|
||||
return buffer;
|
||||
}
|
||||
catch (Exception ex) when (ex is not CreateReportException)
|
||||
{
|
||||
Logger.LogError(ex);
|
||||
throw new CreateReportException("Could not prepare report data!", ex);
|
||||
}
|
||||
}
|
||||
|
||||
private byte[] DoCreateReport(List<ReportItem> reportItems)
|
||||
{
|
||||
var source = new ReportSource { Items = reportItems };
|
||||
var source = new ReportSource { Items = items };
|
||||
var report = new rptEnvelopeHistory { DataSource = source, DataMember = "Items" };
|
||||
|
||||
Logger.LogDebug("Creating report in memory..");
|
||||
report.CreateDocument();
|
||||
|
||||
Logger.LogDebug("Exporting report to stream..");
|
||||
using var stream = new MemoryStream();
|
||||
report.ExportToPdf(stream);
|
||||
|
||||
Logger.LogDebug("Writing report to buffer..");
|
||||
return stream.ToArray();
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user