using General_Ledger_Service.Enums; using General_Ledger_Service.Infra; using General_Ledger_Service.Interfaces.Repositories; using General_Ledger_Service.Models; using Microsoft.EntityFrameworkCore; namespace General_Ledger_Service.Repositories { public class AuditLogRepository(AppDBContext context) : IAuditLogRepository { private readonly AppDBContext _context = context; public async Task AddAsync(AuditLog log) => await _context.AuditLogs.AddAsync(log); public async Task<(List Items, int TotalCount)> ListAsync( List categories, string? entityName, AuditAction? action, string? performedBy, DateOnly? from, DateOnly? to, int? page, int? pageSize) { var query = _context.AuditLogs.Where(a => categories.Contains(a.Category)); if (!string.IsNullOrWhiteSpace(entityName)) { query = query.Where(a => a.EntityName == entityName); } if (action.HasValue) { query = query.Where(a => a.Action == action.Value); } if (!string.IsNullOrWhiteSpace(performedBy)) { query = query.Where(a => a.PerformedBy == performedBy); } if (from.HasValue) { var fromDateTime = from.Value.ToDateTime(TimeOnly.MinValue, DateTimeKind.Utc); query = query.Where(a => a.OccurredAt >= fromDateTime); } if (to.HasValue) { var toDateTimeExclusive = to.Value.AddDays(1).ToDateTime(TimeOnly.MinValue, DateTimeKind.Utc); query = query.Where(a => a.OccurredAt < toDateTimeExclusive); } var totalCount = await query.CountAsync(); var items = await RepositoryHelpers.ApplyPaging(query.OrderByDescending(a => a.OccurredAt), page, pageSize) .ToListAsync(); return (items, totalCount); } } }