Adds .AsNoTracking() to all read-only list/pagination queries in SiteService, AgentService, CameraService, AuditLogService, and UserService. Prevents EF Core change tracker overhead when only displaying data. UserService.ListAsync and UserSites query benefit most (materialize AppUser/UserSite entities). AuditLogService, SiteService, CameraService, AgentService paginated lists are read-only so AsNoTracking is safe.
52 lines
1.6 KiB
C#
52 lines
1.6 KiB
C#
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<ServiceResult<PagedResult<AuditLogResponse>>> 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<PagedResult<AuditLogResponse>>.Success(
|
|
new PagedResult<AuditLogResponse>(items, total, page, pageSize));
|
|
}
|
|
}
|