mirror of https://github.com/ErsatzTV/ErsatzTV.git
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
28 lines
1.2 KiB
28 lines
1.2 KiB
using ErsatzTV.Application.MediaCollections; |
|
using ErsatzTV.Infrastructure.Data; |
|
using Microsoft.EntityFrameworkCore; |
|
using static ErsatzTV.Application.MediaCollections.Mapper; |
|
|
|
namespace ErsatzTV.Application.Search; |
|
|
|
public class SearchSmartCollectionsHandler : IRequestHandler<SearchSmartCollections, List<SmartCollectionViewModel>> |
|
{ |
|
private readonly IDbContextFactory<TvContext> _dbContextFactory; |
|
|
|
public SearchSmartCollectionsHandler(IDbContextFactory<TvContext> dbContextFactory) => |
|
_dbContextFactory = dbContextFactory; |
|
|
|
public async Task<List<SmartCollectionViewModel>> Handle( |
|
SearchSmartCollections request, |
|
CancellationToken cancellationToken) |
|
{ |
|
await using TvContext dbContext = await _dbContextFactory.CreateDbContextAsync(cancellationToken); |
|
return await dbContext.SmartCollections |
|
.AsNoTracking() |
|
.Where(c => EF.Functions.Like(EF.Functions.Collate(c.Name, TvContext.CaseInsensitiveCollation), $"%{request.Query}%")) |
|
.OrderBy(c => EF.Functions.Collate(c.Name, TvContext.CaseInsensitiveCollation)) |
|
.Take(10) |
|
.ToListAsync(cancellationToken) |
|
.Map(list => list.Map(ProjectToViewModel).ToList()); |
|
} |
|
}
|
|
|