Support collections/id/items via dapper

This commit is contained in:
Zoe Roux 2023-11-27 01:22:48 +01:00
parent 5f177e9338
commit 29314d473f
3 changed files with 60 additions and 30 deletions

View File

@ -20,7 +20,6 @@ using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Data.Common; using System.Data.Common;
using System.IO; using System.IO;
using System.Linq.Expressions;
using System.Threading.Tasks; using System.Threading.Tasks;
using Kyoo.Abstractions.Controllers; using Kyoo.Abstractions.Controllers;
using Kyoo.Abstractions.Models; using Kyoo.Abstractions.Models;
@ -66,9 +65,15 @@ namespace Kyoo.Core.Controllers
if (items[0] is Show show && show.Id != 0) if (items[0] is Show show && show.Id != 0)
return show; return show;
if (items[1] is Movie movie && movie.Id != 0) if (items[1] is Movie movie && movie.Id != 0)
{
movie.Id = -movie.Id;
return movie; return movie;
}
if (items[2] is Collection collection && collection.Id != 0) if (items[2] is Collection collection && collection.Id != 0)
{
collection.Id += 10_000;
return collection; return collection;
}
throw new InvalidDataException(); throw new InvalidDataException();
} }
@ -77,27 +82,48 @@ namespace Kyoo.Core.Controllers
{ } { }
public async Task<ICollection<ILibraryItem>> GetAllOfCollection( public async Task<ICollection<ILibraryItem>> GetAllOfCollection(
Expression<Func<Collection, bool>> selector, int collectionId,
Expression<Func<ILibraryItem, bool>>? where = null, Filter<ILibraryItem>? filter = default,
Sort<ILibraryItem>? sort = default, Sort<ILibraryItem>? sort = default,
Pagination? limit = default, Include<ILibraryItem>? include = default,
Include<ILibraryItem>? include = default) Pagination? limit = default)
{ {
throw new NotImplementedException(); // language=PostgreSQL
// return await ApplyFilters( FormattableString sql = $"""
// _database.LibraryItems select
// .Where(item => s.*,
// _database.Movies m.*
// .Where(x => x.Id == -item.Id) /* includes */
// .Any(x => x.Collections!.AsQueryable().Any(selector)) from (
// || _database.Shows select
// .Where(x => x.Id == item.Id) * -- Show
// .Any(x => x.Collections!.AsQueryable().Any(selector)) from
// ), shows
// where, inner join link_collection_show as ls on ls.show_id = id and ls.collection_id = {collectionId}
// sort, ) as s
// limit, full outer join (
// include); select
* -- Movie
from
movies
inner join link_collection_movie as lm on lm.movie_id = id and lm.collection_id = {collectionId}
) as m on false
""";
return await Database.Query<ILibraryItem>(
sql,
new()
{
{ "s", typeof(Show) },
{ "m", typeof(Movie) },
},
Mapper,
(id) => Get(id),
include,
filter,
sort ?? new Sort<ILibraryItem>.Default(),
limit ?? new()
);
} }
} }
} }

View File

@ -144,17 +144,21 @@ namespace Kyoo.Core.Api
[FromQuery] Pagination pagination, [FromQuery] Pagination pagination,
[FromQuery] Include<ILibraryItem>? fields) [FromQuery] Include<ILibraryItem>? fields)
{ {
// ICollection<ILibraryItem> resources = await _items.GetAllOfCollection( int collectionId = await identifier.Match(
// identifier.IsSame<Collection>(), id => Task.FromResult(id),
// filter, async slug => (await _libraryManager.Collections.Get(slug)).Id
// sortBy == new Sort<ILibraryItem>.Default() ? new Sort<ILibraryItem>.By(nameof(Movie.AirDate)) : sortBy, );
// pagination, ICollection<ILibraryItem> resources = await _items.GetAllOfCollection(
// fields collectionId,
// ); filter,
sortBy == new Sort<ILibraryItem>.Default() ? new Sort<ILibraryItem>.By(nameof(Movie.AirDate)) : sortBy,
fields,
pagination
);
// if (!resources.Any() && await _libraryManager.Collections.GetOrDefault(identifier.IsSame<Collection>()) == null) if (!resources.Any() && await _libraryManager.Collections.GetOrDefault(identifier.IsSame<Collection>()) == null)
return NotFound(); return NotFound();
// return Page(resources, pagination.Limit); return Page(resources, pagination.Limit);
} }
/// <summary> /// <summary>

View File

@ -88,7 +88,7 @@ public class SearchManager : ISearchManager
}); });
// Since library items's ID are still ints mapped from real items ids, we must map it here to match the db's value. // Since library items's ID are still ints mapped from real items ids, we must map it here to match the db's value.
// Look at the items Migration's sql to understand where magic numbers come from. // Look at the LibraryItemRepository's Mapper to understand what those magic numbers are.
List<int> ids = res.Hits.Select(x => x.Kind switch List<int> ids = res.Hits.Select(x => x.Kind switch
{ {
nameof(Show) => x.Id, nameof(Show) => x.Id,