using Heimdall.Application.Common; using Heimdall.Application.DTOs; using Heimdall.Application.Services; using Heimdall.Infrastructure.Data; using Microsoft.EntityFrameworkCore; namespace Heimdall.Infrastructure.Services; public class AuditLogService : IAuditLogService { private readonly AppDbContext _db; public AuditLogService(AppDbContext db) => _db = db; public async Task>> ListAsync( string? resourceType = null, string? action = null, int page = 1, int pageSize = 20, CancellationToken ct = default) { var query = _db.AuditLogs.AsNoTracking(); if (!string.IsNullOrEmpty(resourceType)) query = query.Where(al => al.ResourceType == resourceType); if (!string.IsNullOrEmpty(action)) query = query.Where(al => al.Action == action); var total = await query.CountAsync(ct); var items = await query .OrderByDescending(al => al.Timestamp) .Skip((page - 1) * pageSize) .Take(pageSize) .Select(al => new AuditLogResponse( al.Id, al.UserId, al.User != null ? al.User.Email : null, al.Action, al.ResourceType, al.ResourceId, al.OldValue, al.NewValue, al.IpAddress, al.Timestamp)) .ToListAsync(ct); return ServiceResult>.Success( new PagedResult(items, total, page, pageSize)); } }