Reworking People's Role

This commit is contained in:
Zoe Roux 2021-02-28 23:41:06 +01:00
parent 00bf834a29
commit 072dd7d9b8
9 changed files with 83 additions and 176 deletions

View File

@ -117,25 +117,25 @@ namespace Kyoo.Controllers
) => GetPeopleFromShow(showSlug, where, new Sort<PeopleRole>(sort), limit);
// Show Role relations
Task<ICollection<ShowRole>> GetRolesFromPeople(int showID,
Expression<Func<ShowRole, bool>> where = null,
Sort<ShowRole> sort = default,
Task<ICollection<PeopleRole>> GetRolesFromPeople(int showID,
Expression<Func<PeopleRole, bool>> where = null,
Sort<PeopleRole> sort = default,
Pagination limit = default);
Task<ICollection<ShowRole>> GetRolesFromPeople(int showID,
[Optional] Expression<Func<ShowRole, bool>> where,
Expression<Func<ShowRole, object>> sort,
Task<ICollection<PeopleRole>> GetRolesFromPeople(int showID,
[Optional] Expression<Func<PeopleRole, bool>> where,
Expression<Func<PeopleRole, object>> sort,
Pagination limit = default
) => GetRolesFromPeople(showID, where, new Sort<ShowRole>(sort), limit);
) => GetRolesFromPeople(showID, where, new Sort<PeopleRole>(sort), limit);
Task<ICollection<ShowRole>> GetRolesFromPeople(string showSlug,
Expression<Func<ShowRole, bool>> where = null,
Sort<ShowRole> sort = default,
Task<ICollection<PeopleRole>> GetRolesFromPeople(string showSlug,
Expression<Func<PeopleRole, bool>> where = null,
Sort<PeopleRole> sort = default,
Pagination limit = default);
Task<ICollection<ShowRole>> GetRolesFromPeople(string showSlug,
[Optional] Expression<Func<ShowRole, bool>> where,
Expression<Func<ShowRole, object>> sort,
Task<ICollection<PeopleRole>> GetRolesFromPeople(string showSlug,
[Optional] Expression<Func<PeopleRole, bool>> where,
Expression<Func<PeopleRole, object>> sort,
Pagination limit = default
) => GetRolesFromPeople(showSlug, where, new Sort<ShowRole>(sort), limit);
) => GetRolesFromPeople(showSlug, where, new Sort<PeopleRole>(sort), limit);
// Helpers
Task AddShowLink(int showID, int? libraryID, int? collectionID);

View File

@ -186,25 +186,25 @@ namespace Kyoo.Controllers
Pagination limit = default
) => GetFromShow(showSlug, where, new Sort<PeopleRole>(sort), limit);
Task<ICollection<ShowRole>> GetFromPeople(int showID,
Expression<Func<ShowRole, bool>> where = null,
Sort<ShowRole> sort = default,
Task<ICollection<PeopleRole>> GetFromPeople(int showID,
Expression<Func<PeopleRole, bool>> where = null,
Sort<PeopleRole> sort = default,
Pagination limit = default);
Task<ICollection<ShowRole>> GetFromPeople(int showID,
[Optional] Expression<Func<ShowRole, bool>> where,
Expression<Func<ShowRole, object>> sort,
Task<ICollection<PeopleRole>> GetFromPeople(int showID,
[Optional] Expression<Func<PeopleRole, bool>> where,
Expression<Func<PeopleRole, object>> sort,
Pagination limit = default
) => GetFromPeople(showID, where, new Sort<ShowRole>(sort), limit);
) => GetFromPeople(showID, where, new Sort<PeopleRole>(sort), limit);
Task<ICollection<ShowRole>> GetFromPeople(string showSlug,
Expression<Func<ShowRole, bool>> where = null,
Sort<ShowRole> sort = default,
Task<ICollection<PeopleRole>> GetFromPeople(string showSlug,
Expression<Func<PeopleRole, bool>> where = null,
Sort<PeopleRole> sort = default,
Pagination limit = default);
Task<ICollection<ShowRole>> GetFromPeople(string showSlug,
[Optional] Expression<Func<ShowRole, bool>> where,
Expression<Func<ShowRole, object>> sort,
Task<ICollection<PeopleRole>> GetFromPeople(string showSlug,
[Optional] Expression<Func<PeopleRole, bool>> where,
Expression<Func<PeopleRole, object>> sort,
Pagination limit = default
) => GetFromPeople(showSlug, where, new Sort<ShowRole>(sort), limit);
) => GetFromPeople(showSlug, where, new Sort<PeopleRole>(sort), limit);
}
public interface IProviderRepository : IRepository<ProviderID>

View File

@ -266,61 +266,58 @@ namespace Kyoo.Controllers
return (obj, member) switch
{
(Library l, nameof(Library.Providers)) => ProviderRepository
.GetAll(x => x.Libraries.Any(y => y.ID == obj.ID || y.Slug == obj.Slug))
.GetAll(x => x.Libraries.Any(y => y.ID == obj.ID))
.Then(x => l.Providers = x),
(Library l, nameof(Library.Shows)) => ShowRepository
.GetAll(x => x.Libraries.Any(y => y.ID == obj.ID || y.Slug == obj.Slug))
.GetAll(x => x.Libraries.Any(y => y.ID == obj.ID))
.Then(x => l.Shows = x),
(Library l, nameof(Library.Collections)) => CollectionRepository
.GetAll(x => x.Libraries.Any(y => y.ID == obj.ID || y.Slug == obj.Slug))
.GetAll(x => x.Libraries.Any(y => y.ID == obj.ID))
.Then(x => l.Collections = x),
(Collection c, nameof(Library.Shows)) => ShowRepository
.GetAll(x => x.Collections.Any(y => y.ID == obj.ID || y.Slug == obj.Slug))
.GetAll(x => x.Collections.Any(y => y.ID == obj.ID))
.Then(x => c.Shows = x),
(Collection c, nameof(Collection.Libraries)) => LibraryRepository
.GetAll(x => x.Collections.Any(y => y.ID == obj.ID || y.Slug == obj.Slug))
.GetAll(x => x.Collections.Any(y => y.ID == obj.ID))
.Then(x => c.Libraries = x),
(Show s, nameof(Show.ExternalIDs)) => ProviderRepository
.GetMetadataID(x => x.ShowID == obj.ID)
.Then(x => s.ExternalIDs = x),
(Show s, nameof(Show.Genres)) => GenreRepository
.GetAll(x => x.Shows.Any(y => y.ID == obj.ID || y.Slug == obj.Slug))
.GetAll(x => x.Shows.Any(y => y.ID == obj.ID))
.Then(x => s.Genres = x),
(Show s, nameof(Show.People)) => (
PeopleRepository.GetFromShow((dynamic)(obj.ID > 0 ? obj.ID : obj.Slug))
as Task<ICollection<PeopleRole>>)
(Show s, nameof(Show.People)) => PeopleRepository
.GetFromShow(obj.ID)
.Then(x => s.People = x),
(Show s, nameof(Show.Seasons)) => SeasonRepository
.GetAll(x => x.Show.ID == obj.ID || x.Show.Slug == obj.Slug)
.GetAll(x => x.Show.ID == obj.ID)
.Then(x => s.Seasons = x),
(Show s, nameof(Show.Episodes)) => EpisodeRepository
.GetAll(x => x.Show.ID == obj.ID || x.Show.Slug == obj.Slug)
.GetAll(x => x.Show.ID == obj.ID)
.Then(x => s.Episodes = x),
(Show s, nameof(Show.Libraries)) => LibraryRepository
.GetAll(x => x.Shows.Any(y => y.ID == obj.ID || y.Slug == obj.Slug))
.GetAll(x => x.Shows.Any(y => y.ID == obj.ID))
.Then(x => s.Libraries = x),
(Show s, nameof(Show.Collections)) => CollectionRepository
.GetAll(x => x.Shows.Any(y => y.ID == obj.ID || y.Slug == obj.Slug))
.GetAll(x => x.Shows.Any(y => y.ID == obj.ID))
.Then(x => s.Collections = x),
// TODO Studio loading does not work.
(Show s, nameof(Show.Studio)) => StudioRepository
.Get(x => x.Shows.Any(y => y.ID == obj.ID || y.Slug == obj.Slug))
.Get(x => x.Shows.Any(y => y.ID == obj.ID))
.Then(x => s.Studio = x),
(Season s, nameof(Season.ExternalIDs)) => ProviderRepository
.GetMetadataID(x => x.SeasonID == obj.ID)
.Then(x => s.ExternalIDs = x),
(Season s, nameof(Season.Episodes)) => EpisodeRepository
.GetAll(x => x.Season.ID == obj.ID || x.Season.Slug == obj.Slug)
.GetAll(x => x.Season.ID == obj.ID)
.Then(x => s.Episodes = x),
(Season s, nameof(Season.Show)) => ShowRepository
.Get(x => x.Seasons
.Any(y => y.ID == obj.ID || y.ShowID == s.ShowID && y.SeasonNumber == s.SeasonNumber))
.Get(x => x.Seasons.Any(y => y.ID == obj.ID))
.Then(x => s.Show = x),
// TODO maybe add slug support for episodes
(Episode e, nameof(Episode.ExternalIDs)) => ProviderRepository
.GetMetadataID(x => x.EpisodeID == obj.ID)
.Then(x => e.ExternalIDs = x),
@ -339,19 +336,19 @@ namespace Kyoo.Controllers
.Then(x => t.Episode = x),
(Genre g, nameof(Genre.Shows)) => ShowRepository
.GetAll(x => x.Genres.Any(y => y.ID == obj.ID || y.Slug == obj.Slug))
.GetAll(x => x.Genres.Any(y => y.ID == obj.ID))
.Then(x => g.Shows = x),
(Studio s, nameof(Studio.Shows)) => ShowRepository
.GetAll(x => x.Studio.ID == obj.ID || x.Studio.Slug == obj.Slug)
.GetAll(x => x.Studio.ID == obj.ID)
.Then(x => s.Shows = x),
(People p, nameof(People.ExternalIDs)) => ProviderRepository
.GetMetadataID(x => x.PeopleID == obj.ID)
.Then(x => p.ExternalIDs = x),
(People p, nameof(People.Roles)) => (
PeopleRepository.GetFromPeople((dynamic)(obj.ID > 0 ? obj.ID : obj.Slug))
as Task<ICollection<PeopleRole>>).Then(x => p.Roles = x),
(People p, nameof(People.Roles)) => PeopleRepository
.GetFromPeople(obj.ID)
.Then(x => p.Roles = x),
_ => throw new ArgumentException($"Couldn't find a way to load {member} of {obj.Slug}.")
};
@ -459,17 +456,17 @@ namespace Kyoo.Controllers
return PeopleRepository.GetFromShow(showSlug, where, sort, limit);
}
public Task<ICollection<ShowRole>> GetRolesFromPeople(int id,
Expression<Func<ShowRole, bool>> where = null,
Sort<ShowRole> sort = default,
public Task<ICollection<PeopleRole>> GetRolesFromPeople(int id,
Expression<Func<PeopleRole, bool>> where = null,
Sort<PeopleRole> sort = default,
Pagination limit = default)
{
return PeopleRepository.GetFromPeople(id, where, sort, limit);
}
public Task<ICollection<ShowRole>> GetRolesFromPeople(string slug,
Expression<Func<ShowRole, bool>> where = null,
Sort<ShowRole> sort = default,
public Task<ICollection<PeopleRole>> GetRolesFromPeople(string slug,
Expression<Func<PeopleRole, bool>> where = null,
Sort<PeopleRole> sort = default,
Pagination limit = default)
{
return PeopleRepository.GetFromPeople(slug, where, sort, limit);

View File

@ -9,37 +9,11 @@ namespace Kyoo.Models
public class PeopleRole : IResource
{
[SerializeIgnore] public int ID { get; set; }
[SerializeIgnore] public string Slug => ForPeople ? Show.Slug : People.Slug;
[SerializeIgnore] public bool ForPeople;
[SerializeIgnore] public int PeopleID { get; set; }
// TODO implement a SerializeInline for People or Show depending on the context.
[SerializeIgnore] public virtual People People { get; set; }
[ExpressionRewrite(nameof(People) + "." + nameof(Models.People.Slug))]
public string Slug
{
get => People.Slug;
set => People.Slug = value;
}
[ExpressionRewrite(nameof(People) + "."+ nameof(Models.People.Name))]
public string Name
{
get => People.Name;
set => People.Name = value;
}
[ExpressionRewrite(nameof(People) + "."+ nameof(Models.People.Poster))]
public string Poster
{
get => People.Poster;
set => People.Poster = value;
}
[ExpressionRewrite(nameof(People) + "."+ nameof(Models.People.ExternalIDs))]
public ICollection<MetadataID> ExternalIDs
{
get => People.ExternalIDs;
set => People.ExternalIDs = value;
}
[SerializeIgnore] public int ShowID { get; set; }
[SerializeIgnore] public virtual Show Show { get; set; }
public string Role { get; set; }
@ -67,67 +41,4 @@ namespace Kyoo.Models
Type = type;
}
}
public class ShowRole : IResource
{
public int ID { get; set; }
public string Role { get; set; }
public string Type { get; set; }
public string Slug { get; set; }
public string Title { get; set; }
public ICollection<string> Aliases { get; set; }
[SerializeIgnore] public string Path { get; set; }
public string Overview { get; set; }
public Status? Status { get; set; }
public string TrailerUrl { get; set; }
public int? StartYear { get; set; }
public int? EndYear { get; set; }
public string Poster { get; set; }
public string Logo { get; set; }
public string Backdrop { get; set; }
public bool IsMovie { get; set; }
public ShowRole() {}
public ShowRole(PeopleRole x)
{
ID = x.ID;
Role = x.Role;
Type = x.Type;
Slug = x.Show.Slug;
Title = x.Show.Title;
Aliases = x.Show.Aliases?.ToArray();
Path = x.Show.Path;
Overview = x.Show.Overview;
Status = x.Show.Status;
TrailerUrl = x.Show.TrailerUrl;
StartYear = x.Show.StartYear;
EndYear = x.Show.EndYear;
Poster = x.Show.Poster;
Logo = x.Show.Logo;
Backdrop = x.Show.Backdrop;
IsMovie = x.Show.IsMovie;
}
public static Expression<Func<PeopleRole, ShowRole>> FromPeopleRole => x => new ShowRole
{
ID = x.ID,
Role = x.Role,
Type = x.Type,
Slug = x.Show.Slug,
Title = x.Show.Title,
Aliases = x.Show.Aliases != null ? x.Show.Aliases.ToArray() : null,
Path = x.Show.Path,
Overview = x.Show.Overview,
Status = x.Show.Status,
TrailerUrl = x.Show.TrailerUrl,
StartYear = x.Show.StartYear,
EndYear = x.Show.EndYear,
Poster = x.Show.Poster,
Logo = x.Show.Logo,
Backdrop = x.Show.Backdrop,
IsMovie = x.Show.IsMovie
};
}
}

View File

@ -126,37 +126,39 @@ namespace Kyoo.Controllers
return people;
}
public async Task<ICollection<ShowRole>> GetFromPeople(int peopleID,
Expression<Func<ShowRole, bool>> where = null,
Sort<ShowRole> sort = default,
public async Task<ICollection<PeopleRole>> GetFromPeople(int peopleID,
Expression<Func<PeopleRole, bool>> where = null,
Sort<PeopleRole> sort = default,
Pagination limit = default)
{
ICollection<ShowRole> roles = await ApplyFilters(_database.PeopleRoles.Where(x => x.PeopleID == peopleID)
.Select(ShowRole.FromPeopleRole),
async id => new ShowRole(await _database.PeopleRoles.FirstOrDefaultAsync(x => x.ID == id)),
x => x.Title,
ICollection<PeopleRole> roles = await ApplyFilters(_database.PeopleRoles.Where(x => x.PeopleID == peopleID),
id => _database.PeopleRoles.FirstOrDefaultAsync(x => x.ID == id),
x => x.Show.Title,
where,
sort,
limit);
if (!roles.Any() && await Get(peopleID) == null)
throw new ItemNotFound();
foreach (PeopleRole role in roles)
role.ForPeople = true;
return roles;
}
public async Task<ICollection<ShowRole>> GetFromPeople(string slug,
Expression<Func<ShowRole, bool>> where = null,
Sort<ShowRole> sort = default,
public async Task<ICollection<PeopleRole>> GetFromPeople(string slug,
Expression<Func<PeopleRole, bool>> where = null,
Sort<PeopleRole> sort = default,
Pagination limit = default)
{
ICollection<ShowRole> roles = await ApplyFilters(_database.PeopleRoles.Where(x => x.People.Slug == slug)
.Select(ShowRole.FromPeopleRole),
async id => new ShowRole(await _database.PeopleRoles.FirstOrDefaultAsync(x => x.ID == id)),
x => x.Title,
ICollection<PeopleRole> roles = await ApplyFilters(_database.PeopleRoles.Where(x => x.People.Slug == slug),
id => _database.PeopleRoles.FirstOrDefaultAsync(x => x.ID == id),
x => x.Show.Title,
where,
sort,
limit);
if (!roles.Any() && await Get(slug) == null)
throw new ItemNotFound();
foreach (PeopleRole role in roles)
role.ForPeople = true;
return roles;
}
}

View File

@ -98,10 +98,7 @@ namespace Kyoo
.Ignore(x => x.Collections);
modelBuilder.Entity<PeopleRole>()
.Ignore(x => x.Slug)
.Ignore(x => x.Name)
.Ignore(x => x.Poster)
.Ignore(x => x.ExternalIDs);
.Ignore(x => x.Slug);
modelBuilder.Entity<GenreDE>()
.Ignore(x => x.Shows);

View File

@ -10,7 +10,7 @@ using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata;
namespace Kyoo.Models.DatabaseMigrations.Internal
{
[DbContext(typeof(DatabaseContext))]
[Migration("20210228210101_Initial")]
[Migration("20210228224046_Initial")]
partial class Initial
{
protected override void BuildTargetModel(ModelBuilder modelBuilder)

View File

@ -29,7 +29,7 @@ namespace Kyoo.Api
[HttpGet("{id:int}/role")]
[HttpGet("{id:int}/roles")]
[Authorize(Policy = "Read")]
public async Task<ActionResult<Page<ShowRole>>> GetRoles(int id,
public async Task<ActionResult<Page<PeopleRole>>> GetRoles(int id,
[FromQuery] string sortBy,
[FromQuery] int afterID,
[FromQuery] Dictionary<string, string> where,
@ -37,9 +37,9 @@ namespace Kyoo.Api
{
try
{
ICollection<ShowRole> resources = await _libraryManager.GetRolesFromPeople(id,
ApiHelper.ParseWhere<ShowRole>(where),
new Sort<ShowRole>(sortBy),
ICollection<PeopleRole> resources = await _libraryManager.GetRolesFromPeople(id,
ApiHelper.ParseWhere<PeopleRole>(where),
new Sort<PeopleRole>(sortBy),
new Pagination(limit, afterID));
return Page(resources, limit);
@ -57,7 +57,7 @@ namespace Kyoo.Api
[HttpGet("{slug}/role")]
[HttpGet("{slug}/roles")]
[Authorize(Policy = "Read")]
public async Task<ActionResult<Page<ShowRole>>> GetRoles(string slug,
public async Task<ActionResult<Page<PeopleRole>>> GetRoles(string slug,
[FromQuery] string sortBy,
[FromQuery] int afterID,
[FromQuery] Dictionary<string, string> where,
@ -65,9 +65,9 @@ namespace Kyoo.Api
{
try
{
ICollection<ShowRole> resources = await _libraryManager.GetRolesFromPeople(slug,
ApiHelper.ParseWhere<ShowRole>(where),
new Sort<ShowRole>(sortBy),
ICollection<PeopleRole> resources = await _libraryManager.GetRolesFromPeople(slug,
ApiHelper.ParseWhere<PeopleRole>(where),
new Sort<PeopleRole>(sortBy),
new Pagination(limit, afterID));
return Page(resources, limit);