| | | 1 | | using FAU.Entidades; |
| | | 2 | | using FAU.Logica.DTOs; |
| | | 3 | | using FAU.Logica.Services; |
| | | 4 | | using Microsoft.AspNetCore.Authorization; |
| | | 5 | | using Microsoft.AspNetCore.Mvc; |
| | | 6 | | |
| | | 7 | | namespace FAU.API.Controllers; |
| | | 8 | | |
| | | 9 | | [ApiController] |
| | | 10 | | [Route("api/novedades-periodo")] |
| | | 11 | | [Authorize(Roles = "Administrador,Operador")] |
| | | 12 | | public class NovedadesPeriodoController : ControllerBase |
| | | 13 | | { |
| | | 14 | | private readonly INovedadPeriodoService _novedadPeriodoService; |
| | | 15 | | private readonly ILogger<NovedadesPeriodoController> _logger; |
| | | 16 | | |
| | 4 | 17 | | public NovedadesPeriodoController( |
| | 4 | 18 | | INovedadPeriodoService novedadPeriodoService, |
| | 4 | 19 | | ILogger<NovedadesPeriodoController> logger) |
| | | 20 | | { |
| | 4 | 21 | | _novedadPeriodoService = novedadPeriodoService; |
| | 4 | 22 | | _logger = logger; |
| | 4 | 23 | | } |
| | | 24 | | |
| | | 25 | | [HttpGet] |
| | | 26 | | public async Task<IActionResult> GetNovedades( |
| | | 27 | | [FromQuery] int page = 1, |
| | | 28 | | [FromQuery] int pageSize = 10, |
| | | 29 | | [FromQuery] long? periodoId = null, |
| | | 30 | | [FromQuery] long? personaId = null, |
| | | 31 | | [FromQuery] string? tipo = null, |
| | | 32 | | [FromQuery] string? estado = null) |
| | | 33 | | { |
| | | 34 | | try |
| | | 35 | | { |
| | 0 | 36 | | var resultado = await _novedadPeriodoService.GetPaginatedAsync(page, pageSize, periodoId, personaId, tipo, e |
| | | 37 | | |
| | 0 | 38 | | return Ok(ApiResponse<PagedResult<NovedadPeriodoDto>>.SuccessResult(resultado)); |
| | | 39 | | } |
| | 0 | 40 | | catch (Exception ex) |
| | | 41 | | { |
| | 0 | 42 | | _logger.LogError(ex, "Error al obtener novedades de período"); |
| | 0 | 43 | | return StatusCode(500, ApiResponse<string>.ErrorResult("Error interno del servidor")); |
| | | 44 | | } |
| | 0 | 45 | | } |
| | | 46 | | |
| | | 47 | | [HttpGet("{id}")] |
| | | 48 | | public async Task<IActionResult> GetNovedad(long id) |
| | | 49 | | { |
| | | 50 | | try |
| | | 51 | | { |
| | 0 | 52 | | var novedad = await _novedadPeriodoService.GetByIdAsync(id); |
| | 0 | 53 | | if (novedad == null) |
| | | 54 | | { |
| | 0 | 55 | | return NotFound(ApiResponse<string>.ErrorResult("Novedad de período no encontrada")); |
| | | 56 | | } |
| | | 57 | | |
| | 0 | 58 | | var response = new NovedadPeriodoDto |
| | 0 | 59 | | { |
| | 0 | 60 | | Id = novedad.Id, |
| | 0 | 61 | | PeriodoId = novedad.PeriodoId, |
| | 0 | 62 | | PersonaId = novedad.PersonaId, |
| | 0 | 63 | | Tipo = novedad.Tipo, |
| | 0 | 64 | | DiasAfectados = novedad.DiasAfectados, |
| | 0 | 65 | | PorcentajeSubsidio = novedad.PorcentajeSubsidio, |
| | 0 | 66 | | FechaDesde = novedad.FechaDesde ?? DateTime.MinValue, |
| | 0 | 67 | | Observaciones = novedad.Observaciones, |
| | 0 | 68 | | Estado = novedad.Estado, |
| | 0 | 69 | | CreadoPor = novedad.CreadoPor, |
| | 0 | 70 | | CreadoEn = novedad.CreadoEn, |
| | 0 | 71 | | ConfirmadoPor = novedad.ConfirmadoPor, |
| | 0 | 72 | | ConfirmadoEn = novedad.ConfirmadoEn |
| | 0 | 73 | | }; |
| | | 74 | | |
| | 0 | 75 | | return Ok(ApiResponse<NovedadPeriodoDto>.SuccessResult(response)); |
| | | 76 | | } |
| | 0 | 77 | | catch (Exception ex) |
| | | 78 | | { |
| | 0 | 79 | | _logger.LogError(ex, "Error al obtener novedad de período {Id}", id); |
| | 0 | 80 | | return StatusCode(500, ApiResponse<string>.ErrorResult("Error interno del servidor")); |
| | | 81 | | } |
| | 0 | 82 | | } |
| | | 83 | | |
| | | 84 | | [HttpPost] |
| | | 85 | | public async Task<IActionResult> CreateNovedad([FromBody] CrearNovedadPeriodoRequest request) |
| | | 86 | | { |
| | | 87 | | try |
| | | 88 | | { |
| | 2 | 89 | | var (success, novedad, error) = await _novedadPeriodoService.CreateAsync(request); |
| | 2 | 90 | | if (!success) |
| | | 91 | | { |
| | 1 | 92 | | if (error?.Contains("ya existe", StringComparison.OrdinalIgnoreCase) == true) |
| | | 93 | | { |
| | 1 | 94 | | return Conflict(ApiResponse<string>.ErrorResult(error)); |
| | | 95 | | } |
| | 0 | 96 | | if (error?.Contains("no encontrado", StringComparison.OrdinalIgnoreCase) == true || |
| | 0 | 97 | | error?.Contains("no existe", StringComparison.OrdinalIgnoreCase) == true) |
| | | 98 | | { |
| | 0 | 99 | | return NotFound(ApiResponse<string>.ErrorResult(error)); |
| | | 100 | | } |
| | | 101 | | |
| | 0 | 102 | | return BadRequest(ApiResponse<string>.ErrorResult(error ?? "Error al crear la novedad de período")); |
| | | 103 | | } |
| | | 104 | | |
| | 1 | 105 | | var response = new { id = novedad!.Id, creado = true }; |
| | 1 | 106 | | return CreatedAtAction(nameof(GetNovedad), new { id = novedad.Id }, ApiResponse<object>.SuccessResult(respon |
| | | 107 | | } |
| | 0 | 108 | | catch (Exception ex) |
| | | 109 | | { |
| | 0 | 110 | | _logger.LogError(ex, "Error al crear novedad de período"); |
| | 0 | 111 | | return StatusCode(500, ApiResponse<string>.ErrorResult("Error interno del servidor")); |
| | | 112 | | } |
| | 2 | 113 | | } |
| | | 114 | | |
| | | 115 | | [HttpPut("{id}")] |
| | | 116 | | public async Task<IActionResult> UpdateNovedad(long id, [FromBody] ActualizarNovedadPeriodoRequest request) |
| | | 117 | | { |
| | | 118 | | try |
| | | 119 | | { |
| | 0 | 120 | | var (success, novedad, error) = await _novedadPeriodoService.UpdateAsync(id, request); |
| | 0 | 121 | | if (!success) |
| | | 122 | | { |
| | 0 | 123 | | if (error?.Contains("no encontrada", StringComparison.OrdinalIgnoreCase) == true) |
| | | 124 | | { |
| | 0 | 125 | | return NotFound(ApiResponse<string>.ErrorResult(error)); |
| | | 126 | | } |
| | 0 | 127 | | if (error?.Contains("solo se pueden", StringComparison.OrdinalIgnoreCase) == true || |
| | 0 | 128 | | error?.Contains("debe proporcionar", StringComparison.OrdinalIgnoreCase) == true) |
| | | 129 | | { |
| | 0 | 130 | | return BadRequest(ApiResponse<string>.ErrorResult(error)); |
| | | 131 | | } |
| | | 132 | | |
| | 0 | 133 | | return Conflict(ApiResponse<string>.ErrorResult(error)); |
| | | 134 | | } |
| | | 135 | | |
| | 0 | 136 | | return Ok(ApiResponse<object>.SuccessResult(new { actualizado = true }, "Novedad de período actualizada exit |
| | | 137 | | } |
| | 0 | 138 | | catch (Exception ex) |
| | | 139 | | { |
| | 0 | 140 | | _logger.LogError(ex, "Error al actualizar novedad de período {Id}", id); |
| | 0 | 141 | | return StatusCode(500, ApiResponse<string>.ErrorResult("Error interno del servidor")); |
| | | 142 | | } |
| | 0 | 143 | | } |
| | | 144 | | |
| | | 145 | | [HttpDelete("{id}")] |
| | | 146 | | public async Task<IActionResult> DeleteNovedad(long id) |
| | | 147 | | { |
| | | 148 | | try |
| | | 149 | | { |
| | 0 | 150 | | var (success, novedad, error) = await _novedadPeriodoService.DeleteAsync(id); |
| | 0 | 151 | | if (!success) |
| | | 152 | | { |
| | 0 | 153 | | if (error?.Contains("no encontrada", StringComparison.OrdinalIgnoreCase) == true) |
| | | 154 | | { |
| | 0 | 155 | | return NotFound(ApiResponse<string>.ErrorResult(error)); |
| | | 156 | | } |
| | 0 | 157 | | return Conflict(ApiResponse<string>.ErrorResult(error ?? "No se pudo eliminar la novedad de período")); |
| | | 158 | | } |
| | | 159 | | |
| | 0 | 160 | | return NoContent(); |
| | | 161 | | } |
| | 0 | 162 | | catch (Exception ex) |
| | | 163 | | { |
| | 0 | 164 | | _logger.LogError(ex, "Error al eliminar novedad de período {Id}", id); |
| | 0 | 165 | | return StatusCode(500, ApiResponse<string>.ErrorResult("Error interno del servidor")); |
| | | 166 | | } |
| | 0 | 167 | | } |
| | | 168 | | |
| | | 169 | | [HttpPost("{id}/confirmar")] |
| | | 170 | | public async Task<IActionResult> ConfirmarNovedad(long id) |
| | | 171 | | { |
| | | 172 | | try |
| | | 173 | | { |
| | 1 | 174 | | var (success, novedad, error) = await _novedadPeriodoService.ChangeStateToConfirmedAsync(id); |
| | 1 | 175 | | if (!success) |
| | | 176 | | { |
| | 0 | 177 | | if (error?.Contains("no encontrada", StringComparison.OrdinalIgnoreCase) == true) |
| | | 178 | | { |
| | 0 | 179 | | return NotFound(ApiResponse<string>.ErrorResult(error)); |
| | | 180 | | } |
| | 0 | 181 | | return Conflict(ApiResponse<string>.ErrorResult(error ?? "No se pudo confirmar la novedad de período")); |
| | | 182 | | } |
| | | 183 | | |
| | 1 | 184 | | return Ok(ApiResponse<object>.SuccessResult(new { confirmado = true }, "Novedad de período confirmada exitos |
| | | 185 | | } |
| | 0 | 186 | | catch (Exception ex) |
| | | 187 | | { |
| | 0 | 188 | | _logger.LogError(ex, "Error al confirmar novedad de período {Id}", id); |
| | 0 | 189 | | return StatusCode(500, ApiResponse<string>.ErrorResult("Error interno del servidor")); |
| | | 190 | | } |
| | 1 | 191 | | } |
| | | 192 | | |
| | | 193 | | [HttpPost("{id}/aplicar")] |
| | | 194 | | public async Task<IActionResult> AplicarNovedad(long id) |
| | | 195 | | { |
| | | 196 | | try |
| | | 197 | | { |
| | 1 | 198 | | var (success, novedad, error) = await _novedadPeriodoService.ChangeStateToAppliedAsync(id); |
| | 1 | 199 | | if (!success) |
| | | 200 | | { |
| | 1 | 201 | | if (error?.Contains("no encontrada", StringComparison.OrdinalIgnoreCase) == true) |
| | | 202 | | { |
| | 0 | 203 | | return NotFound(ApiResponse<string>.ErrorResult(error)); |
| | | 204 | | } |
| | 1 | 205 | | return Conflict(ApiResponse<string>.ErrorResult(error ?? "No se pudo aplicar la novedad de período")); |
| | | 206 | | } |
| | | 207 | | |
| | 0 | 208 | | return Ok(ApiResponse<object>.SuccessResult(new { aplicada = true }, "Novedad de período aplicada exitosamen |
| | | 209 | | } |
| | 0 | 210 | | catch (Exception ex) |
| | | 211 | | { |
| | 0 | 212 | | _logger.LogError(ex, "Error al aplicar novedad de período {Id}", id); |
| | 0 | 213 | | return StatusCode(500, ApiResponse<string>.ErrorResult("Error interno del servidor")); |
| | | 214 | | } |
| | 1 | 215 | | } |
| | | 216 | | } |