feat: add UnderlyingChartDataService and DI registration

This commit is contained in:
2026-07-21 10:34:08 +02:00
parent 57f09cec62
commit a75d1711e6
2 changed files with 73 additions and 0 deletions

View File

@@ -35,6 +35,7 @@ builder.Services.AddHealthChecks()
builder.Services.AddScoped<ICertificateDataService, CertificateDataService>();
builder.Services.AddScoped<IChartDataService, ChartDataService>();
builder.Services.AddScoped<IChartDataServiceV2, ChartDataServiceV2>();
builder.Services.AddScoped<IUnderlyingChartDataService, UnderlyingChartDataService>();
builder.Services.AddScoped<IPdfSectionRenderer, AnagraficaSectionRenderer>();
builder.Services.AddScoped<IPdfSectionRenderer, EventiSectionRenderer>();
builder.Services.AddScoped<IPdfSectionRenderer, ScenarioSectionRenderer>();

View File

@@ -0,0 +1,72 @@
using CertReports.Syncfusion.Models;
using Microsoft.Data.SqlClient;
using System.Data;
namespace CertReports.Syncfusion.Services.Implementations;
/// <summary>
/// Recupera i dati per il grafico standalone di un singolo sottostante
/// (nessun contesto certificato). SP utilizzata: cedlab_Chart_UnderlyingOnly.
/// </summary>
public interface IUnderlyingChartDataService
{
Task<UnderlyingChartData?> GetChartDataAsync(int idUnderlyings);
}
public class UnderlyingChartDataService : IUnderlyingChartDataService
{
private readonly string _connectionString;
private readonly ILogger<UnderlyingChartDataService> _logger;
public UnderlyingChartDataService(IConfiguration config, ILogger<UnderlyingChartDataService> logger)
{
_connectionString = config.GetConnectionString("CertDb")
?? throw new InvalidOperationException("ConnectionString 'CertDb' non configurata.");
_logger = logger;
}
public async Task<UnderlyingChartData?> GetChartDataAsync(int idUnderlyings)
{
await using var conn = new SqlConnection(_connectionString);
await conn.OpenAsync();
await using var cmd = new SqlCommand("cedlab_Chart_UnderlyingOnly", conn)
{ CommandType = CommandType.StoredProcedure };
cmd.Parameters.AddWithValue("@IDUnderlyings", idUnderlyings);
var result = new UnderlyingChartData { IDUnderlyings = idUnderlyings };
await using var r = await cmd.ExecuteReaderAsync();
while (await r.ReadAsync())
{
if (result.Points.Count == 0)
result.Nome = ToStr(r, "Nome");
result.Points.Add(new UnderlyingChartPoint
{
Date = r.GetDateTime(r.GetOrdinal("Px_date")),
Px = Convert.ToDecimal(r.GetValue(r.GetOrdinal("Px"))),
});
}
if (result.Points.Count == 0)
{
_logger.LogWarning(
"Nessun prezzo trovato per il sottostante {IDUnderlyings} (non trovato, cancellato, sospeso, o senza storico prezzi)",
idUnderlyings);
return null;
}
_logger.LogInformation(
"Dati grafico sottostante caricati per {IDUnderlyings}: {Points} punti",
idUnderlyings, result.Points.Count);
return result;
}
private static string ToStr(SqlDataReader r, string column)
{
int ord = r.GetOrdinal(column);
return r.IsDBNull(ord) ? string.Empty : r.GetString(ord);
}
}