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.Data.Common;
using System.IO;
using System.Linq.Expressions;
using System.Threading.Tasks;
using Kyoo.Abstractions.Controllers;
using Kyoo.Abstractions.Models;
@ -66,9 +65,15 @@ namespace Kyoo.Core.Controllers
if (items[0] is Show show && show.Id != 0)
return show;
if (items[1] is Movie movie && movie.Id != 0)
{
movie.Id = -movie.Id;
return movie;
}
if (items[2] is Collection collection && collection.Id != 0)
{
collection.Id += 10_000;
return collection;
}
throw new InvalidDataException();
}
@ -77,27 +82,48 @@ namespace Kyoo.Core.Controllers
{ }
public async Task<ICollection<ILibraryItem>> GetAllOfCollection(
Expression<Func<Collection, bool>> selector,
Expression<Func<ILibraryItem, bool>>? where = null,
int collectionId,
Filter<ILibraryItem>? filter = default,
Sort<ILibraryItem>? sort = default,
Pagination? limit = default,
Include<ILibraryItem>? include = default)
Include<ILibraryItem>? include = default,
Pagination? limit = default)
{
throw new NotImplementedException();
// return await ApplyFilters(
// _database.LibraryItems
// .Where(item =>
// _database.Movies
// .Where(x => x.Id == -item.Id)
// .Any(x => x.Collections!.AsQueryable().Any(selector))
// || _database.Shows
// .Where(x => x.Id == item.Id)
// .Any(x => x.Collections!.AsQueryable().Any(selector))
// ),
// where,
// sort,
// limit,
// include);
// language=PostgreSQL
FormattableString sql = $"""
select
s.*,
m.*
/* includes */
from (
select
* -- Show
from
shows
inner join link_collection_show as ls on ls.show_id = id and ls.collection_id = {collectionId}
) as s
full outer join (
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] Include<ILibraryItem>? fields)
{
// ICollection<ILibraryItem> resources = await _items.GetAllOfCollection(
// identifier.IsSame<Collection>(),
// filter,
// sortBy == new Sort<ILibraryItem>.Default() ? new Sort<ILibraryItem>.By(nameof(Movie.AirDate)) : sortBy,
// pagination,
// fields
// );
int collectionId = await identifier.Match(
id => Task.FromResult(id),
async slug => (await _libraryManager.Collections.Get(slug)).Id
);
ICollection<ILibraryItem> resources = await _items.GetAllOfCollection(
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 Page(resources, pagination.Limit);
return Page(resources, pagination.Limit);
}
/// <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.
// 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
{
nameof(Show) => x.Id,