diff --git a/Kyoo.Common/Controllers/ILibraryManager.cs b/Kyoo.Common/Controllers/ILibraryManager.cs index 52acb660..3da6fd8f 100644 --- a/Kyoo.Common/Controllers/ILibraryManager.cs +++ b/Kyoo.Common/Controllers/ILibraryManager.cs @@ -237,7 +237,6 @@ namespace Kyoo.Controllers Expression> where = null, Sort sort = default, Pagination limit = default); - Task> GetItemsFromLibrary(int id, [Optional] Expression> where, Expression> sort, @@ -248,12 +247,51 @@ namespace Kyoo.Controllers Expression> where = null, Sort sort = default, Pagination limit = default); - Task> GetItemsFromLibrary(string librarySlug, [Optional] Expression> where, Expression> sort, Pagination limit = default ) => GetItemsFromLibrary(librarySlug, where, new Sort(sort), limit); + + Task> GetShowsFromCollection(int id, + Expression> where = null, + Sort sort = default, + Pagination limit = default); + Task> GetShowsFromCollection(int id, + [Optional] Expression> where, + Expression> sort, + Pagination limit = default + ) => GetShowsFromCollection(id, where, new Sort(sort), limit); + + Task> GetShowsFromCollection(string slug, + Expression> where = null, + Sort sort = default, + Pagination limit = default); + Task> GetShowsFromCollection(string slug, + [Optional] Expression> where, + Expression> sort, + Pagination limit = default + ) => GetShowsFromCollection(slug, where, new Sort(sort), limit); + + Task> GetLibrariesFromCollection(int id, + Expression> where = null, + Sort sort = default, + Pagination limit = default); + Task> GetLibrariesFromCollection(int id, + [Optional] Expression> where, + Expression> sort, + Pagination limit = default + ) => GetLibrariesFromCollection(id, where, new Sort(sort), limit); + + Task> GetLibrariesFromCollection(string slug, + Expression> where = null, + Sort sort = default, + Pagination limit = default); + Task> GetLibrariesFromCollection(string slug, + [Optional] Expression> where, + Expression> sort, + Pagination limit = default + ) => GetLibrariesFromCollection(slug, where, new Sort(sort), limit); // Helpers diff --git a/Kyoo.Common/Controllers/IRepository.cs b/Kyoo.Common/Controllers/IRepository.cs index e2e5a2c0..1553951e 100644 --- a/Kyoo.Common/Controllers/IRepository.cs +++ b/Kyoo.Common/Controllers/IRepository.cs @@ -123,25 +123,25 @@ namespace Kyoo.Controllers Pagination limit = default ) => GetFromLibrary(slug, where, new Sort(sort), limit); - // Task> GetFromCollection(int id, - // Expression> where = null, - // Sort sort = default, - // Pagination limit = default); - // Task> GetFromCollection(int id, - // [Optional] Expression> where, - // Expression> sort, - // Pagination limit = default - // ) => GetFromCollection(id, where, new Sort(sort), limit); - // - // Task> GetFromCollection(string slug, - // Expression> where = null, - // Sort sort = default, - // Pagination limit = default); - // Task> GetFromCollection(string slug, - // [Optional] Expression> where, - // Expression> sort, - // Pagination limit = default - // ) => GetFromCollection(slug, where, new Sort(sort), limit); + Task> GetFromCollection(int id, + Expression> where = null, + Sort sort = default, + Pagination limit = default); + Task> GetFromCollection(int id, + [Optional] Expression> where, + Expression> sort, + Pagination limit = default + ) => GetFromCollection(id, where, new Sort(sort), limit); + + Task> GetFromCollection(string slug, + Expression> where = null, + Sort sort = default, + Pagination limit = default); + Task> GetFromCollection(string slug, + [Optional] Expression> where, + Expression> sort, + Pagination limit = default + ) => GetFromCollection(slug, where, new Sort(sort), limit); } public interface ISeasonRepository : IRepository @@ -255,6 +255,26 @@ namespace Kyoo.Controllers Expression> sort, Pagination limit = default ) => GetFromShow(showSlug, where, new Sort(sort), limit); + + Task> GetFromCollection(int id, + Expression> where = null, + Sort sort = default, + Pagination limit = default); + Task> GetFromCollection(int id, + [Optional] Expression> where, + Expression> sort, + Pagination limit = default + ) => GetFromCollection(id, where, new Sort(sort), limit); + + Task> GetFromCollection(string slug, + Expression> where = null, + Sort sort = default, + Pagination limit = default); + Task> GetFromCollection(string slug, + [Optional] Expression> where, + Expression> sort, + Pagination limit = default + ) => GetFromCollection(slug, where, new Sort(sort), limit); } public interface ILibraryItemRepository : IRepository diff --git a/Kyoo.Common/Controllers/Implementations/LibraryManager.cs b/Kyoo.Common/Controllers/Implementations/LibraryManager.cs index fd6c2e79..82f0a8b3 100644 --- a/Kyoo.Common/Controllers/Implementations/LibraryManager.cs +++ b/Kyoo.Common/Controllers/Implementations/LibraryManager.cs @@ -380,6 +380,38 @@ namespace Kyoo.Controllers return LibraryItemRepository.GetFromLibrary(librarySlug, where, sort, limit); } + public Task> GetShowsFromCollection(int id, + Expression> where = null, + Sort sort = default, + Pagination limit = default) + { + return ShowRepository.GetFromCollection(id, where, sort, limit); + } + + public Task> GetShowsFromCollection(string slug, + Expression> where = null, + Sort sort = default, + Pagination limit = default) + { + return ShowRepository.GetFromCollection(slug, where, sort, limit); + } + + public Task> GetLibrariesFromCollection(int id, + Expression> where = null, + Sort sort = default, + Pagination limit = default) + { + return LibraryRepository.GetFromCollection(id, where, sort, limit); + } + + public Task> GetLibrariesFromCollection(string slug, + Expression> where = null, + Sort sort = default, + Pagination limit = default) + { + return LibraryRepository.GetFromCollection(slug, where, sort, limit); + } + public Task AddShowLink(int showID, int? libraryID, int? collectionID) { return ShowRepository.AddShowLink(showID, libraryID, collectionID); diff --git a/Kyoo/Controllers/Repositories/LibraryRepository.cs b/Kyoo/Controllers/Repositories/LibraryRepository.cs index da1dfd84..db39e22d 100644 --- a/Kyoo/Controllers/Repositories/LibraryRepository.cs +++ b/Kyoo/Controllers/Repositories/LibraryRepository.cs @@ -125,5 +125,37 @@ namespace Kyoo.Controllers throw new ItemNotFound(); return libraries; } + + public async Task> GetFromCollection(int id, + Expression> where = null, + Sort sort = default, + Pagination limit = default) + { + ICollection libraries = await ApplyFilters(_database.LibraryLinks + .Where(x => x.CollectionID == id) + .Select(x => x.Library), + where, + sort, + limit); + if (!libraries.Any() && await _shows.Value.Get(id) == null) + throw new ItemNotFound(); + return libraries; + } + + public async Task> GetFromCollection(string slug, + Expression> where = null, + Sort sort = default, + Pagination limit = default) + { + ICollection libraries = await ApplyFilters(_database.LibraryLinks + .Where(x => x.Collection.Slug == slug) + .Select(x => x.Library), + where, + sort, + limit); + if (!libraries.Any() && await _shows.Value.Get(slug) == null) + throw new ItemNotFound(); + return libraries; + } } } \ No newline at end of file diff --git a/Kyoo/Controllers/Repositories/ShowRepository.cs b/Kyoo/Controllers/Repositories/ShowRepository.cs index 77b18b56..36e285a6 100644 --- a/Kyoo/Controllers/Repositories/ShowRepository.cs +++ b/Kyoo/Controllers/Repositories/ShowRepository.cs @@ -211,5 +211,37 @@ namespace Kyoo.Controllers throw new ItemNotFound(); return shows; } + + public async Task> GetFromCollection(int id, + Expression> where = null, + Sort sort = default, + Pagination limit = default) + { + ICollection shows = await ApplyFilters(_database.CollectionLinks + .Where(x => x.CollectionID== id) + .Select(x => x.Show), + where, + sort, + limit); + if (!shows.Any() && await _libraries.Value.Get(id) == null) + throw new ItemNotFound(); + return shows; + } + + public async Task> GetFromCollection(string slug, + Expression> where = null, + Sort sort = default, + Pagination limit = default) + { + ICollection shows = await ApplyFilters(_database.CollectionLinks + .Where(x => x.Collection.Slug == slug) + .Select(x => x.Show), + where, + sort, + limit); + if (!shows.Any() && await _libraries.Value.Get(slug) == null) + throw new ItemNotFound(); + return shows; + } } } \ No newline at end of file diff --git a/Kyoo/Views/API/CollectionAPI.cs b/Kyoo/Views/API/CollectionAPI.cs deleted file mode 100644 index c4223e20..00000000 --- a/Kyoo/Views/API/CollectionAPI.cs +++ /dev/null @@ -1,20 +0,0 @@ -using Kyoo.Controllers; -using Kyoo.Models; -using Microsoft.AspNetCore.Mvc; -using System.Threading.Tasks; -using Kyoo.CommonApi; -using Microsoft.AspNetCore.Authorization; -using Microsoft.Extensions.Configuration; - -namespace Kyoo.Api -{ - [Route("api/collection")] - [Route("api/collections")] - [ApiController] - public class CollectionApi : CrudApi - { - public CollectionApi(ICollectionRepository repository, IConfiguration configuration) - : base(repository, configuration) - { } - } -} \ No newline at end of file diff --git a/Kyoo/Views/API/CollectionApi.cs b/Kyoo/Views/API/CollectionApi.cs new file mode 100644 index 00000000..bb9675d2 --- /dev/null +++ b/Kyoo/Views/API/CollectionApi.cs @@ -0,0 +1,155 @@ +using System; +using System.Collections.Generic; +using Kyoo.Controllers; +using Kyoo.Models; +using Microsoft.AspNetCore.Mvc; +using System.Threading.Tasks; +using Kyoo.CommonApi; +using Kyoo.Models.Exceptions; +using Microsoft.AspNetCore.Authorization; +using Microsoft.Extensions.Configuration; + +namespace Kyoo.Api +{ + [Route("api/collection")] + [Route("api/collections")] + [ApiController] + public class CollectionApi : CrudApi + { + private readonly ILibraryManager _libraryManager; + + public CollectionApi(ILibraryManager libraryManager, IConfiguration configuration) + : base(libraryManager.CollectionRepository, configuration) + { + _libraryManager = libraryManager; + } + + [HttpGet("{id:int}/show")] + [HttpGet("{id:int}/shows")] + [Authorize(Policy = "Read")] + public async Task>> GetShows(int id, + [FromQuery] string sortBy, + [FromQuery] int afterID, + [FromQuery] Dictionary where, + [FromQuery] int limit = 30) + { + where.Remove("sortBy"); + where.Remove("limit"); + where.Remove("afterID"); + + try + { + ICollection ressources = await _libraryManager.GetShowsFromCollection(id, + ApiHelper.ParseWhere(where), + new Sort(sortBy), + new Pagination(limit, afterID)); + + return Page(ressources, limit); + } + catch (ItemNotFound) + { + return NotFound(); + } + catch (ArgumentException ex) + { + return BadRequest(new {Error = ex.Message}); + } + } + + [HttpGet("{slug}/show")] + [HttpGet("{slug}/shows")] + [Authorize(Policy = "Read")] + public async Task>> GetShows(string slug, + [FromQuery] string sortBy, + [FromQuery] int afterID, + [FromQuery] Dictionary where, + [FromQuery] int limit = 30) + { + where.Remove("sortBy"); + where.Remove("limit"); + where.Remove("afterID"); + + try + { + ICollection ressources = await _libraryManager.GetShowsFromCollection(slug, + ApiHelper.ParseWhere(where), + new Sort(sortBy), + new Pagination(limit, afterID)); + + return Page(ressources, limit); + } + catch (ItemNotFound) + { + return NotFound(); + } + catch (ArgumentException ex) + { + return BadRequest(new {Error = ex.Message}); + } + } + + [HttpGet("{id:int}/library")] + [HttpGet("{id:int}/libraries")] + [Authorize(Policy = "Read")] + public async Task>> GetLibraries(int id, + [FromQuery] string sortBy, + [FromQuery] int afterID, + [FromQuery] Dictionary where, + [FromQuery] int limit = 30) + { + where.Remove("sortBy"); + where.Remove("limit"); + where.Remove("afterID"); + + try + { + ICollection ressources = await _libraryManager.GetLibrariesFromCollection(id, + ApiHelper.ParseWhere(where), + new Sort(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>> GetLibraries(string slug, + [FromQuery] string sortBy, + [FromQuery] int afterID, + [FromQuery] Dictionary where, + [FromQuery] int limit = 30) + { + where.Remove("sortBy"); + where.Remove("limit"); + where.Remove("afterID"); + + try + { + ICollection ressources = await _libraryManager.GetLibrariesFromCollection(slug, + ApiHelper.ParseWhere(where), + new Sort(sortBy), + new Pagination(limit, afterID)); + + return Page(ressources, limit); + } + catch (ItemNotFound) + { + return NotFound(); + } + catch (ArgumentException ex) + { + return BadRequest(new {Error = ex.Message}); + } + } + } +} \ No newline at end of file diff --git a/Kyoo/Views/WebClient b/Kyoo/Views/WebClient index db058928..de1b3b06 160000 --- a/Kyoo/Views/WebClient +++ b/Kyoo/Views/WebClient @@ -1 +1 @@ -Subproject commit db0589285c0790c0f2a2f5beb98fb37303256ec3 +Subproject commit de1b3b069aa4f920bd5248223eda151b1eedf541