Fix fields serialization

This commit is contained in:
Zoe Roux 2023-10-27 20:45:03 +02:00
parent c7db07f7ba
commit 86427cf6ef
7 changed files with 23 additions and 20 deletions

View File

@ -17,12 +17,11 @@
// along with Kyoo. If not, see <https://www.gnu.org/licenses/>.
using System;
using Kyoo.Abstractions.Controllers;
namespace Kyoo.Abstractions.Models.Attributes
{
/// <summary>
/// The targeted relation can be loaded via a call to <see cref="ILibraryManager.Load"/>.
/// The targeted relation can be loaded.
/// </summary>
[AttributeUsage(AttributeTargets.Property)]
public class LoadableRelationAttribute : Attribute

View File

@ -84,7 +84,7 @@ namespace Kyoo.Abstractions.Models
[SerializeIgnore] public int ShowId { get; set; }
/// <summary>
/// The show that contains this episode. This must be explicitly loaded via a call to <see cref="ILibraryManager.Load"/>.
/// The show that contains this episode.
/// </summary>
[LoadableRelation(nameof(ShowId))] public Show? Show { get; set; }
@ -95,7 +95,6 @@ namespace Kyoo.Abstractions.Models
/// <summary>
/// The season that contains this episode.
/// This must be explicitly loaded via a call to <see cref="ILibraryManager.Load"/>.
/// </summary>
/// <remarks>
/// This can be null if the season is unknown and the episode is only identified

View File

@ -25,8 +25,15 @@ using Kyoo.Abstractions.Models.Attributes;
namespace Kyoo.Abstractions.Models.Utils;
/// <summary>
/// The aditional fields to include in the result.
/// </summary>
/// <typeparam name="T">The type related to the new fields</typeparam>
public class Include<T>
{
/// <summary>
/// The aditional fields to include in the result.
/// </summary>
public ICollection<string> Fields { get; private init; } = ArraySegment<string>.Empty;
public static Include<T> From(string? fields)

View File

@ -129,18 +129,18 @@ namespace Kyoo.Core.Controllers
// Sort<PeopleRole>? sort = default,
// Pagination? limit = default)
// {
// ICollection<PeopleRole> people = await ApplyFilters(_database.PeopleRoles
// .Where(x => x.ShowID == showID)
// .Include(x => x.People),
// x => x.People.Name,
// where,
// sort,
// limit);
// if (!people.Any() && await _shows.Value.GetOrDefault(showID) == null)
// throw new ItemNotFoundException();
// foreach (PeopleRole role in people)
// role.ForPeople = true;
// return people;
// ICollection<PeopleRole> people = await ApplyFilters(_database.PeopleRoles
// .Where(x => x.ShowID == showID)
// .Include(x => x.People),
// x => x.People.Name,
// where,
// sort,
// limit);
// if (!people.Any() && await _shows.Value.GetOrDefault(showID) == null)
// throw new ItemNotFoundException();
// foreach (PeopleRole role in people)
// role.ForPeople = true;
// return people;
// }
// /// <inheritdoc />

View File

@ -37,6 +37,7 @@ public class IncludeBinder : IModelBinder
object include = bindingContext.ModelType.GetMethod(nameof(Include<object>.From))!
.Invoke(null, new object?[] { fields.FirstValue })!;
bindingContext.Result = ModelBindingResult.Success(include);
bindingContext.HttpContext.Items["fields"] = ((dynamic)include).Fields;
return Task.CompletedTask;
}
catch (TargetInvocationException ex)

View File

@ -58,9 +58,6 @@ namespace Kyoo.Core.Api
{
property.ShouldSerialize = _ =>
{
string resType = (string)_httpContextAccessor.HttpContext!.Items["ResourceType"]!;
if (member.DeclaringType!.Name != resType)
return false;
ICollection<string> fields = (ICollection<string>)_httpContextAccessor.HttpContext!.Items["fields"]!;
return fields.Contains(member.Name);
};

View File

@ -82,7 +82,7 @@ namespace Kyoo.Tests.Database
}
public IRepository<T> GetRepository<T>()
where T: class, IResource
where T : class, IResource
{
return _repositories.First(x => x.RepositoryType == typeof(T)) as IRepository<T>;
}