Finishing the show api

This commit is contained in:
Zoe Roux 2020-07-25 17:14:53 +02:00
parent 1f53affafa
commit 193004a3ee
7 changed files with 359 additions and 9 deletions

View File

@ -151,6 +151,46 @@ namespace Kyoo.Controllers
Task<Studio> GetStudioFromShow(int showID);
Task<Studio> GetStudioFromShow(string showSlug);
Task<ICollection<Library>> GetLibrariesFromShow(int showID,
Expression<Func<Library, bool>> where = null,
Sort<Library> sort = default,
Pagination limit = default);
Task<ICollection<Library>> GetLibrariesFromShow(int showID,
[Optional] Expression<Func<Library, bool>> where,
Expression<Func<Library, object>> sort,
Pagination limit = default
) => GetLibrariesFromShow(showID, where, new Sort<Library>(sort), limit);
Task<ICollection<Library>> GetLibrariesFromShow(string showSlug,
Expression<Func<Library, bool>> where = null,
Sort<Library> sort = default,
Pagination limit = default);
Task<ICollection<Library>> GetLibrariesFromShow(string showSlug,
[Optional] Expression<Func<Library, bool>> where,
Expression<Func<Library, object>> sort,
Pagination limit = default
) => GetLibrariesFromShow(showSlug, where, new Sort<Library>(sort), limit);
Task<ICollection<Collection>> GetCollectionsFromShow(int showID,
Expression<Func<Collection, bool>> where = null,
Sort<Collection> sort = default,
Pagination limit = default);
Task<ICollection<Collection>> GetCollectionsFromShow(int showID,
[Optional] Expression<Func<Collection, bool>> where,
Expression<Func<Collection, object>> sort,
Pagination limit = default
) => GetCollectionsFromShow(showID, where, new Sort<Collection>(sort), limit);
Task<ICollection<Collection>> GetCollectionsFromShow(string showSlug,
Expression<Func<Collection, bool>> where = null,
Sort<Collection> sort = default,
Pagination limit = default);
Task<ICollection<Collection>> GetCollectionsFromShow(string showSlug,
[Optional] Expression<Func<Collection, bool>> where,
Expression<Func<Collection, object>> sort,
Pagination limit = default
) => GetCollectionsFromShow(showSlug, where, new Sort<Collection>(sort), limit);
// Helpers

View File

