Creating the collection's rest api

This commit is contained in:
Zoe Roux 2020-08-03 17:29:17 +02:00
parent 8af2cff1a2
commit 6a0fb2dd38
8 changed files with 331 additions and 42 deletions

View File

@ -237,7 +237,6 @@ namespace Kyoo.Controllers
Expression<Func<LibraryItem, bool>> where = null,
Sort<LibraryItem> sort = default,
Pagination limit = default);
Task<ICollection<LibraryItem>> GetItemsFromLibrary(int id,
[Optional] Expression<Func<LibraryItem, bool>> where,
Expression<Func<LibraryItem, object>> sort,
@ -248,12 +247,51 @@ namespace Kyoo.Controllers
Expression<Func<LibraryItem, bool>> where = null,
Sort<LibraryItem> sort = default,
Pagination limit = default);
Task<ICollection<LibraryItem>> GetItemsFromLibrary(string librarySlug,
[Optional] Expression<Func<LibraryItem, bool>> where,
Expression<Func<LibraryItem, object>> sort,
Pagination limit = default
) => GetItemsFromLibrary(librarySlug, where, new Sort<LibraryItem>(sort), limit);
Task<ICollection<Show>> GetShowsFromCollection(int id,
Expression<Func<Show, bool>> where = null,
Sort<Show> sort = default,
Pagination limit = default);
Task<ICollection<Show>> GetShowsFromCollection(int id,
[Optional] Expression<Func<Show, bool>> where,
Expression<Func<Show, object>> sort,
Pagination limit = default
) => GetShowsFromCollection(id, where, new Sort<Show>(sort), limit);
Task<ICollection<Show>> GetShowsFromCollection(string slug,
Expression<Func<Show, bool>> where = null,
Sort<Show> sort = default,
Pagination limit = default);
Task<ICollection<Show>> GetShowsFromCollection(string slug,
[Optional] Expression<Func<Show, bool>> where,
Expression<Func<Show, object>> sort,
Pagination limit = default
) => GetShowsFromCollection(slug, where, new Sort<Show>(sort), limit);
Task<ICollection<Library>> GetLibrariesFromCollection(int id,
Expression<Func<Library, bool>> where = null,
Sort<Library> sort = default,
Pagination limit = default);
Task<ICollection<Library>> GetLibrariesFromCollection(int id,
[Optional] Expression<Func<Library, bool>> where,
Expression<Func<Library, object>> sort,
Pagination limit = default
) => GetLibrariesFromCollection(id, where, new Sort<Library>(sort), limit);
Task<ICollection<Library>> GetLibrariesFromCollection(string slug,
Expression<Func<Library, bool>> where = null,
Sort<Library> sort = default,
Pagination limit = default);
Task<ICollection<Library>> GetLibrariesFromCollection(string slug,
[Optional] Expression<Func<Library, bool>> where,
Expression<Func<Library, object>> sort,
Pagination limit = default
) => GetLibrariesFromCollection(slug, where, new Sort<Library>(sort), limit);
// Helpers

View File

@ -123,25 +123,25 @@ namespace Kyoo.Controllers
Pagination limit = default
) => GetFromLibrary(slug, where, new Sort<Show>(sort), limit);
// Task<ICollection<Show>> GetFromCollection(int id,
// Expression<Func<Show, bool>> where = null,
// Sort<Show> sort = default,
// Pagination limit = default);
// Task<ICollection<Show>> GetFromCollection(int id,
// [Optional] Expression<Func<Show, bool>> where,
// Expression<Func<Show, object>> sort,
// Pagination limit = default
// ) => GetFromCollection(id, where, new Sort<Show>(sort), limit);
//
// Task<ICollection<Show>> GetFromCollection(string slug,
// Expression<Func<Show, bool>> where = null,
// Sort<Show> sort = default,
// Pagination limit = default);
// Task<ICollection<Show>> GetFromCollection(string slug,
// [Optional] Expression<Func<Show, bool>> where,
// Expression<Func<Show, object>> sort,
// Pagination limit = default
// ) => GetFromCollection(slug, where, new Sort<Show>(sort), limit);
Task<ICollection<Show>> GetFromCollection(int id,
Expression<Func<Show, bool>> where = null,
Sort<Show> sort = default,
Pagination limit = default);
Task<ICollection<Show>> GetFromCollection(int id,
[Optional] Expression<Func<Show, bool>> where,
Expression<Func<Show, object>> sort,
Pagination limit = default
) => GetFromCollection(id, where, new Sort<Show>(sort), limit);
Task<ICollection<Show>> GetFromCollection(string slug,
Expression<Func<Show, bool>> where = null,
Sort<Show> sort = default,
Pagination limit = default);
Task<ICollection<Show>> GetFromCollection(string slug,
[Optional] Expression<Func<Show, bool>> where,
Expression<Func<Show, object>> sort,
Pagination limit = default
) => GetFromCollection(slug, where, new Sort<Show>(sort), limit);
}
public interface ISeasonRepository : IRepository<Season>
@ -255,6 +255,26 @@ namespace Kyoo.Controllers
Expression<Func<Library, object>> sort,
Pagination limit = default
) => GetFromShow(showSlug, where, new Sort<Library>(sort), limit);
Task<ICollection<Library>> GetFromCollection(int id,
Expression<Func<Library, bool>> where = null,
Sort<Library> sort = default,
Pagination limit = default);
Task<ICollection<Library>> GetFromCollection(int id,
[Optional] Expression<Func<Library, bool>> where,
Expression<Func<Library, object>> sort,
Pagination limit = default
) => GetFromCollection(id, where, new Sort<Library>(sort), limit);
Task<ICollection<Library>> GetFromCollection(string slug,
Expression<Func<Library, bool>> where = null,
Sort<Library> sort = default,
Pagination limit = default);
Task<ICollection<Library>> GetFromCollection(string slug,
[Optional] Expression<Func<Library, bool>> where,
Expression<Func<Library, object>> sort,
Pagination limit = default
) => GetFromCollection(slug, where, new Sort<Library>(sort), limit);
}
public interface ILibraryItemRepository : IRepository<LibraryItem>

