diff --git a/CertReports.Syncfusion/Controllers/ChartController.cs b/CertReports.Syncfusion/Controllers/ChartController.cs index 8db7b12..38220d2 100644 --- a/CertReports.Syncfusion/Controllers/ChartController.cs +++ b/CertReports.Syncfusion/Controllers/ChartController.cs @@ -20,11 +20,16 @@ namespace CertReports.Syncfusion.Controllers; public class ChartController : ControllerBase { private readonly IChartDataService _chartDataService; + private readonly IChartDataServiceV2 _chartDataServiceV2; private readonly ILogger _logger; - public ChartController(IChartDataService chartDataService, ILogger logger) + public ChartController( + IChartDataService chartDataService, + IChartDataServiceV2 chartDataServiceV2, + ILogger logger) { _chartDataService = chartDataService; + _chartDataServiceV2 = chartDataServiceV2; _logger = logger; } @@ -77,6 +82,55 @@ public class ChartController : ControllerBase } } + /// + /// Endpoint V2: grafico migliorato con titolo, colori distinti, label sulle linee e legenda in basso. + /// Richiede SP cedlab_Chart_UL1 e cedlab_Chart_AllSeriesV2 nel DB. + /// + [HttpGet("v2/{isin}")] + public async Task GenerateChartV2( + string isin, + [FromQuery] int width = 1100, + [FromQuery] int height = 700, + [FromQuery] string format = "png") + { + if (string.IsNullOrWhiteSpace(isin)) + return BadRequest("ISIN non valido."); + + width = Math.Clamp(width, 400, 2000); + height = Math.Clamp(height, 300, 1500); + + try + { + var chartData = await _chartDataServiceV2.GetChartDataV2Async(isin); + + if (chartData == null || chartData.SeriesPoints.Count == 0) + { + return NotFound(new + { + status = "KO", + message = $"Nessun dato per il grafico V2 di {isin} (meno di 30 prezzi EOD?).", + }); + } + + byte[] pngBytes = SkiaChartRendererV2.RenderToPng(chartData, width, height); + + if (format.Equals("pdf", StringComparison.OrdinalIgnoreCase)) + { + byte[] pdfBytes = WrapPngInPdf(pngBytes); + Response.Headers.Append("Content-Disposition", $"inline; filename=chart_v2_{isin}.pdf"); + return File(pdfBytes, "application/pdf"); + } + + Response.Headers.Append("Content-Disposition", $"inline; filename=chart_v2_{isin}.png"); + return File(pngBytes, "image/png"); + } + catch (Exception ex) + { + _logger.LogError(ex, "Errore generazione chart V2 per ISIN {Isin}", isin); + return StatusCode(500, new { status = "KO", message = "Errore nella generazione del grafico V2." }); + } + } + private static byte[] WrapPngInPdf(byte[] pngBytes) { var doc = new PdfDocument(); diff --git a/CertReports.Syncfusion/Program.cs b/CertReports.Syncfusion/Program.cs index 662316b..b9d5f0c 100644 --- a/CertReports.Syncfusion/Program.cs +++ b/CertReports.Syncfusion/Program.cs @@ -41,6 +41,7 @@ builder.Services.AddHealthChecks() // Registra i servizi applicativi builder.Services.AddScoped(); builder.Services.AddScoped(); +builder.Services.AddScoped(); builder.Services.AddScoped(); builder.Services.AddScoped(); builder.Services.AddScoped();