| | | 1 | | using System.Security.Claims; |
| | | 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/beneficios-sociales/{beneficioId:long}/beneficiarios")] |
| | | 11 | | [Authorize(Roles = "Administrador,Supervisor")] |
| | | 12 | | public class BeneficiosSocialesBeneficiariosController : ControllerBase |
| | | 13 | | { |
| | | 14 | | private readonly IBeneficioSocialBeneficiarioService _service; |
| | | 15 | | private readonly ILogger<BeneficiosSocialesBeneficiariosController> _logger; |
| | | 16 | | |
| | 0 | 17 | | public BeneficiosSocialesBeneficiariosController( |
| | 0 | 18 | | IBeneficioSocialBeneficiarioService service, |
| | 0 | 19 | | ILogger<BeneficiosSocialesBeneficiariosController> logger) |
| | | 20 | | { |
| | 0 | 21 | | _service = service; |
| | 0 | 22 | | _logger = logger; |
| | 0 | 23 | | } |
| | | 24 | | |
| | | 25 | | [HttpGet] |
| | | 26 | | public async Task<IActionResult> Listar(long beneficioId, [FromQuery] bool soloActivos = true) |
| | | 27 | | { |
| | | 28 | | try |
| | | 29 | | { |
| | 0 | 30 | | var items = await _service.ObtenerPorBeneficioAsync(beneficioId, soloActivos); |
| | 0 | 31 | | return Ok(ApiResponse<object>.SuccessResult(items)); |
| | | 32 | | } |
| | 0 | 33 | | catch (Exception ex) |
| | | 34 | | { |
| | 0 | 35 | | _logger.LogError(ex, "Error al listar beneficiarios del beneficio {BeneficioId}", beneficioId); |
| | 0 | 36 | | return StatusCode(500, ApiResponse<string>.ErrorResult("Error interno del servidor")); |
| | | 37 | | } |
| | 0 | 38 | | } |
| | | 39 | | |
| | | 40 | | [HttpGet("{id:long}")] |
| | | 41 | | public async Task<IActionResult> Obtener(long beneficioId, long id) |
| | | 42 | | { |
| | | 43 | | try |
| | | 44 | | { |
| | 0 | 45 | | var item = await _service.ObtenerPorIdAsync(id); |
| | 0 | 46 | | if (item is null || item.BeneficioId != beneficioId) |
| | 0 | 47 | | return NotFound(ApiResponse<string>.ErrorResult("Beneficiario no encontrado")); |
| | 0 | 48 | | return Ok(ApiResponse<object>.SuccessResult(item)); |
| | | 49 | | } |
| | 0 | 50 | | catch (Exception ex) |
| | | 51 | | { |
| | 0 | 52 | | _logger.LogError(ex, "Error al obtener beneficiario {Id}", id); |
| | 0 | 53 | | return StatusCode(500, ApiResponse<string>.ErrorResult("Error interno del servidor")); |
| | | 54 | | } |
| | 0 | 55 | | } |
| | | 56 | | |
| | | 57 | | [HttpPost] |
| | | 58 | | public async Task<IActionResult> Agregar(long beneficioId, [FromBody] CrearBeneficiarioSocialRequest request) |
| | | 59 | | { |
| | 0 | 60 | | if (!ModelState.IsValid) |
| | 0 | 61 | | return BadRequest(ApiResponse<string>.ErrorResult("Datos inválidos")); |
| | | 62 | | |
| | | 63 | | try |
| | | 64 | | { |
| | 0 | 65 | | var usuarioId = ObtenerUsuarioId(); |
| | 0 | 66 | | var creado = await _service.AgregarAsync(beneficioId, request, usuarioId); |
| | 0 | 67 | | return CreatedAtAction(nameof(Obtener), |
| | 0 | 68 | | new { beneficioId, id = creado.Id }, |
| | 0 | 69 | | ApiResponse<object>.SuccessResult(creado, "Beneficiario agregado correctamente")); |
| | | 70 | | } |
| | 0 | 71 | | catch (ArgumentException ex) |
| | | 72 | | { |
| | 0 | 73 | | return BadRequest(ApiResponse<string>.ErrorResult(ex.Message)); |
| | | 74 | | } |
| | 0 | 75 | | catch (InvalidOperationException ex) |
| | | 76 | | { |
| | 0 | 77 | | return Conflict(ApiResponse<string>.ErrorResult(ex.Message)); |
| | | 78 | | } |
| | 0 | 79 | | catch (Exception ex) |
| | | 80 | | { |
| | 0 | 81 | | _logger.LogError(ex, "Error al agregar beneficiario al beneficio {BeneficioId}", beneficioId); |
| | 0 | 82 | | return StatusCode(500, ApiResponse<string>.ErrorResult("Error interno del servidor")); |
| | | 83 | | } |
| | 0 | 84 | | } |
| | | 85 | | |
| | | 86 | | [HttpPut("{id:long}")] |
| | | 87 | | public async Task<IActionResult> Actualizar( |
| | | 88 | | long beneficioId, long id, [FromBody] ActualizarBeneficiarioSocialRequest request) |
| | | 89 | | { |
| | | 90 | | try |
| | | 91 | | { |
| | 0 | 92 | | var item = await _service.ObtenerPorIdAsync(id); |
| | 0 | 93 | | if (item is null || item.BeneficioId != beneficioId) |
| | 0 | 94 | | return NotFound(ApiResponse<string>.ErrorResult("Beneficiario no encontrado")); |
| | | 95 | | |
| | 0 | 96 | | var actualizado = await _service.ActualizarAsync(id, request); |
| | 0 | 97 | | return Ok(ApiResponse<object>.SuccessResult(actualizado, "Beneficiario actualizado correctamente")); |
| | | 98 | | } |
| | 0 | 99 | | catch (ArgumentException ex) |
| | | 100 | | { |
| | 0 | 101 | | return BadRequest(ApiResponse<string>.ErrorResult(ex.Message)); |
| | | 102 | | } |
| | 0 | 103 | | catch (Exception ex) |
| | | 104 | | { |
| | 0 | 105 | | _logger.LogError(ex, "Error al actualizar beneficiario {Id}", id); |
| | 0 | 106 | | return StatusCode(500, ApiResponse<string>.ErrorResult("Error interno del servidor")); |
| | | 107 | | } |
| | 0 | 108 | | } |
| | | 109 | | |
| | | 110 | | [HttpDelete("{id:long}")] |
| | | 111 | | public async Task<IActionResult> Eliminar(long beneficioId, long id) |
| | | 112 | | { |
| | | 113 | | try |
| | | 114 | | { |
| | 0 | 115 | | var item = await _service.ObtenerPorIdAsync(id); |
| | 0 | 116 | | if (item is null || item.BeneficioId != beneficioId) |
| | 0 | 117 | | return NotFound(ApiResponse<string>.ErrorResult("Beneficiario no encontrado")); |
| | | 118 | | |
| | 0 | 119 | | await _service.EliminarAsync(id); |
| | 0 | 120 | | return Ok(ApiResponse<string>.SuccessResult("ok", "Beneficiario eliminado correctamente")); |
| | | 121 | | } |
| | 0 | 122 | | catch (ArgumentException ex) |
| | | 123 | | { |
| | 0 | 124 | | return BadRequest(ApiResponse<string>.ErrorResult(ex.Message)); |
| | | 125 | | } |
| | 0 | 126 | | catch (Exception ex) |
| | | 127 | | { |
| | 0 | 128 | | _logger.LogError(ex, "Error al eliminar beneficiario {Id}", id); |
| | 0 | 129 | | return StatusCode(500, ApiResponse<string>.ErrorResult("Error interno del servidor")); |
| | | 130 | | } |
| | 0 | 131 | | } |
| | | 132 | | |
| | | 133 | | [HttpPost("{id:long}/documentos")] |
| | | 134 | | public async Task<IActionResult> SubirDocumento(long beneficioId, long id, IFormFile archivo) |
| | | 135 | | { |
| | 0 | 136 | | if (archivo is null || archivo.Length == 0) |
| | 0 | 137 | | return BadRequest(ApiResponse<string>.ErrorResult("Debe adjuntar un archivo")); |
| | | 138 | | |
| | | 139 | | try |
| | | 140 | | { |
| | 0 | 141 | | var item = await _service.ObtenerPorIdAsync(id); |
| | 0 | 142 | | if (item is null || item.BeneficioId != beneficioId) |
| | 0 | 143 | | return NotFound(ApiResponse<string>.ErrorResult("Beneficiario no encontrado")); |
| | | 144 | | |
| | 0 | 145 | | var usuarioId = ObtenerUsuarioId(); |
| | 0 | 146 | | using var stream = archivo.OpenReadStream(); |
| | 0 | 147 | | var ruta = await _service.SubirDocumentoAsync(id, stream, archivo.FileName, archivo.ContentType, usuarioId); |
| | 0 | 148 | | return Ok(ApiResponse<object>.SuccessResult(new { ruta }, "Documento subido correctamente")); |
| | | 149 | | } |
| | 0 | 150 | | catch (ArgumentException ex) |
| | | 151 | | { |
| | 0 | 152 | | return BadRequest(ApiResponse<string>.ErrorResult(ex.Message)); |
| | | 153 | | } |
| | 0 | 154 | | catch (Exception ex) |
| | | 155 | | { |
| | 0 | 156 | | _logger.LogError(ex, "Error al subir documento para beneficiario {Id}", id); |
| | 0 | 157 | | return StatusCode(500, ApiResponse<string>.ErrorResult("Error interno del servidor")); |
| | | 158 | | } |
| | 0 | 159 | | } |
| | | 160 | | |
| | | 161 | | [HttpGet("{id:long}/documentos")] |
| | | 162 | | public async Task<IActionResult> ListarDocumentos(long beneficioId, long id) |
| | | 163 | | { |
| | | 164 | | try |
| | | 165 | | { |
| | 0 | 166 | | var item = await _service.ObtenerPorIdAsync(id); |
| | 0 | 167 | | if (item is null || item.BeneficioId != beneficioId) |
| | 0 | 168 | | return NotFound(ApiResponse<string>.ErrorResult("Beneficiario no encontrado")); |
| | | 169 | | |
| | 0 | 170 | | var docs = await _service.ObtenerDocumentosAsync(id); |
| | 0 | 171 | | return Ok(ApiResponse<object>.SuccessResult(docs)); |
| | | 172 | | } |
| | 0 | 173 | | catch (Exception ex) |
| | | 174 | | { |
| | 0 | 175 | | _logger.LogError(ex, "Error al listar documentos del beneficiario {Id}", id); |
| | 0 | 176 | | return StatusCode(500, ApiResponse<string>.ErrorResult("Error interno del servidor")); |
| | | 177 | | } |
| | 0 | 178 | | } |
| | | 179 | | |
| | | 180 | | [HttpDelete("{id:long}/documentos/{docId:long}")] |
| | | 181 | | public async Task<IActionResult> EliminarDocumento(long beneficioId, long id, long docId) |
| | | 182 | | { |
| | | 183 | | try |
| | | 184 | | { |
| | 0 | 185 | | var item = await _service.ObtenerPorIdAsync(id); |
| | 0 | 186 | | if (item is null || item.BeneficioId != beneficioId) |
| | 0 | 187 | | return NotFound(ApiResponse<string>.ErrorResult("Beneficiario no encontrado")); |
| | | 188 | | |
| | 0 | 189 | | await _service.EliminarDocumentoAsync(id, docId); |
| | 0 | 190 | | return Ok(ApiResponse<string>.SuccessResult("ok", "Documento eliminado correctamente")); |
| | | 191 | | } |
| | 0 | 192 | | catch (KeyNotFoundException) |
| | | 193 | | { |
| | 0 | 194 | | return NotFound(ApiResponse<string>.ErrorResult("Documento no encontrado")); |
| | | 195 | | } |
| | 0 | 196 | | catch (Exception ex) |
| | | 197 | | { |
| | 0 | 198 | | _logger.LogError(ex, "Error al eliminar documento {DocId} del beneficiario {Id}", docId, id); |
| | 0 | 199 | | return StatusCode(500, ApiResponse<string>.ErrorResult("Error interno del servidor")); |
| | | 200 | | } |
| | 0 | 201 | | } |
| | | 202 | | |
| | | 203 | | [HttpGet("{id:long}/documentos/{docId:long}")] |
| | | 204 | | public async Task<IActionResult> DescargarDocumento( |
| | | 205 | | long beneficioId, long id, long docId, [FromQuery] bool inline = false) |
| | | 206 | | { |
| | | 207 | | try |
| | | 208 | | { |
| | 0 | 209 | | var item = await _service.ObtenerPorIdAsync(id); |
| | 0 | 210 | | if (item is null || item.BeneficioId != beneficioId) |
| | 0 | 211 | | return NotFound(ApiResponse<string>.ErrorResult("Beneficiario no encontrado")); |
| | | 212 | | |
| | 0 | 213 | | var (stream, nombre, contentType) = await _service.DescargarDocumentoAsync(id, docId); |
| | 0 | 214 | | var disposition = inline ? "inline" : "attachment"; |
| | 0 | 215 | | Response.Headers.Append("Content-Disposition", $"{disposition}; filename=\"{nombre}\""); |
| | 0 | 216 | | return File(stream, contentType); |
| | | 217 | | } |
| | 0 | 218 | | catch (KeyNotFoundException) |
| | | 219 | | { |
| | 0 | 220 | | return NotFound(ApiResponse<string>.ErrorResult("Documento no encontrado")); |
| | | 221 | | } |
| | 0 | 222 | | catch (Exception ex) |
| | | 223 | | { |
| | 0 | 224 | | _logger.LogError(ex, "Error al descargar documento {DocId} del beneficiario {Id}", docId, id); |
| | 0 | 225 | | return StatusCode(500, ApiResponse<string>.ErrorResult("Error interno del servidor")); |
| | | 226 | | } |
| | 0 | 227 | | } |
| | | 228 | | |
| | | 229 | | private long ObtenerUsuarioId() |
| | | 230 | | { |
| | 0 | 231 | | var claim = User.FindFirstValue(ClaimTypes.NameIdentifier); |
| | 0 | 232 | | return long.TryParse(claim, out var id) ? id : 0; |
| | | 233 | | } |
| | | 234 | | } |