| | | 1 | | using FAU.Logica.DTOs; |
| | | 2 | | using FAU.Logica.Services; |
| | | 3 | | using Microsoft.AspNetCore.Authorization; |
| | | 4 | | using Microsoft.AspNetCore.Mvc; |
| | | 5 | | |
| | | 6 | | namespace FAU.API.Controllers; |
| | | 7 | | |
| | | 8 | | [ApiController] |
| | | 9 | | [Route("api/[controller]")] |
| | | 10 | | [Authorize] |
| | | 11 | | public class ProgramasController : ControllerBase |
| | | 12 | | { |
| | | 13 | | private readonly IProgramaService _programaService; |
| | | 14 | | private readonly ILogger<ProgramasController> _logger; |
| | | 15 | | |
| | 7 | 16 | | public ProgramasController(IProgramaService programaService, ILogger<ProgramasController> logger) |
| | | 17 | | { |
| | 7 | 18 | | _programaService = programaService; |
| | 7 | 19 | | _logger = logger; |
| | 7 | 20 | | } |
| | | 21 | | |
| | | 22 | | /// <summary> |
| | | 23 | | /// Listar todos los programas vigentes |
| | | 24 | | /// GET /api/programas |
| | | 25 | | /// </summary> |
| | | 26 | | [HttpGet] |
| | | 27 | | public async Task<IActionResult> ObtenerProgramas() |
| | | 28 | | { |
| | | 29 | | try |
| | | 30 | | { |
| | 3 | 31 | | var programas = await _programaService.ObtenerTodosAsync(); |
| | 2 | 32 | | return Ok(ApiResponse<IEnumerable<ProgramaDto>>.SuccessResult(programas)); |
| | | 33 | | } |
| | 1 | 34 | | catch (Exception ex) |
| | | 35 | | { |
| | 1 | 36 | | _logger.LogError(ex, "Error al obtener programas"); |
| | 1 | 37 | | return StatusCode(500, ApiResponse<string>.ErrorResult("Error interno del servidor")); |
| | | 38 | | } |
| | 3 | 39 | | } |
| | | 40 | | |
| | | 41 | | /// <summary> |
| | | 42 | | /// Crear un nuevo programa |
| | | 43 | | /// POST /api/programas |
| | | 44 | | /// </summary> |
| | | 45 | | [HttpPost] |
| | | 46 | | [Authorize(Roles = "Administrador")] |
| | | 47 | | public async Task<IActionResult> CrearPrograma([FromBody] CrearProgramaRequest request) |
| | | 48 | | { |
| | | 49 | | try |
| | | 50 | | { |
| | 2 | 51 | | var (exito, resultado, error) = await _programaService.CrearAsync(request.Codigo, request.Denominacion, requ |
| | | 52 | | |
| | 2 | 53 | | if (!exito) |
| | | 54 | | { |
| | 1 | 55 | | if (error?.Contains("ya existe", StringComparison.OrdinalIgnoreCase) == true) |
| | 1 | 56 | | return Conflict(ApiResponse<string>.ErrorResult(error)); |
| | 0 | 57 | | return BadRequest(ApiResponse<string>.ErrorResult(error ?? "Error al crear programa")); |
| | | 58 | | } |
| | | 59 | | |
| | 1 | 60 | | return CreatedAtAction(nameof(ObtenerProgramas), ApiResponse<ProgramaDto>.SuccessResult(resultado!, "Program |
| | | 61 | | } |
| | 0 | 62 | | catch (Exception ex) |
| | | 63 | | { |
| | 0 | 64 | | _logger.LogError(ex, "Error al crear programa"); |
| | 0 | 65 | | return StatusCode(500, ApiResponse<string>.ErrorResult("Error interno del servidor")); |
| | | 66 | | } |
| | 2 | 67 | | } |
| | | 68 | | |
| | | 69 | | /// <summary> |
| | | 70 | | /// Actualizar un programa existente |
| | | 71 | | /// PUT /api/programas/{id} |
| | | 72 | | /// </summary> |
| | | 73 | | [HttpPut("{id}")] |
| | | 74 | | [Authorize(Roles = "Administrador")] |
| | | 75 | | public async Task<IActionResult> ActualizarPrograma(long id, [FromBody] ActualizarProgramaRequest request) |
| | | 76 | | { |
| | | 77 | | try |
| | | 78 | | { |
| | 2 | 79 | | var (exito, resultado, error) = await _programaService.ActualizarAsync(id, request.Codigo, request.Denominac |
| | | 80 | | |
| | 2 | 81 | | if (!exito) |
| | | 82 | | { |
| | 1 | 83 | | if (error?.Contains("no encontrado", StringComparison.OrdinalIgnoreCase) == true) |
| | 1 | 84 | | return NotFound(ApiResponse<string>.ErrorResult(error)); |
| | 0 | 85 | | if (error?.Contains("ya existe", StringComparison.OrdinalIgnoreCase) == true) |
| | 0 | 86 | | return Conflict(ApiResponse<string>.ErrorResult(error)); |
| | 0 | 87 | | return BadRequest(ApiResponse<string>.ErrorResult(error ?? "Error al actualizar programa")); |
| | | 88 | | } |
| | | 89 | | |
| | 1 | 90 | | return Ok(ApiResponse<ProgramaDto>.SuccessResult(resultado!, "Programa actualizado exitosamente")); |
| | | 91 | | } |
| | 0 | 92 | | catch (Exception ex) |
| | | 93 | | { |
| | 0 | 94 | | _logger.LogError(ex, "Error al actualizar programa {Id}", id); |
| | 0 | 95 | | return StatusCode(500, ApiResponse<string>.ErrorResult("Error interno del servidor")); |
| | | 96 | | } |
| | 2 | 97 | | } |
| | | 98 | | } |