| | | 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 la bitácora de auditoría con paginación y filtros opcionales. |
| | | 27 | | /// GET /api/auditoria/auditoria?page=1&pageSize=50&contexto=Bancos&accion=CrearBanco&usuario=admin& |
| | | 28 | | /// </summary> |
| | | 29 | | [HttpGet("auditoria")] |
| | | 30 | | [FAU.API.Security.RequirePermission(PermisoEnum.VerAuditoria)] |
| | | 31 | | public async Task<IActionResult> GetBitacorasAsync( |
| | | 32 | | [FromQuery] int page = 1, |
| | | 33 | | [FromQuery] int pageSize = 50, |
| | | 34 | | [FromQuery] string? contexto = null, |
| | | 35 | | [FromQuery] string? accion = null, |
| | | 36 | | [FromQuery] string? usuario = null, |
| | | 37 | | [FromQuery] DateTime? desde = null, |
| | | 38 | | [FromQuery] DateTime? hasta = null) |
| | | 39 | | { |
| | | 40 | | try |
| | | 41 | | { |
| | 2 | 42 | | if (page < 1) page = 1; |
| | 2 | 43 | | if (pageSize < 1) pageSize = 50; |
| | 2 | 44 | | if (pageSize > 200) pageSize = 200; |
| | | 45 | | |
| | 2 | 46 | | var (items, total) = await _auditoriaService.GetPagedAsync( |
| | 2 | 47 | | page, pageSize, contexto, accion, usuario, desde, hasta); |
| | | 48 | | |
| | 1 | 49 | | var dtos = items.Select(b => new BitacoraDto |
| | 1 | 50 | | { |
| | 1 | 51 | | Id = b.Id, |
| | 1 | 52 | | UsuarioNombre = b.Usuario.Username, |
| | 1 | 53 | | AccionNombre = b.Accion.Nombre, |
| | 1 | 54 | | ContextoNombre = b.Contexto.Nombre, |
| | 1 | 55 | | Host = b.Host, |
| | 1 | 56 | | Fecha = b.Fecha, |
| | 1 | 57 | | Entidad = b.Entidad, |
| | 1 | 58 | | EntidadId = b.EntidadId, |
| | 1 | 59 | | Detalle = b.Detalle |
| | 1 | 60 | | }); |
| | | 61 | | |
| | 1 | 62 | | var resultado = new PagedResult<BitacoraDto> |
| | 1 | 63 | | { |
| | 1 | 64 | | Items = dtos, |
| | 1 | 65 | | Page = page, |
| | 1 | 66 | | PageSize = pageSize, |
| | 1 | 67 | | TotalCount = total |
| | 1 | 68 | | }; |
| | | 69 | | |
| | 1 | 70 | | return Ok(ApiResponse<PagedResult<BitacoraDto>>.SuccessResult(resultado, "Bitácoras obtenidas correctamente" |
| | | 71 | | } |
| | 1 | 72 | | catch (Exception ex) |
| | | 73 | | { |
| | 1 | 74 | | _logger.LogError(ex, "Error al obtener la auditoría"); |
| | 1 | 75 | | return StatusCode(500, ApiResponse<string>.ErrorResult("Error interno del servidor")); |
| | | 76 | | } |
| | 2 | 77 | | } |
| | | 78 | | } |