Files
ersatztv/ErsatzTV.Application/ProgramSchedules/Queries/GetPagedProgramSchedulesHandler.cs
T
timothy 19bb605aae fix(690,758): count the same query a paged handler pages
`GetPagedRerunCollections` (#690) and `GetPagedPlayouts` (#758) were filed as
separate bugs; they are one mechanism, and the sweep found it at six sites.
Each handler computed `CountAsync` over the whole `DbSet` and then paged a
differently-filtered `IQueryable`, so `TotalCount` reported the size of the
table whenever a search query was supplied. The page is right and the count is
wrong, which is silent: the SPA paginates on `TotalCount` and renders pages
that can never contain anything, and an MCP agent paging to a completeness
target reads a total its own page can never reach.

The two named handlers were two of six. Sweeping `ErsatzTV.Application` for the
shape rather than auditing the issues' file lists also found
`GetPagedCollections`, `GetPagedMultiCollections`, `GetPagedSmartCollections`
and `GetPagedProgramSchedules`. `GetPagedMultiCollections` and
`GetPagedSmartCollections` were in the half-state that makes the structural fix
the right one: their counts carried the `OwnedByChannelId == null` clause
faithfully and silently omitted the `Query` clause added later.

So the fix is not "add the missing Where to the count" — both are derived from
one `IQueryable` and cannot drift. Includes move to the page chain, where they
already belonged: a COUNT does not materialize the graph, and #671's bound on
the rerun selection graph is unaffected. `GetPagedPlayouts` additionally gains
its `Channel != null` filter in the count, which the DbSet count never had.

`GetPagedFillerPresets` and `GetPagedTraktLists` take no `Query` parameter, so
their DbSet counts already count what they page and are deliberately unchanged.
`GetBlockPlayoutHistory`, `GetFuturePlayoutItemsById` and `GetLibraryBrowseItems`
already used this idiom.

Mutation proof: restoring the pre-fix `CountAsync` clause at all six sites turns
all six new tests red; each test constructs exactly one handler, so every test
is shown to detect its own site. Expected counts are pinned literals, never
recomputed by re-applying the handler's own predicate.

BOMs stripped from the three touched handlers that carried one (#311
fix-as-you-touch; `dotnet format whitespace` reported CHARSET on them).

fixes #690
fixes #758
2026-08-26 08:04:03 +02:00

36 lines
1.4 KiB
C#

using ErsatzTV.Core.Domain;
using ErsatzTV.Infrastructure.Data;
using Microsoft.EntityFrameworkCore;
using static ErsatzTV.Application.ProgramSchedules.Mapper;
namespace ErsatzTV.Application.ProgramSchedules;
public class GetPagedProgramSchedulesHandler(IDbContextFactory<TvContext> dbContextFactory)
: IRequestHandler<GetPagedProgramSchedules, PagedProgramSchedulesViewModel>
{
public async Task<PagedProgramSchedulesViewModel> Handle(
GetPagedProgramSchedules request,
CancellationToken cancellationToken)
{
await using TvContext dbContext = await dbContextFactory.CreateDbContextAsync(cancellationToken);
IQueryable<ProgramSchedule> query = dbContext.ProgramSchedules.AsNoTracking();
if (!string.IsNullOrWhiteSpace(request.Query))
{
query = query.Where(ps => EF.Functions.Like(ps.Name, $"%{request.Query}%"));
}
// count the SAME query the page is taken from, so the two cannot drift (issues #690, #758)
int count = await query.CountAsync(cancellationToken);
List<ProgramScheduleViewModel> page = await query
.OrderBy(ps => ps.Name)
.Skip(request.PageNum * request.PageSize)
.Take(request.PageSize)
.ToListAsync(cancellationToken)
.Map(list => list.Map(ProjectToViewModel).ToList());
return new PagedProgramSchedulesViewModel(count, page);
}
}