using Microsoft.AspNetCore.Mvc; using MotoresArgentinosV2.Core.Entities; using MotoresArgentinosV2.Core.Interfaces; namespace MotoresArgentinosV2.API.Controllers; [ApiController] [Route("api/[controller]")] public class OperacionesLegacyController : ControllerBase { private readonly IOperacionesLegacyService _operacionesService; private readonly ILogger _logger; public OperacionesLegacyController(IOperacionesLegacyService operacionesService, ILogger logger) { _operacionesService = operacionesService; _logger = logger; } /// /// Obtiene los medios de pago disponibles /// [HttpGet("medios-pago")] public async Task>> GetMediosDePago() { try { var medios = await _operacionesService.ObtenerMediosDePagoAsync(); return Ok(medios); } catch (Exception ex) { _logger.LogError(ex, "Error al obtener medios de pago"); return StatusCode(500, "Ocurrió un error interno al obtener medios de pago"); } } /// /// Busca una operación por su número de operación /// [HttpGet("{noperacion}")] public async Task>> GetOperacion(string noperacion) { try { var operaciones = await _operacionesService.ObtenerOperacionesPorNumeroAsync(noperacion); if (operaciones == null || !operaciones.Any()) { return NotFound($"No se encontraron operaciones con el número {noperacion}"); } return Ok(operaciones); } catch (Exception ex) { _logger.LogError(ex, "Error al obtener operación {Noperacion}", noperacion); return StatusCode(500, "Ocurrió un error interno al buscar la operación"); } } /// /// Obtiene operaciones realizadas en un rango de fechas /// [HttpGet("buscar")] public async Task>> GetOperacionesPorFecha([FromQuery] DateTime fechaInicio, [FromQuery] DateTime fechaFin) { try { if (fechaInicio > fechaFin) { return BadRequest("La fecha de inicio no puede ser mayor a la fecha de fin."); } var operaciones = await _operacionesService.ObtenerOperacionesPorFechasAsync(fechaInicio, fechaFin); return Ok(operaciones); } catch (Exception ex) { _logger.LogError(ex, "Error al buscar operaciones por fecha"); return StatusCode(500, "Ocurrió un error interno al buscar operaciones."); } } /// /// Registra una nueva operación de pago /// [HttpPost] public async Task CrearOperacion([FromBody] Operacion operacion) { try { if (!ModelState.IsValid) { return BadRequest(ModelState); } // Validar campos mínimos necesarios si es necesario if (string.IsNullOrEmpty(operacion.Noperacion)) { return BadRequest("El número de operación es obligatorio."); } var resultado = await _operacionesService.InsertarOperacionAsync(operacion); if (resultado) { return CreatedAtAction(nameof(GetOperacion), new { noperacion = operacion.Noperacion }, operacion); } return BadRequest("No se pudo registrar la operación."); } catch (Exception ex) { _logger.LogError(ex, "Error al crear operación {Noperacion}", operacion.Noperacion); return StatusCode(500, "Ocurrió un error interno al registrar la operación."); } } }