| | | 1 | | using FAU.Entidades; |
| | | 2 | | using Microsoft.EntityFrameworkCore; |
| | | 3 | | |
| | | 4 | | namespace FAU.DataAccess.Repositories; |
| | | 5 | | |
| | | 6 | | public class AuditoriaRepository : IAuditoriaRepository |
| | | 7 | | { |
| | | 8 | | private readonly ApplicationDbContext _context; |
| | | 9 | | |
| | 3 | 10 | | public AuditoriaRepository(ApplicationDbContext context) |
| | | 11 | | { |
| | 3 | 12 | | _context = context; |
| | 3 | 13 | | } |
| | | 14 | | |
| | | 15 | | public async Task<(IEnumerable<BitacoraAuditoria> Items, int TotalCount)> GetPagedAsync( |
| | | 16 | | int page, |
| | | 17 | | int pageSize, |
| | | 18 | | string? contexto = null, |
| | | 19 | | string? accion = null, |
| | | 20 | | string? usuario = null, |
| | | 21 | | DateTime? desde = null, |
| | | 22 | | DateTime? hasta = null) |
| | | 23 | | { |
| | 3 | 24 | | var query = _context.BitacorasAuditoria |
| | 3 | 25 | | .Include(b => b.Usuario) |
| | 3 | 26 | | .Include(b => b.Accion) |
| | 3 | 27 | | .Include(b => b.Contexto) |
| | 3 | 28 | | .AsQueryable(); |
| | | 29 | | |
| | 3 | 30 | | if (!string.IsNullOrWhiteSpace(contexto)) |
| | 1 | 31 | | query = query.Where(b => b.Contexto.Nombre == contexto); |
| | | 32 | | |
| | 3 | 33 | | if (!string.IsNullOrWhiteSpace(accion)) |
| | 0 | 34 | | query = query.Where(b => b.Accion.Nombre == accion); |
| | | 35 | | |
| | 3 | 36 | | if (!string.IsNullOrWhiteSpace(usuario)) |
| | 0 | 37 | | query = query.Where(b => b.Usuario.Username.Contains(usuario)); |
| | | 38 | | |
| | 3 | 39 | | if (desde.HasValue) |
| | 0 | 40 | | query = query.Where(b => b.Fecha >= desde.Value); |
| | | 41 | | |
| | 3 | 42 | | if (hasta.HasValue) |
| | 0 | 43 | | query = query.Where(b => b.Fecha <= hasta.Value); |
| | | 44 | | |
| | 3 | 45 | | var total = await query.CountAsync(); |
| | | 46 | | |
| | 3 | 47 | | var items = await query |
| | 3 | 48 | | .OrderByDescending(b => b.Fecha) |
| | 3 | 49 | | .Skip((page - 1) * pageSize) |
| | 3 | 50 | | .Take(pageSize) |
| | 3 | 51 | | .ToListAsync(); |
| | | 52 | | |
| | 3 | 53 | | return (items, total); |
| | 3 | 54 | | } |
| | | 55 | | |
| | | 56 | | public async Task<BitacoraAuditoria> CreateAsync(BitacoraAuditoria bitacora) |
| | | 57 | | { |
| | 0 | 58 | | _context.BitacorasAuditoria.Add(bitacora); |
| | 0 | 59 | | await _context.SaveChangesAsync(); |
| | 0 | 60 | | return bitacora; |
| | 0 | 61 | | } |
| | | 62 | | } |