using ErsatzTV.Core.Domain; using ErsatzTV.Infrastructure.Data; using Microsoft.EntityFrameworkCore; using static ErsatzTV.Application.Logs.Mapper; namespace ErsatzTV.Application.Logs; public class GetRecentLogEntriesHandler : IRequestHandler { private readonly IDbContextFactory _dbContextFactory; public GetRecentLogEntriesHandler(IDbContextFactory dbContextFactory) => _dbContextFactory = dbContextFactory; public async Task Handle( GetRecentLogEntries request, CancellationToken cancellationToken) { await using LogContext logContext = _dbContextFactory.CreateDbContext(); int count = await logContext.LogEntries.CountAsync(cancellationToken); IOrderedQueryable ordered = logContext.LogEntries .OrderByDescending(le => le.Id); foreach (bool descending in request.SortDescending) { ordered = descending ? logContext.LogEntries.OrderByDescending(request.SortExpression).ThenByDescending(le => le.Id) : logContext.LogEntries.OrderBy(request.SortExpression).ThenByDescending(le => le.Id); } List page = await ordered .Skip(request.PageNum * request.PageSize) .Take(request.PageSize) .ToListAsync(cancellationToken) .Map(list => list.Map(ProjectToViewModel).ToList()); return new PagedLogEntriesViewModel(count, page); } }