| | | 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/tabla-permanencia")] |
| | | 11 | | [Authorize] |
| | | 12 | | [Produces("application/json")] |
| | | 13 | | public class TablaPermanenciaController : ControllerBase |
| | | 14 | | { |
| | | 15 | | private readonly ITablaPermanenciaService _service; |
| | | 16 | | private readonly ILogger<TablaPermanenciaController> _logger; |
| | | 17 | | |
| | 12 | 18 | | public TablaPermanenciaController( |
| | 12 | 19 | | ITablaPermanenciaService service, |
| | 12 | 20 | | ILogger<TablaPermanenciaController> logger) |
| | | 21 | | { |
| | 12 | 22 | | _service = service; |
| | 12 | 23 | | _logger = logger; |
| | 12 | 24 | | } |
| | | 25 | | |
| | | 26 | | /// <summary> |
| | | 27 | | /// Listar rangos de permanencia (paginado, filtrable por escalafón y grado). |
| | | 28 | | /// GET /api/tabla-permanencia?page=1&pageSize=10&escalafonId=1&gradoId=2 |
| | | 29 | | /// </summary> |
| | | 30 | | [HttpGet] |
| | | 31 | | [ProducesResponseType(typeof(ApiResponse<PagedResult<TablaPermanenciaDto>>), StatusCodes.Status200OK)] |
| | | 32 | | [ProducesResponseType(typeof(ApiResponse<string>), StatusCodes.Status500InternalServerError)] |
| | | 33 | | public async Task<IActionResult> GetTablaPermanencia( |
| | | 34 | | [FromQuery] int page = 1, |
| | | 35 | | [FromQuery] int pageSize = 10, |
| | | 36 | | [FromQuery] long? escalafonId = null, |
| | | 37 | | [FromQuery] long? gradoId = null) |
| | | 38 | | { |
| | | 39 | | try |
| | | 40 | | { |
| | 1 | 41 | | var result = await _service.GetPagedAsync(page, pageSize, escalafonId, gradoId); |
| | 1 | 42 | | return Ok(ApiResponse<PagedResult<TablaPermanenciaDto>>.SuccessResult(result)); |
| | | 43 | | } |
| | 0 | 44 | | catch (Exception ex) |
| | | 45 | | { |
| | 0 | 46 | | _logger.LogError(ex, "Error al obtener tabla de permanencia"); |
| | 0 | 47 | | return StatusCode(500, ApiResponse<string>.ErrorResult("Error interno del servidor")); |
| | | 48 | | } |
| | 1 | 49 | | } |
| | | 50 | | |
| | | 51 | | /// <summary> |
| | | 52 | | /// Obtener un rango de permanencia por ID. |
| | | 53 | | /// GET /api/tabla-permanencia/{id} |
| | | 54 | | /// </summary> |
| | | 55 | | [HttpGet("{id}")] |
| | | 56 | | [ProducesResponseType(typeof(ApiResponse<TablaPermanenciaDto>), StatusCodes.Status200OK)] |
| | | 57 | | [ProducesResponseType(typeof(ApiResponse<string>), StatusCodes.Status404NotFound)] |
| | | 58 | | [ProducesResponseType(typeof(ApiResponse<string>), StatusCodes.Status500InternalServerError)] |
| | | 59 | | public async Task<IActionResult> GetById(long id) |
| | | 60 | | { |
| | | 61 | | try |
| | | 62 | | { |
| | 2 | 63 | | var item = await _service.GetByIdAsync(id); |
| | 2 | 64 | | if (item == null) |
| | 1 | 65 | | return NotFound(ApiResponse<string>.ErrorResult("Rango de permanencia no encontrado")); |
| | | 66 | | |
| | 1 | 67 | | var dto = new TablaPermanenciaDto |
| | 1 | 68 | | { |
| | 1 | 69 | | Id = item.Id, |
| | 1 | 70 | | EscalafonId = item.EscalafonId, |
| | 1 | 71 | | GradoId = item.GradoId, |
| | 1 | 72 | | MesesDesde = item.MesesDesde, |
| | 1 | 73 | | MesesHasta = item.MesesHasta, |
| | 1 | 74 | | MontoFijo = item.MontoFijo, |
| | 1 | 75 | | Porcentaje = item.Porcentaje, |
| | 1 | 76 | | AplicaRiesgoVuelo = item.AplicaRiesgoVuelo, |
| | 1 | 77 | | VigenteDesde = item.VigenteDesde, |
| | 1 | 78 | | VigenteHasta = item.VigenteHasta, |
| | 1 | 79 | | Activo = item.Activo |
| | 1 | 80 | | }; |
| | 1 | 81 | | return Ok(ApiResponse<TablaPermanenciaDto>.SuccessResult(dto)); |
| | | 82 | | } |
| | 0 | 83 | | catch (Exception ex) |
| | | 84 | | { |
| | 0 | 85 | | _logger.LogError(ex, "Error al obtener tabla de permanencia {Id}", id); |
| | 0 | 86 | | return StatusCode(500, ApiResponse<string>.ErrorResult("Error interno del servidor")); |
| | | 87 | | } |
| | 2 | 88 | | } |
| | | 89 | | |
| | | 90 | | /// <summary> |
| | | 91 | | /// Crear un nuevo rango de permanencia. |
| | | 92 | | /// POST /api/tabla-permanencia |
| | | 93 | | /// </summary> |
| | | 94 | | [HttpPost] |
| | | 95 | | [Authorize(Roles = "Administrador")] |
| | | 96 | | [ProducesResponseType(typeof(ApiResponse<TablaPermanenciaDto>), StatusCodes.Status201Created)] |
| | | 97 | | [ProducesResponseType(typeof(ApiResponse<string>), StatusCodes.Status400BadRequest)] |
| | | 98 | | [ProducesResponseType(typeof(ApiResponse<string>), StatusCodes.Status409Conflict)] |
| | | 99 | | [ProducesResponseType(typeof(ApiResponse<string>), StatusCodes.Status500InternalServerError)] |
| | | 100 | | public async Task<IActionResult> Create([FromBody] CrearTablaPermanenciaRequest request) |
| | | 101 | | { |
| | | 102 | | try |
| | | 103 | | { |
| | 3 | 104 | | var (exito, resultado, error) = await _service.CreateAsync(request); |
| | | 105 | | |
| | 3 | 106 | | if (!exito) |
| | | 107 | | { |
| | 2 | 108 | | if (error?.Contains("solapado", StringComparison.OrdinalIgnoreCase) == true) |
| | 1 | 109 | | return Conflict(ApiResponse<string>.ErrorResult(error)); |
| | 1 | 110 | | return BadRequest(ApiResponse<string>.ErrorResult(error ?? "Error al crear rango de permanencia")); |
| | | 111 | | } |
| | | 112 | | |
| | 1 | 113 | | return CreatedAtAction( |
| | 1 | 114 | | nameof(GetById), |
| | 1 | 115 | | new { id = resultado!.Id }, |
| | 1 | 116 | | ApiResponse<TablaPermanenciaDto>.SuccessResult(null!, "Rango de permanencia creado exitosamente")); |
| | | 117 | | } |
| | 0 | 118 | | catch (Exception ex) |
| | | 119 | | { |
| | 0 | 120 | | _logger.LogError(ex, "Error al crear rango de permanencia"); |
| | 0 | 121 | | return StatusCode(500, ApiResponse<string>.ErrorResult("Error interno del servidor")); |
| | | 122 | | } |
| | 3 | 123 | | } |
| | | 124 | | |
| | | 125 | | /// <summary> |
| | | 126 | | /// Actualizar un rango de permanencia existente. |
| | | 127 | | /// PUT /api/tabla-permanencia/{id} |
| | | 128 | | /// </summary> |
| | | 129 | | [HttpPut("{id}")] |
| | | 130 | | [Authorize(Roles = "Administrador")] |
| | | 131 | | [ProducesResponseType(typeof(ApiResponse<TablaPermanenciaDto>), StatusCodes.Status200OK)] |
| | | 132 | | [ProducesResponseType(typeof(ApiResponse<string>), StatusCodes.Status400BadRequest)] |
| | | 133 | | [ProducesResponseType(typeof(ApiResponse<string>), StatusCodes.Status404NotFound)] |
| | | 134 | | [ProducesResponseType(typeof(ApiResponse<string>), StatusCodes.Status409Conflict)] |
| | | 135 | | [ProducesResponseType(typeof(ApiResponse<string>), StatusCodes.Status500InternalServerError)] |
| | | 136 | | public async Task<IActionResult> Update(long id, [FromBody] ActualizarTablaPermanenciaRequest request) |
| | | 137 | | { |
| | | 138 | | try |
| | | 139 | | { |
| | 4 | 140 | | var (exito, resultado, error) = await _service.UpdateAsync(id, request); |
| | | 141 | | |
| | 4 | 142 | | if (!exito) |
| | | 143 | | { |
| | 3 | 144 | | if (error?.Contains("no encontrado", StringComparison.OrdinalIgnoreCase) == true) |
| | 1 | 145 | | return NotFound(ApiResponse<string>.ErrorResult(error)); |
| | 2 | 146 | | if (error?.Contains("solapado", StringComparison.OrdinalIgnoreCase) == true) |
| | 1 | 147 | | return Conflict(ApiResponse<string>.ErrorResult(error)); |
| | 1 | 148 | | return BadRequest(ApiResponse<string>.ErrorResult(error ?? "Error al actualizar rango de permanencia")); |
| | | 149 | | } |
| | | 150 | | |
| | 1 | 151 | | return Ok(ApiResponse<TablaPermanenciaDto>.SuccessResult(null!, "Rango de permanencia actualizado exitosamen |
| | | 152 | | } |
| | 0 | 153 | | catch (Exception ex) |
| | | 154 | | { |
| | 0 | 155 | | _logger.LogError(ex, "Error al actualizar rango de permanencia {Id}", id); |
| | 0 | 156 | | return StatusCode(500, ApiResponse<string>.ErrorResult("Error interno del servidor")); |
| | | 157 | | } |
| | 4 | 158 | | } |
| | | 159 | | |
| | | 160 | | /// <summary> |
| | | 161 | | /// Eliminar (soft delete) un rango de permanencia. |
| | | 162 | | /// DELETE /api/tabla-permanencia/{id} |
| | | 163 | | /// </summary> |
| | | 164 | | [HttpDelete("{id}")] |
| | | 165 | | [Authorize(Roles = "Administrador")] |
| | | 166 | | [ProducesResponseType(typeof(ApiResponse<string>), StatusCodes.Status200OK)] |
| | | 167 | | [ProducesResponseType(typeof(ApiResponse<string>), StatusCodes.Status404NotFound)] |
| | | 168 | | [ProducesResponseType(typeof(ApiResponse<string>), StatusCodes.Status500InternalServerError)] |
| | | 169 | | public async Task<IActionResult> Delete(long id) |
| | | 170 | | { |
| | | 171 | | try |
| | | 172 | | { |
| | 2 | 173 | | var existe = await _service.GetByIdAsync(id); |
| | 2 | 174 | | if (existe == null) |
| | 1 | 175 | | return NotFound(ApiResponse<string>.ErrorResult("Rango de permanencia no encontrado")); |
| | | 176 | | |
| | 1 | 177 | | await _service.DeleteAsync(id); |
| | 1 | 178 | | return Ok(ApiResponse<string>.SuccessResult("Rango de permanencia eliminado exitosamente")); |
| | | 179 | | } |
| | 0 | 180 | | catch (Exception ex) |
| | | 181 | | { |
| | 0 | 182 | | _logger.LogError(ex, "Error al eliminar rango de permanencia {Id}", id); |
| | 0 | 183 | | return StatusCode(500, ApiResponse<string>.ErrorResult("Error interno del servidor")); |
| | | 184 | | } |
| | 2 | 185 | | } |
| | | 186 | | } |