View File

@ -380,6 +380,38 @@ namespace Kyoo.Controllers
return LibraryItemRepository.GetFromLibrary(librarySlug, where, sort, limit);
}
public Task<ICollection<Show>> GetShowsFromCollection(int id,
Expression<Func<Show, bool>> where = null,
Sort<Show> sort = default,
Pagination limit = default)
{
return ShowRepository.GetFromCollection(id, where, sort, limit);
}
public Task<ICollection<Show>> GetShowsFromCollection(string slug,
Expression<Func<Show, bool>> where = null,
Sort<Show> sort = default,
Pagination limit = default)
{
return ShowRepository.GetFromCollection(slug, where, sort, limit);
}
public Task<ICollection<Library>> GetLibrariesFromCollection(int id,
Expression<Func<Library, bool>> where = null,
Sort<Library> sort = default,
Pagination limit = default)
{
return LibraryRepository.GetFromCollection(id, where, sort, limit);
}
public Task<ICollection<Library>> GetLibrariesFromCollection(string slug,
Expression<Func<Library, bool>> where = null,
Sort<Library> 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);

View File

@ -125,5 +125,37 @@ namespace Kyoo.Controllers
throw new ItemNotFound();
return libraries;
}
public async Task<ICollection<Library>> GetFromCollection(int id,
Expression<Func<Library, bool>> where = null,
Sort<Library> sort = default,
Pagination limit = default)
{
ICollection<Library> 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<ICollection<Library>> GetFromCollection(string slug,
Expression<Func<Library, bool>> where = null,
Sort<Library> sort = default,
Pagination limit = default)
{
ICollection<Library> 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;
}
}
}

View File

@ -211,5 +211,37 @@ namespace Kyoo.Controllers
throw new ItemNotFound();
return shows;
}
public async Task<ICollection<Show>> GetFromCollection(int id,
Expression<Func<Show, bool>> where = null,
Sort<Show> sort = default,
Pagination limit = default)
{
ICollection<Show> 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<ICollection<Show>> GetFromCollection(string slug,
Expression<Func<Show, bool>> where = null,
Sort<Show> sort = default,
Pagination limit = default)
{
ICollection<Show> 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;
}
}
}

View File

@ -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<Collection>
{
public CollectionApi(ICollectionRepository repository, IConfiguration configuration)
: base(repository, configuration)
{ }
}
}

View File

@ -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<Collection>
{
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<ActionResult<Page<Show>>> GetShows(int id,
[FromQuery] string sortBy,
[FromQuery] int afterID,
[FromQuery] Dictionary<string, string> where,
[FromQuery] int limit = 30)
{
where.Remove("sortBy");
where.Remove("limit");
where.Remove("afterID");
try
{
ICollection<Show> ressources = await _libraryManager.GetShowsFromCollection(id,
ApiHelper.ParseWhere<Show>(where),
new Sort<Show>(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<ActionResult<Page<Show>>> GetShows(string slug,
[FromQuery] string sortBy,
[FromQuery] int afterID,
[FromQuery] Dictionary<string, string> where,
[FromQuery] int limit = 30)
{
where.Remove("sortBy");
where.Remove("limit");
where.Remove("afterID");
try
{
ICollection<Show> ressources = await _libraryManager.GetShowsFromCollection(slug,
ApiHelper.ParseWhere<Show>(where),
new Sort<Show>(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<ActionResult<Page<Library>>> GetLibraries(int id,
[FromQuery] string sortBy,
[FromQuery] int afterID,
[FromQuery] Dictionary<string, string> where,
[FromQuery] int limit = 30)
{
where.Remove("sortBy");
where.Remove("limit");
where.Remove("afterID");
try
{
ICollection<Library> ressources = await _libraryManager.GetLibrariesFromCollection(id,
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("sortBy");
where.Remove("limit");
where.Remove("afterID");
try
{
ICollection<Library> ressources = await _libraryManager.GetLibrariesFromCollection(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});
}
}
}
}

@ -1 +1 @@
Subproject commit db0589285c0790c0f2a2f5beb98fb37303256ec3
Subproject commit de1b3b069aa4f920bd5248223eda151b1eedf541