diff --git a/src/Mercados.Api/Controllers/MercadosController.cs b/src/Mercados.Api/Controllers/MercadosController.cs index 821b772..c0c5aba 100644 --- a/src/Mercados.Api/Controllers/MercadosController.cs +++ b/src/Mercados.Api/Controllers/MercadosController.cs @@ -38,9 +38,7 @@ namespace Mercados.Api.Controllers { try { - var hoy = DateOnly.FromDateTime(DateTime.UtcNow); - var data = await _ganadoRepo.ObtenerTandaPorFechaAsync(hoy); - + var data = await _ganadoRepo.ObtenerUltimaTandaDisponibleAsync(); return Ok(data); } catch (Exception ex) diff --git a/src/Mercados.Infrastructure/Persistence/Repositories/CotizacionGanadoRepository.cs b/src/Mercados.Infrastructure/Persistence/Repositories/CotizacionGanadoRepository.cs index d529a32..a1852ff 100644 --- a/src/Mercados.Infrastructure/Persistence/Repositories/CotizacionGanadoRepository.cs +++ b/src/Mercados.Infrastructure/Persistence/Repositories/CotizacionGanadoRepository.cs @@ -13,6 +13,22 @@ namespace Mercados.Infrastructure.Persistence.Repositories _connectionFactory = connectionFactory; } + public async Task> ObtenerUltimaTandaDisponibleAsync() + { + using IDbConnection connection = _connectionFactory.CreateConnection(); + // Esta consulta busca la fecha más reciente que tiene registros + // y luego devuelve todos los registros de esa fecha. Es la lógica original y correcta. + const string sql = @" + SELECT * FROM CotizacionesGanado + WHERE CONVERT(date, FechaRegistro) = ( + SELECT TOP 1 CONVERT(date, FechaRegistro) + FROM CotizacionesGanado + ORDER BY FechaRegistro DESC + );"; + return await connection.QueryAsync(sql); + } + + // Este método lo sigue necesitando el Fetcher para comparar los datos del día. public async Task> ObtenerTandaPorFechaAsync(DateOnly fecha) { using IDbConnection connection = _connectionFactory.CreateConnection(); diff --git a/src/Mercados.Infrastructure/Persistence/Repositories/ICotizacionGanadoRepository.cs b/src/Mercados.Infrastructure/Persistence/Repositories/ICotizacionGanadoRepository.cs index 84f7f23..e12a4a9 100644 --- a/src/Mercados.Infrastructure/Persistence/Repositories/ICotizacionGanadoRepository.cs +++ b/src/Mercados.Infrastructure/Persistence/Repositories/ICotizacionGanadoRepository.cs @@ -4,9 +4,9 @@ namespace Mercados.Infrastructure.Persistence.Repositories { public interface ICotizacionGanadoRepository : IBaseRepository { - Task> ObtenerTandaPorFechaAsync(DateOnly fecha); - Task GuardarMuchosAsync(IEnumerable cotizaciones); + Task> ObtenerUltimaTandaDisponibleAsync(); Task ReemplazarTandaDelDiaAsync(DateOnly fecha, IEnumerable nuevasCotizaciones); Task> ObtenerHistorialAsync(string categoria, string especificaciones, int dias); + Task> ObtenerTandaPorFechaAsync(DateOnly fecha); } } \ No newline at end of file