| | | 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 GradosController : ControllerBase |
| | | 14 | | { |
| | | 15 | | private readonly IGradoService _gradoService; |
| | | 16 | | private readonly ILogger<GradosController> _logger; |
| | | 17 | | |
| | 7 | 18 | | public GradosController(IGradoService gradoService, ILogger<GradosController> logger) |
| | | 19 | | { |
| | 7 | 20 | | _gradoService = gradoService; |
| | 7 | 21 | | _logger = logger; |
| | 7 | 22 | | } |
| | | 23 | | |
| | | 24 | | /// <summary> |
| | | 25 | | /// Listar todos los grados vigentes |
| | | 26 | | /// GET /api/grados |
| | | 27 | | /// </summary> |
| | | 28 | | [HttpGet] |
| | | 29 | | [ProducesResponseType(typeof(ApiResponse<IEnumerable<GradoDto>>), StatusCodes.Status200OK)] |
| | | 30 | | [ProducesResponseType(typeof(ApiResponse<string>), StatusCodes.Status500InternalServerError)] |
| | | 31 | | public async Task<IActionResult> ObtenerGrados() |
| | | 32 | | { |
| | | 33 | | try |
| | | 34 | | { |
| | 3 | 35 | | var grados = await _gradoService.ObtenerTodosAsync(); |
| | 2 | 36 | | return Ok(ApiResponse<IEnumerable<GradoDto>>.SuccessResult(grados)); |
| | | 37 | | } |
| | 1 | 38 | | catch (Exception ex) |
| | | 39 | | { |
| | 1 | 40 | | _logger.LogError(ex, "Error al obtener grados"); |
| | 1 | 41 | | return StatusCode(500, ApiResponse<string>.ErrorResult("Error interno del servidor")); |
| | | 42 | | } |
| | 3 | 43 | | } |
| | | 44 | | |
| | | 45 | | /// <summary> |
| | | 46 | | /// Crear un nuevo grado |
| | | 47 | | /// POST /api/grados |
| | | 48 | | /// </summary> |
| | | 49 | | [HttpPost] |
| | | 50 | | [Authorize(Roles = "Administrador")] |
| | | 51 | | [ProducesResponseType(typeof(ApiResponse<GradoDto>), 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> CrearGrado([FromBody] CrearGradoRequest request) |
| | | 56 | | { |
| | | 57 | | try |
| | | 58 | | { |
| | 2 | 59 | | var (exito, resultado, error) = await _gradoService.CrearAsync( |
| | 2 | 60 | | request.Codigo, request.Denominacion, request.Orden, |
| | 2 | 61 | | request.EscalafonId, request.EsOficial, request.EsSubalterno, request.Vigente); |
| | | 62 | | |
| | 2 | 63 | | if (!exito) |
| | | 64 | | { |
| | 1 | 65 | | if (error?.Contains("ya existe", StringComparison.OrdinalIgnoreCase) == true) |
| | 0 | 66 | | return Conflict(ApiResponse<string>.ErrorResult(error)); |
| | 1 | 67 | | return BadRequest(ApiResponse<string>.ErrorResult(error ?? "Error al crear grado")); |
| | | 68 | | } |
| | | 69 | | |
| | 1 | 70 | | return CreatedAtAction(nameof(ObtenerGrados), ApiResponse<GradoDto>.SuccessResult(resultado!, "Grado creado |
| | | 71 | | } |
| | 0 | 72 | | catch (Exception ex) |
| | | 73 | | { |
| | 0 | 74 | | _logger.LogError(ex, "Error al crear grado"); |
| | 0 | 75 | | return StatusCode(500, ApiResponse<string>.ErrorResult("Error interno del servidor")); |
| | | 76 | | } |
| | 2 | 77 | | } |
| | | 78 | | |
| | | 79 | | /// <summary> |
| | | 80 | | /// Actualizar un grado existente |
| | | 81 | | /// PUT /api/grados/{id} |
| | | 82 | | /// </summary> |
| | | 83 | | [HttpPut("{id}")] |
| | | 84 | | [Authorize(Roles = "Administrador")] |
| | | 85 | | [ProducesResponseType(typeof(ApiResponse<GradoDto>), StatusCodes.Status200OK)] |
| | | 86 | | [ProducesResponseType(typeof(ApiResponse<string>), StatusCodes.Status400BadRequest)] |
| | | 87 | | [ProducesResponseType(typeof(ApiResponse<string>), StatusCodes.Status404NotFound)] |
| | | 88 | | [ProducesResponseType(typeof(ApiResponse<string>), StatusCodes.Status409Conflict)] |
| | | 89 | | [ProducesResponseType(typeof(ApiResponse<string>), StatusCodes.Status500InternalServerError)] |
| | | 90 | | public async Task<IActionResult> ActualizarGrado(long id, [FromBody] ActualizarGradoRequest request) |
| | | 91 | | { |
| | | 92 | | try |
| | | 93 | | { |
| | 2 | 94 | | var (exito, resultado, error) = await _gradoService.ActualizarAsync( |
| | 2 | 95 | | id, request.Codigo, request.Denominacion, request.Orden, |
| | 2 | 96 | | request.EscalafonId, request.EsOficial, request.EsSubalterno, request.Vigente); |
| | | 97 | | |
| | 2 | 98 | | if (!exito) |
| | | 99 | | { |
| | 1 | 100 | | if (error?.Contains("no encontrado", StringComparison.OrdinalIgnoreCase) == true) |
| | 1 | 101 | | return NotFound(ApiResponse<string>.ErrorResult(error)); |
| | 0 | 102 | | if (error?.Contains("ya existe", StringComparison.OrdinalIgnoreCase) == true) |
| | 0 | 103 | | return Conflict(ApiResponse<string>.ErrorResult(error)); |
| | 0 | 104 | | return BadRequest(ApiResponse<string>.ErrorResult(error ?? "Error al actualizar grado")); |
| | | 105 | | } |
| | | 106 | | |
| | 1 | 107 | | return Ok(ApiResponse<GradoDto>.SuccessResult(resultado!, "Grado actualizado exitosamente")); |
| | | 108 | | } |
| | 0 | 109 | | catch (Exception ex) |
| | | 110 | | { |
| | 0 | 111 | | _logger.LogError(ex, "Error al actualizar grado {Id}", id); |
| | 0 | 112 | | return StatusCode(500, ApiResponse<string>.ErrorResult("Error interno del servidor")); |
| | | 113 | | } |
| | 2 | 114 | | } |
| | | 115 | | } |