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