using ErsatzTV.Application.MediaCollections; using ErsatzTV.Infrastructure.Data; using Microsoft.EntityFrameworkCore; using static ErsatzTV.Application.MediaCollections.Mapper; namespace ErsatzTV.Application.Search; public class SearchSmartCollectionsHandler(IDbContextFactory dbContextFactory) : IRequestHandler> { public async Task> Handle( SearchSmartCollections request, CancellationToken cancellationToken) { await using TvContext dbContext = await dbContextFactory.CreateDbContextAsync(cancellationToken); return await dbContext.SmartCollections .AsNoTracking() // Hide system-owned auto-tune weighted artifacts (#425) from the scheduling picker: selecting one // into a user schedule would let a later channel delete cascade away that schedule item. .Where(sc => sc.OwnedByChannelId == null) .Where(sc => EF.Functions.Like(sc.Name, $"%{request.Query}%")) .OrderBy(sc => sc.Name) .Take(10) .ToListAsync(cancellationToken) .Map(list => list.Map(ProjectToViewModel).ToList()); } }