using ColaFlow.Modules.ProjectManagement.Domain.Entities; using ColaFlow.Modules.ProjectManagement.Domain.Repositories; using ColaFlow.Modules.ProjectManagement.Infrastructure.Persistence; using Microsoft.EntityFrameworkCore; namespace ColaFlow.Modules.ProjectManagement.Infrastructure.Repositories; public class AuditLogRepository : IAuditLogRepository { private readonly PMDbContext _context; public AuditLogRepository(PMDbContext context) { _context = context; } public async Task GetByIdAsync(Guid id, CancellationToken cancellationToken = default) { return await _context.AuditLogs .AsNoTracking() .FirstOrDefaultAsync(a => a.Id == id, cancellationToken); } public async Task> GetByEntityAsync( string entityType, Guid entityId, CancellationToken cancellationToken = default) { return await _context.AuditLogs .AsNoTracking() .Where(a => a.EntityType == entityType && a.EntityId == entityId) .OrderByDescending(a => a.Timestamp) .ToListAsync(cancellationToken); } public async Task> GetByUserAsync( Guid userId, int pageNumber = 1, int pageSize = 50, CancellationToken cancellationToken = default) { return await _context.AuditLogs .AsNoTracking() .Where(a => a.UserId == userId) .OrderByDescending(a => a.Timestamp) .Skip((pageNumber - 1) * pageSize) .Take(pageSize) .ToListAsync(cancellationToken); } public async Task> GetRecentAsync( int count = 100, CancellationToken cancellationToken = default) { return await _context.AuditLogs .AsNoTracking() .OrderByDescending(a => a.Timestamp) .Take(count) .ToListAsync(cancellationToken); } public async Task AddAsync(AuditLog auditLog, CancellationToken cancellationToken = default) { await _context.AuditLogs.AddAsync(auditLog, cancellationToken); await _context.SaveChangesAsync(cancellationToken); } public async Task GetCountAsync(CancellationToken cancellationToken = default) { return await _context.AuditLogs.CountAsync(cancellationToken); } }