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); ) => GetPeopleFromShow(showSlug, where, new Sort<PeopleRole>(sort), limit);
// Show Role relations // Show Role relations
Task<ICollection<ShowRole>> GetRolesFromPeople(int showID, Task<ICollection<PeopleRole>> GetRolesFromPeople(int showID,
Expression<Func<ShowRole, bool>> where = null, Expression<Func<PeopleRole, bool>> where = null,
Sort<ShowRole> sort = default, Sort<PeopleRole> sort = default,
Pagination limit = default); Pagination limit = default);
Task<ICollection<ShowRole>> GetRolesFromPeople(int showID, Task<ICollection<PeopleRole>> GetRolesFromPeople(int showID,
[Optional] Expression<Func<ShowRole, bool>> where, [Optional] Expression<Func<PeopleRole, bool>> where,
Expression<Func<ShowRole, object>> sort, Expression<Func<PeopleRole, object>> sort,
Pagination limit = default 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, Task<ICollection<PeopleRole>> GetRolesFromPeople(string showSlug,
Expression<Func<ShowRole, bool>> where = null, Expression<Func<PeopleRole, bool>> where = null,
Sort<ShowRole> sort = default, Sort<PeopleRole> sort = default,
Pagination limit = default); Pagination limit = default);
Task<ICollection<ShowRole>> GetRolesFromPeople(string showSlug, Task<ICollection<PeopleRole>> GetRolesFromPeople(string showSlug,
[Optional] Expression<Func<ShowRole, bool>> where, [Optional] Expression<Func<PeopleRole, bool>> where,
Expression<Func<ShowRole, object>> sort, Expression<Func<PeopleRole, object>> sort,
Pagination limit = default Pagination limit = default
) => GetRolesFromPeople(showSlug, where, new Sort<ShowRole>(sort), limit); ) => GetRolesFromPeople(showSlug, where, new Sort<PeopleRole>(sort), limit);
// Helpers // Helpers
Task AddShowLink(int showID, int? libraryID, int? collectionID); Task AddShowLink(int showID, int? libraryID, int? collectionID);

View File

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

View File

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

View File

@ -9,37 +9,11 @@ namespace Kyoo.Models
public class PeopleRole : IResource public class PeopleRole : IResource
{ {
[SerializeIgnore] public int ID { get; set; } [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; } [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; } [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 int ShowID { get; set; }
[SerializeIgnore] public virtual Show Show { get; set; } [SerializeIgnore] public virtual Show Show { get; set; }
public string Role { get; set; } public string Role { get; set; }
@ -67,67 +41,4 @@ namespace Kyoo.Models
Type = type; 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; return people;
} }
public async Task<ICollection<ShowRole>> GetFromPeople(int peopleID, public async Task<ICollection<PeopleRole>> GetFromPeople(int peopleID,
Expression<Func<ShowRole, bool>> where = null, Expression<Func<PeopleRole, bool>> where = null,
Sort<ShowRole> sort = default, Sort<PeopleRole> sort = default,
Pagination limit = default) Pagination limit = default)
{ {
ICollection<ShowRole> roles = await ApplyFilters(_database.PeopleRoles.Where(x => x.PeopleID == peopleID) ICollection<PeopleRole> roles = await ApplyFilters(_database.PeopleRoles.Where(x => x.PeopleID == peopleID),
.Select(ShowRole.FromPeopleRole), id => _database.PeopleRoles.FirstOrDefaultAsync(x => x.ID == id),
async id => new ShowRole(await _database.PeopleRoles.FirstOrDefaultAsync(x => x.ID == id)), x => x.Show.Title,
x => x.Title,
where, where,
sort, sort,
limit); limit);
if (!roles.Any() && await Get(peopleID) == null) if (!roles.Any() && await Get(peopleID) == null)
throw new ItemNotFound(); throw new ItemNotFound();
foreach (PeopleRole role in roles)
role.ForPeople = true;
return roles; return roles;
} }
public async Task<ICollection<ShowRole>> GetFromPeople(string slug, public async Task<ICollection<PeopleRole>> GetFromPeople(string slug,
Expression<Func<ShowRole, bool>> where = null, Expression<Func<PeopleRole, bool>> where = null,
Sort<ShowRole> sort = default, Sort<PeopleRole> sort = default,
Pagination limit = default) Pagination limit = default)
{ {
ICollection<ShowRole> roles = await ApplyFilters(_database.PeopleRoles.Where(x => x.People.Slug == slug) ICollection<PeopleRole> roles = await ApplyFilters(_database.PeopleRoles.Where(x => x.People.Slug == slug),
.Select(ShowRole.FromPeopleRole), id => _database.PeopleRoles.FirstOrDefaultAsync(x => x.ID == id),
async id => new ShowRole(await _database.PeopleRoles.FirstOrDefaultAsync(x => x.ID == id)), x => x.Show.Title,
x => x.Title,
where, where,
sort, sort,
limit); limit);
if (!roles.Any() && await Get(slug) == null) if (!roles.Any() && await Get(slug) == null)
throw new ItemNotFound(); throw new ItemNotFound();
foreach (PeopleRole role in roles)
role.ForPeople = true;
return roles; return roles;
} }
} }

View File

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

View File

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

View File

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