@ -193,8 +193,52 @@ namespace Kyoo.Controllers
{
Task<Track> Get(int episodeID, string languageTag, bool isForced);
}
public interface ILibraryRepository : IRepository<Library> {}
public interface ICollectionRepository : IRepository<Collection> {}
public interface ILibraryRepository : IRepository<Library>
{
Task<ICollection<Library>> GetFromShow(int showID,
Expression<Func<Library, bool>> where = null,
Sort<Library> sort = default,
Pagination limit = default);
Task<ICollection<Library>> GetFromShow(int showID,
[Optional] Expression<Func<Library, bool>> where,
Expression<Func<Library, object>> sort,
Pagination limit = default
) => GetFromShow(showID, where, new Sort<Library>(sort), limit);
Task<ICollection<Library>> GetFromShow(string showSlug,
Expression<Func<Library, bool>> where = null,
Sort<Library> sort = default,
Pagination limit = default);
Task<ICollection<Library>> GetFromShow(string showSlug,
[Optional] Expression<Func<Library, bool>> where,
Expression<Func<Library, object>> sort,
Pagination limit = default
) => GetFromShow(showSlug, where, new Sort<Library>(sort), limit);
}
public interface ICollectionRepository : IRepository<Collection>
{
Task<ICollection<Collection>> GetFromShow(int showID,
Expression<Func<Collection, bool>> where = null,
Sort<Collection> sort = default,
Pagination limit = default);
Task<ICollection<Collection>> GetFromShow(int showID,
[Optional] Expression<Func<Collection, bool>> where,
Expression<Func<Collection, object>> sort,
Pagination limit = default
) => GetFromShow(showID, where, new Sort<Collection>(sort), limit);
Task<ICollection<Collection>> GetFromShow(string showSlug,
Expression<Func<Collection, bool>> where = null,
Sort<Collection> sort = default,
Pagination limit = default);
Task<ICollection<Collection>> GetFromShow(string showSlug,
[Optional] Expression<Func<Collection, bool>> where,
Expression<Func<Collection, object>> sort,
Pagination limit = default
) => GetFromShow(showSlug, where, new Sort<Collection>(sort), limit);
}
public interface IGenreRepository : IRepository<Genre>
{

View File

@ -297,6 +297,38 @@ namespace Kyoo.Controllers
return StudioRepository.GetFromShow(showSlug);
}
public Task<ICollection<Library>> GetLibrariesFromShow(int showID,
Expression<Func<Library, bool>> where = null,
Sort<Library> sort = default,
Pagination limit = default)
{
return LibraryRepository.GetFromShow(showID, where, sort, limit);
}
public Task<ICollection<Library>> GetLibrariesFromShow(string showSlug,
Expression<Func<Library, bool>> where = null,
Sort<Library> sort = default,
Pagination limit = default)
{
return LibraryRepository.GetFromShow(showSlug, where, sort, limit);
}
public Task<ICollection<Collection>> GetCollectionsFromShow(int showID,
Expression<Func<Collection, bool>> where = null,
Sort<Collection> sort = default,
Pagination limit = default)
{
return CollectionRepository.GetFromShow(showID, where, sort, limit);
}
public Task<ICollection<Collection>> GetCollectionsFromShow(string showSlug,
Expression<Func<Collection, bool>> where = null,
Sort<Collection> sort = default,
Pagination limit = default)
{
return CollectionRepository.GetFromShow(showSlug, where, sort, limit);
}
public Task AddShowLink(int showID, int? libraryID, int? collectionID)
{
return ShowRepository.AddShowLink(showID, libraryID, collectionID);

View File

@ -6,19 +6,36 @@ using System.Threading.Tasks;
using Kyoo.Models;
using Kyoo.Models.Exceptions;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
namespace Kyoo.Controllers
{
public class CollectionRepository : LocalRepository<Collection>, ICollectionRepository
{
private readonly DatabaseContext _database;
private readonly Lazy<IShowRepository> _shows;
protected override Expression<Func<Collection, object>> DefaultSort => x => x.Name;
public CollectionRepository(DatabaseContext database) : base(database)
public CollectionRepository(DatabaseContext database, IServiceProvider services) : base(database)
{
_database = database;
_shows = new Lazy<IShowRepository>(services.GetRequiredService<IShowRepository>);
}
public override void Dispose()
{
base.Dispose();
if (_shows.IsValueCreated)
_shows.Value.Dispose();
}
public override async ValueTask DisposeAsync()
{
await _database.DisposeAsync();
if (_shows.IsValueCreated)
await _shows.Value.DisposeAsync();
}
public override async Task<ICollection<Collection>> Search(string query)
{
return await _database.Collections
@ -68,5 +85,37 @@ namespace Kyoo.Controllers
_database.Entry(link).State = EntityState.Deleted;
await _database.SaveChangesAsync();
}
public async Task<ICollection<Collection>> GetFromShow(int showID,
Expression<Func<Collection, bool>> where = null,
Sort<Collection> sort = default,
Pagination limit = default)
{
ICollection<Collection> collections = await ApplyFilters(_database.CollectionLinks
.Where(x => x.ShowID == showID)
.Select(x => x.Collection),
where,
sort,
limit);
if (!collections.Any() & await _shows.Value.Get(showID) == null)
throw new ItemNotFound();
return collections;
}
public async Task<ICollection<Collection>> GetFromShow(string showSlug,
Expression<Func<Collection, bool>> where = null,
Sort<Collection> sort = default,
Pagination limit = default)
{
ICollection<Collection> collections = await ApplyFilters(_database.CollectionLinks
.Where(x => x.Show.Slug == showSlug)
.Select(x => x.Collection),
where,
sort,
limit);
if (!collections.Any() & await _shows.Value.Get(showSlug) == null)
throw new ItemNotFound();
return collections;
}
}
}

View File

@ -22,7 +22,20 @@ namespace Kyoo.Controllers
_database = database;
_shows = new Lazy<IShowRepository>(services.GetRequiredService<IShowRepository>);
}
public override void Dispose()
{
base.Dispose();
if (_shows.IsValueCreated)
_shows.Value.Dispose();
}
public override async ValueTask DisposeAsync()
{
await _database.DisposeAsync();
if (_shows.IsValueCreated)
await _shows.Value.DisposeAsync();
}
public override async Task<ICollection<Genre>> Search(string query)
{
@ -92,7 +105,8 @@ namespace Kyoo.Controllers
Sort<Genre> sort = default,
Pagination limit = default)
{
ICollection<Genre> genres = await ApplyFilters(_database.GenreLinks.Where(x => x.Show.Slug == showSlug)
ICollection<Genre> genres = await ApplyFilters(_database.GenreLinks
.Where(x => x.Show.Slug == showSlug)
.Select(x => x.Genre),
where,
sort,

View File

@ -6,6 +6,7 @@ using System.Threading.Tasks;
using Kyoo.Models;
using Kyoo.Models.Exceptions;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
namespace Kyoo.Controllers
{
@ -13,26 +14,32 @@ namespace Kyoo.Controllers
{
private readonly DatabaseContext _database;
private readonly IProviderRepository _providers;
private readonly Lazy<IShowRepository> _shows;
protected override Expression<Func<Library, object>> DefaultSort => x => x.ID;
public LibraryRepository(DatabaseContext database, IProviderRepository providers) : base(database)
public LibraryRepository(DatabaseContext database, IProviderRepository providers, IServiceProvider services)
: base(database)
{
_database = database;
_providers = providers;
_shows = new Lazy<IShowRepository>(services.GetRequiredService<IShowRepository>);
}
public override void Dispose()
{
_database.Dispose();
_providers.Dispose();
if (_shows.IsValueCreated)
_shows.Value.Dispose();
}
public override async ValueTask DisposeAsync()
{
await _database.DisposeAsync();
await _providers.DisposeAsync();
if (_shows.IsValueCreated)
await _shows.Value.DisposeAsync();
}
public override async Task<ICollection<Library>> Search(string query)
@ -90,5 +97,37 @@ namespace Kyoo.Controllers
_database.Entry(entry).State = EntityState.Deleted;
await _database.SaveChangesAsync();
}
public async Task<ICollection<Library>> GetFromShow(int showID,
Expression<Func<Library, bool>> where = null,
Sort<Library> sort = default,
Pagination limit = default)
{
ICollection<Library> libraries = await ApplyFilters(_database.LibraryLinks
.Where(x => x.ShowID == showID)
.Select(x => x.Library),
where,
sort,
limit);
if (!libraries.Any() && await _shows.Value.Get(showID) == null)
throw new ItemNotFound();
return libraries;
}
public async Task<ICollection<Library>> GetFromShow(string showSlug,
Expression<Func<Library, bool>> where = null,
Sort<Library> sort = default,
Pagination limit = default)
{
ICollection<Library> libraries = await ApplyFilters(_database.LibraryLinks
.Where(x => x.Show.Slug == showSlug)
.Select(x => x.Library),
where,
sort,
limit);
if (!libraries.Any() && await _shows.Value.Get(showSlug) == null)
throw new ItemNotFound();
return libraries;
}
}
}

View File

@ -314,5 +314,137 @@ namespace Kyoo.Api
return NotFound();
}
}
[HttpGet("{showID:int}/library")]
[HttpGet("{showID:int}/libraries")]
[Authorize(Policy = "Read")]
public async Task<ActionResult<Page<Library>>> GetLibraries(int showID,
[FromQuery] string sortBy,
[FromQuery] int afterID,
[FromQuery] Dictionary<string, string> where,
[FromQuery] int limit = 30)
{
where.Remove("showID");
where.Remove("sortBy");
where.Remove("limit");
where.Remove("afterID");
try
{
ICollection<Library> ressources = await _libraryManager.GetLibrariesFromShow(showID,
ApiHelper.ParseWhere<Library>(where),
new Sort<Library>(sortBy),
new Pagination(limit, afterID));
return Page(ressources, limit);
}
catch (ItemNotFound)
{
return NotFound();
}
catch (ArgumentException ex)
{
return BadRequest(new {Error = ex.Message});
}
}
[HttpGet("{slug}/library")]
[HttpGet("{slug}/libraries")]
[Authorize(Policy = "Read")]
public async Task<ActionResult<Page<Library>>> GetLibraries(string slug,
[FromQuery] string sortBy,
[FromQuery] int afterID,
[FromQuery] Dictionary<string, string> where,
[FromQuery] int limit = 30)
{
where.Remove("slug");
where.Remove("sortBy");
where.Remove("limit");
where.Remove("afterID");
try
{
ICollection<Library> ressources = await _libraryManager.GetLibrariesFromShow(slug,
ApiHelper.ParseWhere<Library>(where),
new Sort<Library>(sortBy),
new Pagination(limit, afterID));
return Page(ressources, limit);
}
catch (ItemNotFound)
{
return NotFound();
}
catch (ArgumentException ex)
{
return BadRequest(new {Error = ex.Message});
}
}
[HttpGet("{showID:int}/collection")]
[HttpGet("{showID:int}/collections")]
[Authorize(Policy = "Read")]
public async Task<ActionResult<Page<Collection>>> GetCollections(int showID,
[FromQuery] string sortBy,
[FromQuery] int afterID,
[FromQuery] Dictionary<string, string> where,
[FromQuery] int limit = 30)
{
where.Remove("showID");
where.Remove("sortBy");
where.Remove("limit");
where.Remove("afterID");
try
{
ICollection<Collection> ressources = await _libraryManager.GetCollectionsFromShow(showID,
ApiHelper.ParseWhere<Collection>(where),
new Sort<Collection>(sortBy),
new Pagination(limit, afterID));
return Page(ressources, limit);
}
catch (ItemNotFound)
{
return NotFound();
}
catch (ArgumentException ex)
{
return BadRequest(new {Error = ex.Message});
}
}
[HttpGet("{slug}/collection")]
[HttpGet("{slug}/collections")]
[Authorize(Policy = "Read")]
public async Task<ActionResult<Page<Collection>>> GetCollections(string slug,
[FromQuery] string sortBy,
[FromQuery] int afterID,
[FromQuery] Dictionary<string, string> where,
[FromQuery] int limit = 30)
{
where.Remove("slug");
where.Remove("sortBy");
where.Remove("limit");
where.Remove("afterID");
try
{
ICollection<Collection> ressources = await _libraryManager.GetCollectionsFromShow(slug,
ApiHelper.ParseWhere<Collection>(where),
new Sort<Collection>(sortBy),
new Pagination(limit, afterID));
return Page(ressources, limit);
}
catch (ItemNotFound)
{
return NotFound();
}
catch (ArgumentException ex)
{
return BadRequest(new {Error = ex.Message});
}
}
}
}