diff --git a/CertReports.Syncfusion/Services/Implementations/FundDataService.cs b/CertReports.Syncfusion/Services/Implementations/FundDataService.cs new file mode 100644 index 0000000..8226911 --- /dev/null +++ b/CertReports.Syncfusion/Services/Implementations/FundDataService.cs @@ -0,0 +1,125 @@ +using System.Data; +using CertReports.Syncfusion.Models; +using CertReports.Syncfusion.Services.Interfaces; +using Microsoft.Data.SqlClient; + +namespace CertReports.Syncfusion.Services.Implementations; + +public class FundDataService : IFundDataService +{ + private readonly string _connectionString; + private readonly ILogger _logger; + + public FundDataService(IConfiguration config, ILogger logger) + { + _connectionString = config.GetConnectionString("CertDb") + ?? throw new InvalidOperationException("ConnectionString 'CertDb' non configurata."); + _logger = logger; + } + + public async Task GetFundInfoAsync(string isin) + { + try + { + await using var conn = new SqlConnection(_connectionString); + await conn.OpenAsync(); + await using var cmd = new SqlCommand("sfih_GetOptDettagli", conn) + { + CommandType = CommandType.StoredProcedure + }; + cmd.Parameters.AddWithValue("@ISIN", isin); + await using var r = await cmd.ExecuteReaderAsync(); + if (!await r.ReadAsync()) return null; + return MapFundInfo(r); + } + catch (Exception ex) + { + _logger.LogError(ex, "Errore GetFundInfoAsync per ISIN {Isin}", isin); + throw; + } + } + + public async Task> GetChartPricesAsync(string isin) + { + var points = new List(); + try + { + await using var conn = new SqlConnection(_connectionString); + await conn.OpenAsync(); + await using var cmd = new SqlCommand("sfih_GetChartPrices", conn) + { + CommandType = CommandType.StoredProcedure + }; + cmd.Parameters.AddWithValue("@ISIN", isin); + await using var r = await cmd.ExecuteReaderAsync(); + while (await r.ReadAsync()) + { + points.Add(new FundChartPoint + { + Date = r.GetSafe("Px_Date"), + Close = r.GetSafe("Px_Close") + }); + } + } + catch (Exception ex) + { + _logger.LogError(ex, "Errore GetChartPricesAsync per ISIN {Isin}", isin); + throw; + } + return points; + } + + public async Task FindIsinByAliasIdAsync(string aliasId) + { + await using var conn = new SqlConnection(_connectionString); + await conn.OpenAsync(); + await using var cmd = new SqlCommand("rpt_FindIsinbyAliasID", conn) + { + CommandType = CommandType.StoredProcedure + }; + cmd.Parameters.AddWithValue("@AliasID", aliasId); + await using var r = await cmd.ExecuteReaderAsync(); + if (!await r.ReadAsync()) return null; + var isin = r.GetStringSafe("ISIN"); + return string.IsNullOrEmpty(isin) ? null : isin; + } + + private static FundInfo MapFundInfo(SqlDataReader r) => new() + { + Isin = r.GetStringSafe("isn"), + Strumento = r.GetStringSafe("str"), + Tipo = r.GetStringSafe("typ").NullIfEmpty(), + Societa = r.GetStringSafe("soc").NullIfEmpty(), + CategoriaMorningstar = r.GetStringSafe("msc").NullIfEmpty(), + Valuta = r.GetStringSafe("val").NullIfEmpty(), + Hedged = r.GetStringSafe("hed").NullIfEmpty(), + Benchmark = r.GetStringSafe("bmk").NullIfEmpty(), + Catalogo = r.GetStringSafe("itr").NullIfEmpty(), + Proventi = r.GetStringSafe("prv").NullIfEmpty(), + DataLancio = r.GetSafe("daf") == default ? null : r.GetSafe("daf"), + Patrimonio = r.GetSafe("pat") == 0m ? null : r.GetSafe("pat"), + SpeseCorrenti = r.GetSafe("spc"), + Prezzo = r.GetSafe("prz") == 0m ? null : r.GetSafe("prz"), + DataPrezzo = r.GetSafe("dpz") == default ? null : r.GetSafe("dpz"), + Rank = r.GetSafe("rnk"), + P3M = r.GetSafe("p3M"), P6M = r.GetSafe("p6M"), + PYD = r.GetSafe("pYD"), P1Y = r.GetSafe("p1Y"), + P3Y = r.GetSafe("p3Y"), P5Y = r.GetSafe("p5Y"), + V3M = r.GetSafe("v3M"), V6M = r.GetSafe("v6M"), + VYD = r.GetSafe("vYD"), V1Y = r.GetSafe("v1Y"), + V3Y = r.GetSafe("v3Y"), V5Y = r.GetSafe("v5Y"), + R3M = r.GetSafe("r3M"), R6M = r.GetSafe("r6M"), + RYD = r.GetSafe("rYD"), R1Y = r.GetSafe("r1Y"), + R3Y = r.GetSafe("r3Y"), R5Y = r.GetSafe("r5Y"), + Sustainability = r.GetSafe("sus"), + Environmental = r.GetSafe("env"), + Social = r.GetSafe("ssc"), + Governance = r.GetSafe("gov"), + }; +} + +file static class StringExtensions +{ + public static string? NullIfEmpty(this string s) => + string.IsNullOrEmpty(s) ? null : s; +}