diff --git a/CertReports.Syncfusion/Program.cs b/CertReports.Syncfusion/Program.cs index 556622b..2fab149 100644 --- a/CertReports.Syncfusion/Program.cs +++ b/CertReports.Syncfusion/Program.cs @@ -35,6 +35,7 @@ builder.Services.AddHealthChecks() builder.Services.AddScoped(); builder.Services.AddScoped(); builder.Services.AddScoped(); +builder.Services.AddScoped(); builder.Services.AddScoped(); builder.Services.AddScoped(); builder.Services.AddScoped(); diff --git a/CertReports.Syncfusion/Services/Implementations/UnderlyingChartDataService.cs b/CertReports.Syncfusion/Services/Implementations/UnderlyingChartDataService.cs new file mode 100644 index 0000000..7a3c8b1 --- /dev/null +++ b/CertReports.Syncfusion/Services/Implementations/UnderlyingChartDataService.cs @@ -0,0 +1,72 @@ +using CertReports.Syncfusion.Models; +using Microsoft.Data.SqlClient; +using System.Data; + +namespace CertReports.Syncfusion.Services.Implementations; + +/// +/// Recupera i dati per il grafico standalone di un singolo sottostante +/// (nessun contesto certificato). SP utilizzata: cedlab_Chart_UnderlyingOnly. +/// +public interface IUnderlyingChartDataService +{ + Task GetChartDataAsync(int idUnderlyings); +} + +public class UnderlyingChartDataService : IUnderlyingChartDataService +{ + private readonly string _connectionString; + private readonly ILogger _logger; + + public UnderlyingChartDataService(IConfiguration config, ILogger logger) + { + _connectionString = config.GetConnectionString("CertDb") + ?? throw new InvalidOperationException("ConnectionString 'CertDb' non configurata."); + _logger = logger; + } + + public async Task 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); + } +}