2025-07-01 12:19:00 -03:00
|
|
|
using Mercados.Core.Entities;
|
|
|
|
|
using Mercados.Infrastructure.Persistence.Repositories;
|
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
|
|
|
|
|
|
namespace Mercados.Api.Controllers
|
|
|
|
|
{
|
|
|
|
|
[ApiController]
|
|
|
|
|
[Route("api/[controller]")]
|
|
|
|
|
public class MercadosController : ControllerBase
|
|
|
|
|
{
|
|
|
|
|
private readonly ICotizacionBolsaRepository _bolsaRepo;
|
2025-07-01 12:45:32 -03:00
|
|
|
private readonly ICotizacionGranoRepository _granoRepo;
|
|
|
|
|
private readonly ICotizacionGanadoRepository _ganadoRepo;
|
2025-07-01 12:19:00 -03:00
|
|
|
private readonly ILogger<MercadosController> _logger;
|
|
|
|
|
|
2025-07-01 12:45:32 -03:00
|
|
|
// Inyectamos TODOS los repositorios que necesita el controlador.
|
|
|
|
|
public MercadosController(
|
|
|
|
|
ICotizacionBolsaRepository bolsaRepo,
|
|
|
|
|
ICotizacionGranoRepository granoRepo,
|
|
|
|
|
ICotizacionGanadoRepository ganadoRepo,
|
|
|
|
|
ILogger<MercadosController> logger)
|
2025-07-01 12:19:00 -03:00
|
|
|
{
|
|
|
|
|
_bolsaRepo = bolsaRepo;
|
2025-07-01 12:45:32 -03:00
|
|
|
_granoRepo = granoRepo;
|
|
|
|
|
_ganadoRepo = ganadoRepo;
|
2025-07-01 12:19:00 -03:00
|
|
|
_logger = logger;
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-01 12:45:32 -03:00
|
|
|
// --- Endpoint para Agroganadero ---
|
|
|
|
|
[HttpGet("agroganadero")]
|
|
|
|
|
[ProducesResponseType(typeof(IEnumerable<CotizacionGanado>), StatusCodes.Status200OK)]
|
|
|
|
|
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
|
|
|
|
|
public async Task<IActionResult> GetAgroganadero()
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var data = await _ganadoRepo.ObtenerUltimaTandaAsync();
|
|
|
|
|
return Ok(data);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogError(ex, "Error al obtener cotizaciones de agroganadero.");
|
|
|
|
|
return StatusCode(500, "Ocurrió un error interno en el servidor.");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// --- Endpoint para Granos ---
|
|
|
|
|
[HttpGet("granos")]
|
|
|
|
|
[ProducesResponseType(typeof(IEnumerable<CotizacionGrano>), StatusCodes.Status200OK)]
|
|
|
|
|
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
|
|
|
|
|
public async Task<IActionResult> GetGranos()
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var data = await _granoRepo.ObtenerUltimasAsync();
|
|
|
|
|
return Ok(data);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogError(ex, "Error al obtener cotizaciones de granos.");
|
|
|
|
|
return StatusCode(500, "Ocurrió un error interno en el servidor.");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// --- Endpoints de Bolsa ---
|
2025-07-01 12:19:00 -03:00
|
|
|
[HttpGet("bolsa/eeuu")]
|
|
|
|
|
[ProducesResponseType(typeof(IEnumerable<CotizacionBolsa>), StatusCodes.Status200OK)]
|
|
|
|
|
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
|
|
|
|
|
public async Task<IActionResult> GetBolsaUsa()
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var data = await _bolsaRepo.ObtenerUltimasPorMercadoAsync("EEUU");
|
|
|
|
|
return Ok(data);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogError(ex, "Error al obtener cotizaciones de bolsa de EEUU.");
|
|
|
|
|
return StatusCode(500, "Ocurrió un error interno en el servidor.");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[HttpGet("bolsa/local")]
|
|
|
|
|
[ProducesResponseType(typeof(IEnumerable<CotizacionBolsa>), StatusCodes.Status200OK)]
|
|
|
|
|
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
|
|
|
|
|
public async Task<IActionResult> GetBolsaLocal()
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var data = await _bolsaRepo.ObtenerUltimasPorMercadoAsync("Local");
|
|
|
|
|
return Ok(data);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogError(ex, "Error al obtener cotizaciones de bolsa local.");
|
|
|
|
|
return StatusCode(500, "Ocurrió un error interno en el servidor.");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|