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.
36 lines
1.5 KiB
36 lines
1.5 KiB
using System.Collections.Immutable; |
|
using System.Globalization; |
|
using ErsatzTV.Application.MediaItems; |
|
using ErsatzTV.Core.Domain; |
|
using ErsatzTV.Core.Interfaces.Search; |
|
using ErsatzTV.Infrastructure.Data; |
|
using ErsatzTV.Infrastructure.Search; |
|
using Microsoft.EntityFrameworkCore; |
|
|
|
namespace ErsatzTV.Application.Search; |
|
|
|
public class SearchMoviesHandler( |
|
ISearchIndex searchIndex, |
|
IDbContextFactory<TvContext> dbContextFactory) |
|
: SearchUsingSearchIndexHandler(searchIndex), IRequestHandler<SearchMovies, List<NamedMediaItemViewModel>> |
|
{ |
|
public async Task<List<NamedMediaItemViewModel>> Handle(SearchMovies request, CancellationToken cancellationToken) |
|
{ |
|
ImmutableHashSet<int> ids = await Search(LuceneSearchIndex.MovieType, request.Query, cancellationToken); |
|
|
|
await using TvContext dbContext = await dbContextFactory.CreateDbContextAsync(cancellationToken); |
|
return await dbContext.MovieMetadata |
|
.TagWithCallSite() |
|
.AsNoTracking() |
|
.Where(mm => ids.Contains(mm.MovieId)) |
|
.OrderBy(mm => mm.Title) |
|
.ThenBy(mm => mm.Year) |
|
.ToListAsync(cancellationToken) |
|
.Map(list => list.Map(ToNamedMediaItem).ToList()); |
|
} |
|
|
|
private static NamedMediaItemViewModel ToNamedMediaItem(MovieMetadata movie) => |
|
new( |
|
movie.MovieId, |
|
$"{movie.Title} ({(movie.Year.HasValue ? movie.Year.Value.ToString(CultureInfo.InvariantCulture) : "???")})"); |
|
}
|
|
|