Implementing shows/genres & GetGenresByShow

This commit is contained in:
Zoe Roux 2020-07-25 04:22:27 +02:00
parent b73be95f11
commit fb2d5b24bd
5 changed files with 159 additions and 2 deletions

View File

@ -128,6 +128,26 @@ namespace Kyoo.Controllers
Expression<Func<PeopleLink, object>> sort,
Pagination limit = default
) => GetPeopleFromShow(showSlug, where, new Sort<PeopleLink>(sort), limit);
Task<ICollection<Genre>> GetGenresFromShow(int showID,
Expression<Func<Genre, bool>> where = null,
Sort<Genre> sort = default,
Pagination limit = default);
Task<ICollection<Genre>> GetGenresFromShow(int showID,
[Optional] Expression<Func<Genre, bool>> where,
Expression<Func<Genre, object>> sort,
Pagination limit = default
) => GetGenresFromShow(showID, where, new Sort<Genre>(sort), limit);
Task<ICollection<Genre>> GetGenresFromShow(string showSlug,
Expression<Func<Genre, bool>> where = null,
Sort<Genre> sort = default,
Pagination limit = default);
Task<ICollection<Genre>> GetGenresFromShow(string showSlug,
[Optional] Expression<Func<Genre, bool>> where,
Expression<Func<Genre, object>> sort,
Pagination limit = default
) => GetGenresFromShow(showSlug, where, new Sort<Genre>(sort), limit);
// Helpers

View File

@ -195,7 +195,29 @@ namespace Kyoo.Controllers
}
public interface ILibraryRepository : IRepository<Library> {}
public interface ICollectionRepository : IRepository<Collection> {}
public interface IGenreRepository : IRepository<Genre> {}
public interface IGenreRepository : IRepository<Genre>
{
Task<ICollection<Genre>> GetFromShow(int showID,
Expression<Func<Genre, bool>> where = null,
Sort<Genre> sort = default,
Pagination limit = default);
Task<ICollection<Genre>> GetFromShow(int showID,
[Optional] Expression<Func<Genre, bool>> where,
Expression<Func<Genre, object>> sort,
Pagination limit = default
) => GetFromShow(showID, where, new Sort<Genre>(sort), limit);
Task<ICollection<Genre>> GetFromShow(string showSlug,
Expression<Func<Genre, bool>> where = null,
Sort<Genre> sort = default,
Pagination limit = default);
Task<ICollection<Genre>> GetFromShow(string showSlug,
[Optional] Expression<Func<Genre, bool>> where,
Expression<Func<Genre, object>> sort,
Pagination limit = default
) => GetFromShow(showSlug, where, new Sort<Genre>(sort), limit);
}
public interface IStudioRepository : IRepository<Studio> {}
public interface IPeopleRepository : IRepository<People>

View File

@ -271,6 +271,22 @@ namespace Kyoo.Controllers
return PeopleRepository.GetFromShow(showSlug, where, sort, limit);
}
public Task<ICollection<Genre>> GetGenresFromShow(int showID,
Expression<Func<Genre, bool>> where = null,
Sort<Genre> sort = default,
Pagination limit = default)
{
return GenreRepository.GetFromShow(showID, where, sort, limit);
}
public Task<ICollection<Genre>> GetGenresFromShow(string showSlug,
Expression<Func<Genre, bool>> where = null,
Sort<Genre> sort = default,
Pagination limit = default)
{
return GenreRepository.GetFromShow(showSlug, where, sort, limit);
}
public Task AddShowLink(int showID, int? libraryID, int? collectionID)
{
return ShowRepository.AddShowLink(showID, libraryID, collectionID);

View File

@ -6,18 +6,21 @@ using System.Threading.Tasks;
using Kyoo.Models;
using Kyoo.Models.Exceptions;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
namespace Kyoo.Controllers
{
public class GenreRepository : LocalRepository<Genre>, IGenreRepository
{
private readonly DatabaseContext _database;
private readonly Lazy<IShowRepository> _shows;
protected override Expression<Func<Genre, object>> DefaultSort => x => x.Slug;
public GenreRepository(DatabaseContext database) : base(database)
public GenreRepository(DatabaseContext database, IServiceProvider services) : base(database)
{
_database = database;
_shows = new Lazy<IShowRepository>(services.GetRequiredService<IShowRepository>);
}
@ -68,5 +71,35 @@ namespace Kyoo.Controllers
_database.Entry(link).State = EntityState.Deleted;
await _database.SaveChangesAsync();
}
public async Task<ICollection<Genre>> GetFromShow(int showID,
Expression<Func<Genre, bool>> where = null,
Sort<Genre> sort = default,
Pagination limit = default)
{
ICollection<Genre> genres = await ApplyFilters(_database.GenreLinks.Where(x => x.ShowID == showID)
.Select(x => x.Genre),
where,
sort,
limit);
if (!genres.Any() && await _shows.Value.Get(showID) == null)
throw new ItemNotFound();
return genres;
}
public async Task<ICollection<Genre>> GetFromShow(string showSlug,
Expression<Func<Genre, bool>> where = null,
Sort<Genre> sort = default,
Pagination limit = default)
{
ICollection<Genre> genres = await ApplyFilters(_database.GenreLinks.Where(x => x.Show.Slug == showSlug)
.Select(x => x.Genre),
where,
sort,
limit);
if (!genres.Any() && await _shows.Value.Get(showSlug) == null)
throw new ItemNotFound();
return genres;
}
}
}

View File

@ -220,5 +220,71 @@ namespace Kyoo.Api
return BadRequest(new {Error = ex.Message});
}
}
[HttpGet("{showID:int}/genre")]
[HttpGet("{showID:int}/genres")]
[Authorize(Policy = "Read")]
public async Task<ActionResult<Page<Genre>>> GetGenres(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<Genre> ressources = await _libraryManager.GetGenresFromShow(showID,
ApiHelper.ParseWhere<Genre>(where),
new Sort<Genre>(sortBy),
new Pagination(limit, afterID));
return Page(ressources, limit);
}
catch (ItemNotFound)
{
return NotFound();
}
catch (ArgumentException ex)
{
return BadRequest(new {Error = ex.Message});
}
}
[HttpGet("{slug}/genre")]
[HttpGet("{slug}/genres")]
[Authorize(Policy = "Read")]
public async Task<ActionResult<Page<Genre>>> GetGenre(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<Genre> ressources = await _libraryManager.GetGenresFromShow(slug,
ApiHelper.ParseWhere<Genre>(where),
new Sort<Genre>(sortBy),
new Pagination(limit, afterID));
return Page(ressources, limit);
}
catch (ItemNotFound)
{
return NotFound();
}
catch (ArgumentException ex)
{
return BadRequest(new {Error = ex.Message});
}
}
}
}