| | | 1 | | using FAU.Logica.DTOs; |
| | | 2 | | using FAU.Logica.Services; |
| | | 3 | | using Microsoft.AspNetCore.Authorization; |
| | | 4 | | using Microsoft.AspNetCore.Mvc; |
| | | 5 | | using FAU.Entidades; |
| | | 6 | | |
| | | 7 | | namespace FAU.API.Controllers; |
| | | 8 | | |
| | | 9 | | /// <summary> |
| | | 10 | | /// Controlador de auditoría |
| | | 11 | | /// </summary> |
| | | 12 | | [ApiController] |
| | | 13 | | [Route("api/[controller]")] |
| | | 14 | | public class AuditoriaController : ControllerBase |
| | | 15 | | { |
| | | 16 | | private readonly IAuditoriaService _auditoriaService; |
| | | 17 | | private readonly ILogger<AuditoriaController> _logger; |
| | | 18 | | |
| | 2 | 19 | | public AuditoriaController(IAuditoriaService auditoriaService, ILogger<AuditoriaController> logger) |
| | | 20 | | { |
| | 2 | 21 | | _auditoriaService = auditoriaService; |
| | 2 | 22 | | _logger = logger; |
| | 2 | 23 | | } |
| | | 24 | | |
| | | 25 | | /// <summary> |
| | | 26 | | /// Obtiene todas las entradas de la bitácora de auditoría |
| | | 27 | | /// </summary> |
| | | 28 | | [HttpGet("auditoria")] |
| | | 29 | | [Authorize(Roles = "Administrador")] |
| | | 30 | | public async Task<IActionResult> GetAllBitacorasAsync() |
| | | 31 | | { |
| | | 32 | | try |
| | | 33 | | { |
| | 2 | 34 | | var bitacoras = await _auditoriaService.GetAllAsync(); |
| | | 35 | | /* |
| | | 36 | | Seteo el DTO ya que el entity framework tiene referencias circulares con usuario |
| | | 37 | | b1.Usuario -> u1 |
| | | 38 | | u1.BitacorasAuditoria -> [b1, b2, ...] |
| | | 39 | | b1.Usuario apunta otra vez a u1 → ciclo. |
| | | 40 | | Entonces al serializar a JSON estaria en un ciclo infinito. |
| | | 41 | | */ |
| | 1 | 42 | | var dtos = bitacoras.Select(b => new BitacoraDto { |
| | 1 | 43 | | Id = b.Id, |
| | 1 | 44 | | UsuarioNombre = b.Usuario.Username, |
| | 1 | 45 | | AccionNombre = b.Accion.Nombre, |
| | 1 | 46 | | ContextoNombre = b.Contexto.Nombre, |
| | 1 | 47 | | Host = b.Host, |
| | 1 | 48 | | Fecha = b.Fecha |
| | 1 | 49 | | }); |
| | 1 | 50 | | return Ok(ApiResponse<IEnumerable<BitacoraDto>>.SuccessResult(dtos, "Bitácoras obtenidas correctamente")); |
| | | 51 | | } |
| | 1 | 52 | | catch (Exception ex) |
| | | 53 | | { |
| | 1 | 54 | | _logger.LogError(ex, "Error al obtener la auditoría"); |
| | 1 | 55 | | return StatusCode(500, ApiResponse<string>.ErrorResult("Error interno del servidor")); |
| | | 56 | | } |
| | 2 | 57 | | } |
| | | 58 | | } |