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