mirror of
https://github.com/zoriya/Kyoo.git
synced 2025-07-09 03:04:20 -04:00
Fixing one to one relation null's serialization
This commit is contained in:
parent
b907e45a08
commit
6bf6746320
@ -306,10 +306,13 @@ namespace Kyoo.Controllers
|
|||||||
(Show s, nameof(Show.Collections)) => CollectionRepository
|
(Show s, nameof(Show.Collections)) => CollectionRepository
|
||||||
.GetAll(x => x.Shows.Any(y => y.ID == obj.ID))
|
.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.
|
|
||||||
(Show s, nameof(Show.Studio)) => StudioRepository
|
(Show s, nameof(Show.Studio)) => StudioRepository
|
||||||
.Get(x => x.Shows.Any(y => y.ID == obj.ID))
|
.Get(x => x.Shows.Any(y => y.ID == obj.ID))
|
||||||
.Then(x => s.Studio = x),
|
.Then(x =>
|
||||||
|
{
|
||||||
|
s.Studio = x;
|
||||||
|
s.StudioID = x?.ID ?? 0;
|
||||||
|
}),
|
||||||
|
|
||||||
(Season s, nameof(Season.ExternalIDs)) => ProviderRepository
|
(Season s, nameof(Season.ExternalIDs)) => ProviderRepository
|
||||||
.GetMetadataID(x => x.SeasonID == obj.ID)
|
.GetMetadataID(x => x.SeasonID == obj.ID)
|
||||||
@ -319,7 +322,11 @@ namespace Kyoo.Controllers
|
|||||||
.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.Any(y => y.ID == obj.ID))
|
.Get(x => x.Seasons.Any(y => y.ID == obj.ID))
|
||||||
.Then(x => s.Show = x),
|
.Then(x =>
|
||||||
|
{
|
||||||
|
s.Show = x;
|
||||||
|
s.ShowID = x?.ID ?? 0;
|
||||||
|
}),
|
||||||
|
|
||||||
(Episode e, nameof(Episode.ExternalIDs)) => ProviderRepository
|
(Episode e, nameof(Episode.ExternalIDs)) => ProviderRepository
|
||||||
.GetMetadataID(x => x.EpisodeID == obj.ID)
|
.GetMetadataID(x => x.EpisodeID == obj.ID)
|
||||||
@ -329,14 +336,26 @@ namespace Kyoo.Controllers
|
|||||||
.Then(x => e.Tracks = x),
|
.Then(x => e.Tracks = x),
|
||||||
(Episode e, nameof(Episode.Show)) => ShowRepository
|
(Episode e, nameof(Episode.Show)) => ShowRepository
|
||||||
.Get(x => x.Episodes.Any(y => y.ID == obj.ID))
|
.Get(x => x.Episodes.Any(y => y.ID == obj.ID))
|
||||||
.Then(x => e.Show = x),
|
.Then(x =>
|
||||||
|
{
|
||||||
|
e.Show = x;
|
||||||
|
e.ShowID = x?.ID ?? 0;
|
||||||
|
}),
|
||||||
(Episode e, nameof(Episode.Season)) => SeasonRepository
|
(Episode e, nameof(Episode.Season)) => SeasonRepository
|
||||||
.Get(x => x.Episodes.Any(y => y.ID == e.ID))
|
.Get(x => x.Episodes.Any(y => y.ID == e.ID))
|
||||||
.Then(x => e.Season = x),
|
.Then(x =>
|
||||||
|
{
|
||||||
|
e.Season = x;
|
||||||
|
e.SeasonID = x?.ID ?? 0;
|
||||||
|
}),
|
||||||
|
|
||||||
(Track t, nameof(Track.Episode)) => EpisodeRepository
|
(Track t, nameof(Track.Episode)) => EpisodeRepository
|
||||||
.Get(x => x.Tracks.Any(y => y.ID == obj.ID))
|
.Get(x => x.Tracks.Any(y => y.ID == obj.ID))
|
||||||
.Then(x => t.Episode = x),
|
.Then(x =>
|
||||||
|
{
|
||||||
|
t.Episode = x;
|
||||||
|
t.EpisodeID = x?.ID ?? 0;
|
||||||
|
}),
|
||||||
|
|
||||||
(Genre g, nameof(Genre.Shows)) => ShowRepository
|
(Genre g, nameof(Genre.Shows)) => ShowRepository
|
||||||
.GetAll(x => x.Genres.Any(y => y.ID == obj.ID))
|
.GetAll(x => x.Genres.Any(y => y.ID == obj.ID))
|
||||||
|
@ -4,7 +4,17 @@ namespace Kyoo.Models.Attributes
|
|||||||
{
|
{
|
||||||
[AttributeUsage(AttributeTargets.Property, Inherited = false)]
|
[AttributeUsage(AttributeTargets.Property, Inherited = false)]
|
||||||
public class EditableRelationAttribute : Attribute { }
|
public class EditableRelationAttribute : Attribute { }
|
||||||
|
|
||||||
[AttributeUsage(AttributeTargets.Property)]
|
[AttributeUsage(AttributeTargets.Property)]
|
||||||
public class LoadableRelationAttribute : Attribute { }
|
public class LoadableRelationAttribute : Attribute
|
||||||
|
{
|
||||||
|
public string RelationID { get; }
|
||||||
|
|
||||||
|
public LoadableRelationAttribute() {}
|
||||||
|
|
||||||
|
public LoadableRelationAttribute(string relationID)
|
||||||
|
{
|
||||||
|
RelationID = relationID;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
@ -10,10 +10,10 @@ namespace Kyoo.Models
|
|||||||
{
|
{
|
||||||
public int ID { get; set; }
|
public int ID { get; set; }
|
||||||
public string Slug => Show != null ? GetSlug(Show.Slug, SeasonNumber, EpisodeNumber) : ID.ToString();
|
public string Slug => Show != null ? GetSlug(Show.Slug, SeasonNumber, EpisodeNumber) : ID.ToString();
|
||||||
public int ShowID { get; set; }
|
[SerializeIgnore] public int ShowID { get; set; }
|
||||||
[LoadableRelation] public virtual Show Show { get; set; }
|
[LoadableRelation(nameof(ShowID))] public virtual Show Show { get; set; }
|
||||||
public int? SeasonID { get; set; }
|
[SerializeIgnore] public int? SeasonID { get; set; }
|
||||||
[LoadableRelation] public virtual Season Season { get; set; }
|
[LoadableRelation(nameof(SeasonID))] public virtual Season Season { get; set; }
|
||||||
|
|
||||||
public int SeasonNumber { get; set; } = -1;
|
public int SeasonNumber { get; set; } = -1;
|
||||||
public int EpisodeNumber { get; set; } = -1;
|
public int EpisodeNumber { get; set; } = -1;
|
||||||
|
@ -8,7 +8,7 @@ namespace Kyoo.Models
|
|||||||
public class Season : IResource
|
public class Season : IResource
|
||||||
{
|
{
|
||||||
public int ID { get; set; }
|
public int ID { get; set; }
|
||||||
public int ShowID { get; set; }
|
[SerializeIgnore] public int ShowID { get; set; }
|
||||||
|
|
||||||
public int SeasonNumber { get; set; } = -1;
|
public int SeasonNumber { get; set; } = -1;
|
||||||
|
|
||||||
@ -21,7 +21,7 @@ namespace Kyoo.Models
|
|||||||
public string Thumb => $"/api/seasons/{Slug}/thumb";
|
public string Thumb => $"/api/seasons/{Slug}/thumb";
|
||||||
[EditableRelation] [LoadableRelation] public virtual ICollection<MetadataID> ExternalIDs { get; set; }
|
[EditableRelation] [LoadableRelation] public virtual ICollection<MetadataID> ExternalIDs { get; set; }
|
||||||
|
|
||||||
[LoadableRelation] public virtual Show Show { get; set; }
|
[LoadableRelation(nameof(ShowID))] public virtual Show Show { get; set; }
|
||||||
[LoadableRelation] public virtual ICollection<Episode> Episodes { get; set; }
|
[LoadableRelation] public virtual ICollection<Episode> Episodes { get; set; }
|
||||||
|
|
||||||
public Season() { }
|
public Season() { }
|
||||||
|
@ -28,7 +28,7 @@ namespace Kyoo.Models
|
|||||||
|
|
||||||
|
|
||||||
[SerializeIgnore] public int? StudioID { get; set; }
|
[SerializeIgnore] public int? StudioID { get; set; }
|
||||||
[LoadableRelation] [EditableRelation] public virtual Studio Studio { get; set; }
|
[LoadableRelation(nameof(StudioID))] [EditableRelation] public virtual Studio Studio { get; set; }
|
||||||
[LoadableRelation] [EditableRelation] public virtual ICollection<Genre> Genres { get; set; }
|
[LoadableRelation] [EditableRelation] public virtual ICollection<Genre> Genres { get; set; }
|
||||||
[LoadableRelation] [EditableRelation] public virtual ICollection<PeopleRole> People { get; set; }
|
[LoadableRelation] [EditableRelation] public virtual ICollection<PeopleRole> People { get; set; }
|
||||||
[LoadableRelation] public virtual ICollection<Season> Seasons { get; set; }
|
[LoadableRelation] public virtual ICollection<Season> Seasons { get; set; }
|
||||||
|
@ -57,7 +57,7 @@ namespace Kyoo.Models
|
|||||||
public class Track : Stream, IResource
|
public class Track : Stream, IResource
|
||||||
{
|
{
|
||||||
public int ID { get; set; }
|
public int ID { get; set; }
|
||||||
public int EpisodeID { get; set; }
|
[SerializeIgnore] public int EpisodeID { get; set; }
|
||||||
public bool IsDefault
|
public bool IsDefault
|
||||||
{
|
{
|
||||||
get => isDefault;
|
get => isDefault;
|
||||||
@ -114,11 +114,18 @@ namespace Kyoo.Models
|
|||||||
}
|
}
|
||||||
|
|
||||||
public bool IsExternal { get; set; }
|
public bool IsExternal { get; set; }
|
||||||
[LoadableRelation] public virtual Episode Episode { get; set; }
|
[LoadableRelation(nameof(EpisodeID))] public virtual Episode Episode { get; set; }
|
||||||
|
|
||||||
public Track() { }
|
public Track() { }
|
||||||
|
|
||||||
public Track(StreamType type, string title, string language, bool isDefault, bool isForced, string codec, bool isExternal, string path)
|
public Track(StreamType type,
|
||||||
|
string title,
|
||||||
|
string language,
|
||||||
|
bool isDefault,
|
||||||
|
bool isForced,
|
||||||
|
string codec,
|
||||||
|
bool isExternal,
|
||||||
|
string path)
|
||||||
: base(title, language, codec, isDefault, isForced, path, type)
|
: base(title, language, codec, isDefault, isForced, path, type)
|
||||||
{
|
{
|
||||||
IsExternal = isExternal;
|
IsExternal = isExternal;
|
||||||
|
@ -33,6 +33,20 @@ namespace Kyoo
|
|||||||
: ex.Body as MemberExpression;
|
: ex.Body as MemberExpression;
|
||||||
return member!.Member.Name;
|
return member!.Member.Name;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static object GetValue([NotNull] this MemberInfo member, [NotNull] object obj)
|
||||||
|
{
|
||||||
|
if (member == null)
|
||||||
|
throw new ArgumentNullException(nameof(member));
|
||||||
|
if (obj == null)
|
||||||
|
throw new ArgumentNullException(nameof(obj));
|
||||||
|
return member switch
|
||||||
|
{
|
||||||
|
PropertyInfo property => property.GetValue(obj),
|
||||||
|
FieldInfo field => field.GetValue(obj),
|
||||||
|
_ => throw new ArgumentException($"Can't get value of a non property/field (member: {member}).")
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
public static string ToSlug(string str)
|
public static string ToSlug(string str)
|
||||||
{
|
{
|
||||||
|
@ -10,9 +10,21 @@ namespace Kyoo.Controllers
|
|||||||
protected override JsonProperty CreateProperty(MemberInfo member, MemberSerialization memberSerialization)
|
protected override JsonProperty CreateProperty(MemberInfo member, MemberSerialization memberSerialization)
|
||||||
{
|
{
|
||||||
JsonProperty property = base.CreateProperty(member, memberSerialization);
|
JsonProperty property = base.CreateProperty(member, memberSerialization);
|
||||||
|
|
||||||
if (member?.GetCustomAttribute<LoadableRelationAttribute>() != null)
|
LoadableRelationAttribute relation = member?.GetCustomAttribute<LoadableRelationAttribute>();
|
||||||
property.NullValueHandling = NullValueHandling.Ignore;
|
if (relation != null)
|
||||||
|
{
|
||||||
|
if (relation.RelationID == null)
|
||||||
|
property.ShouldSerialize = x => member.GetValue(x) != null;
|
||||||
|
else
|
||||||
|
property.ShouldSerialize = x =>
|
||||||
|
{
|
||||||
|
if (member.GetValue(x) != null)
|
||||||
|
return true;
|
||||||
|
return x.GetType().GetProperty(relation.RelationID)?.GetValue(x) != null;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
if (member?.GetCustomAttribute<SerializeIgnoreAttribute>() != null)
|
if (member?.GetCustomAttribute<SerializeIgnoreAttribute>() != null)
|
||||||
property.ShouldSerialize = _ => false;
|
property.ShouldSerialize = _ => false;
|
||||||
if (member?.GetCustomAttribute<DeserializeIgnoreAttribute>() != null)
|
if (member?.GetCustomAttribute<DeserializeIgnoreAttribute>() != null)
|
||||||
|
@ -75,7 +75,6 @@ namespace Kyoo.CommonApi
|
|||||||
Type pageType = Utility.GetGenericDefinition(result.DeclaredType, typeof(Page<>));
|
Type pageType = Utility.GetGenericDefinition(result.DeclaredType, typeof(Page<>));
|
||||||
|
|
||||||
|
|
||||||
// TODO loading is case sensitive. Maybe convert them in the first check.
|
|
||||||
if (pageType != null)
|
if (pageType != null)
|
||||||
{
|
{
|
||||||
foreach (IResource resource in ((dynamic)result.Value).Items)
|
foreach (IResource resource in ((dynamic)result.Value).Items)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user