using Dapper; using ErsatzTV.Infrastructure.Data; using Microsoft.EntityFrameworkCore; namespace ErsatzTV.Application.Libraries; public class CountMediaItemsByLibraryHandler : IRequestHandler { private readonly IDbContextFactory _dbContextFactory; public CountMediaItemsByLibraryHandler(IDbContextFactory dbContextFactory) => _dbContextFactory = dbContextFactory; public async Task Handle(CountMediaItemsByLibrary request, CancellationToken cancellationToken) { await using TvContext dbContext = await _dbContextFactory.CreateDbContextAsync(cancellationToken); return await dbContext.Connection.QuerySingleAsync( @"SELECT COUNT(*) FROM MediaItem INNER JOIN LibraryPath LP on MediaItem.LibraryPathId = LP.Id WHERE LP.LibraryId = @LibraryId", new { request.LibraryId }); } }