Files
Mercados-Web/src/Mercados.Api/Controllers/MercadosController.cs

100 lines
3.7 KiB
C#
Raw Normal View History

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;
private readonly ICotizacionGranoRepository _granoRepo;
private readonly ICotizacionGanadoRepository _ganadoRepo;
private readonly ILogger<MercadosController> _logger;
// Inyectamos TODOS los repositorios que necesita el controlador.
public MercadosController(
ICotizacionBolsaRepository bolsaRepo,
ICotizacionGranoRepository granoRepo,
ICotizacionGanadoRepository ganadoRepo,
ILogger<MercadosController> logger)
{
_bolsaRepo = bolsaRepo;
_granoRepo = granoRepo;
_ganadoRepo = ganadoRepo;
_logger = logger;
}
// --- 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 ---
[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.");
}
}
}
}