diff --git a/Kyoo.Common/Kyoo.Common.csproj b/Kyoo.Common/Kyoo.Common.csproj
index 5154a72c..7799859f 100644
--- a/Kyoo.Common/Kyoo.Common.csproj
+++ b/Kyoo.Common/Kyoo.Common.csproj
@@ -15,6 +15,7 @@
+
diff --git a/Kyoo.Common/Models/Attributes/MergeAttributes.cs b/Kyoo.Common/Models/Attributes/MergeAttributes.cs
new file mode 100644
index 00000000..57861f61
--- /dev/null
+++ b/Kyoo.Common/Models/Attributes/MergeAttributes.cs
@@ -0,0 +1,6 @@
+using System;
+
+namespace Kyoo.Models.Attributes
+{
+ public class NotMergableAttribute : Attribute { }
+}
\ No newline at end of file
diff --git a/Kyoo.Common/Models/Collection.cs b/Kyoo.Common/Models/Collection.cs
index ce6ba4c9..c0297521 100644
--- a/Kyoo.Common/Models/Collection.cs
+++ b/Kyoo.Common/Models/Collection.cs
@@ -1,6 +1,7 @@
using Newtonsoft.Json;
using System.Collections.Generic;
using System.Linq;
+using Kyoo.Models.Attributes;
namespace Kyoo.Models
{
@@ -12,7 +13,7 @@ namespace Kyoo.Models
public string Poster { get; set; }
public string Overview { get; set; }
[JsonIgnore] public string ImgPrimary { get; set; }
- [JsonIgnore] public virtual IEnumerable Links { get; set; }
+ [NotMergable] [JsonIgnore] public virtual IEnumerable Links { get; set; }
public virtual IEnumerable Shows
{
get => Links.Select(x => x.Show);
diff --git a/Kyoo.Common/Models/Library.cs b/Kyoo.Common/Models/Library.cs
index d6a7e702..354507d3 100644
--- a/Kyoo.Common/Models/Library.cs
+++ b/Kyoo.Common/Models/Library.cs
@@ -1,4 +1,7 @@
-using System.Collections.Generic;
+using System.Collections;
+using System.Collections.Generic;
+using System.Linq;
+using Kyoo.Models.Attributes;
using Newtonsoft.Json;
namespace Kyoo.Models
@@ -8,14 +11,34 @@ namespace Kyoo.Models
[JsonIgnore] public long ID { get; set; }
public string Slug { get; set; }
public string Name { get; set; }
- public string[] Paths { get; set; }
- public virtual IEnumerable Providers { get; set; }
- [JsonIgnore] public virtual IEnumerable Shows { get; set; }
- [JsonIgnore] public virtual IEnumerable Collections { get; set; }
+ public IEnumerable Paths { get; set; }
+
+ public IEnumerable Providers
+ {
+ get => ProviderLinks.Select(x => x.Provider);
+ set => ProviderLinks = value.Select(x => new ProviderLink(x, this));
+ }
+ [NotMergable] [JsonIgnore] public virtual IEnumerable ProviderLinks { get; set; }
+ [NotMergable] [JsonIgnore] public virtual IEnumerable Links { get; set; }
+
+ [JsonIgnore] public IEnumerable Shows
+ {
+ get => Links.Where(x => x.Show != null).Select(x => x.Show);
+ set => Links = Utility.MergeLists(
+ value?.Select(x => new LibraryLink(this, x)),
+ Links?.Where(x => x.Show == null));
+ }
+ [JsonIgnore] public IEnumerable Collections
+ {
+ get => Links.Where(x => x.Collection != null).Select(x => x.Collection);
+ set => Links = Utility.MergeLists(
+ value?.Select(x => new LibraryLink(this, x)),
+ Links?.Where(x => x.Collection == null));
+ }
public Library() { }
- public Library(string slug, string name, string[] paths, IEnumerable providers)
+ public Library(string slug, string name, IEnumerable paths, IEnumerable providers)
{
Slug = slug;
Name = name;
diff --git a/Kyoo.Common/Models/LibraryLink.cs b/Kyoo.Common/Models/LibraryLink.cs
index 86635adf..36cfc749 100644
--- a/Kyoo.Common/Models/LibraryLink.cs
+++ b/Kyoo.Common/Models/LibraryLink.cs
@@ -9,5 +9,19 @@ namespace Kyoo.Models
public virtual Show Show { get; set; }
public long? CollectionID { get; set; }
public virtual Collection Collection { get; set; }
+
+ public LibraryLink() { }
+
+ public LibraryLink(Library library, Show show)
+ {
+ Library = library;
+ Show = show;
+ }
+
+ public LibraryLink(Library library, Collection collection)
+ {
+ Library = library;
+ Collection = collection;
+ }
}
}
\ No newline at end of file
diff --git a/Kyoo.Common/Models/ProviderID.cs b/Kyoo.Common/Models/ProviderID.cs
index fb90f648..e7d76268 100644
--- a/Kyoo.Common/Models/ProviderID.cs
+++ b/Kyoo.Common/Models/ProviderID.cs
@@ -16,26 +16,5 @@ namespace Kyoo.Models
Name = name;
Logo = logo;
}
-
- protected bool Equals(ProviderID other)
- {
- return Name == other.Name;
- }
-
- public override bool Equals(object obj)
- {
- if (ReferenceEquals(null, obj))
- return false;
- if (ReferenceEquals(this, obj))
- return true;
- if (obj.GetType() != this.GetType())
- return false;
- return Equals((ProviderID)obj);
- }
-
- public override int GetHashCode()
- {
- return Name != null ? Name.GetHashCode() : 0;
- }
}
}
\ No newline at end of file
diff --git a/Kyoo.Common/Models/ProviderLink.cs b/Kyoo.Common/Models/ProviderLink.cs
index 6bf0eacf..088e4f80 100644
--- a/Kyoo.Common/Models/ProviderLink.cs
+++ b/Kyoo.Common/Models/ProviderLink.cs
@@ -10,30 +10,12 @@ namespace Kyoo.Models
[JsonIgnore] public long? LibraryID { get; set; }
[JsonIgnore] public virtual Library Library { get; set; }
- public string Name
- {
- get => Provider?.Name;
- set
- {
- if (Provider != null)
- Provider.Name = value;
- else
- Provider = new ProviderID {Name = value};
- }
- }
-
- public string Logo
- {
- get => Provider?.Logo;
- set
- {
- if (Provider != null)
- Provider.Logo = value;
- else
- Provider = new ProviderID {Logo = value};
- }
- }
-
public ProviderLink() { }
+
+ public ProviderLink(ProviderID provider, Library library)
+ {
+ Provider = provider;
+ Library = library;
+ }
}
}
\ No newline at end of file
diff --git a/Kyoo.Common/Models/Show.cs b/Kyoo.Common/Models/Show.cs
index 8d66e7c7..f6e9578b 100644
--- a/Kyoo.Common/Models/Show.cs
+++ b/Kyoo.Common/Models/Show.cs
@@ -1,6 +1,7 @@
using Newtonsoft.Json;
using System.Collections.Generic;
using System.Linq;
+using Kyoo.Models.Attributes;
namespace Kyoo.Models
{
@@ -10,7 +11,7 @@ namespace Kyoo.Models
public string Slug { get; set; }
public string Title { get; set; }
- public string[] Aliases { get; set; }
+ public IEnumerable Aliases { get; set; }
[JsonIgnore] public string Path { get; set; }
public string Overview { get; set; }
public Status? Status { get; set; }
@@ -31,10 +32,10 @@ namespace Kyoo.Models
public virtual IEnumerable Genres
{
- get { return GenreLinks?.Select(x => x.Genre).OrderBy(x => x.Name); }
- set { GenreLinks = value?.Select(x => new GenreLink(this, x)).ToList(); }
+ get => GenreLinks?.Select(x => x.Genre);
+ set => GenreLinks = value?.Select(x => new GenreLink(this, x)).ToList();
}
- [JsonIgnore] public virtual IEnumerable GenreLinks { get; set; }
+ [NotMergable] [JsonIgnore] public virtual IEnumerable GenreLinks { get; set; }
public virtual Studio Studio { get; set; }
[JsonIgnore] public virtual IEnumerable People { get; set; }
[JsonIgnore] public virtual IEnumerable Seasons { get; set; }
@@ -55,7 +56,7 @@ namespace Kyoo.Models
{
Slug = slug;
Title = title;
- Aliases = aliases?.ToArray();
+ Aliases = aliases;
Path = path;
Overview = overview;
TrailerUrl = trailerUrl;
@@ -83,7 +84,7 @@ namespace Kyoo.Models
{
Slug = slug;
Title = title;
- Aliases = aliases?.ToArray();
+ Aliases = aliases;
Path = path;
Overview = overview;
TrailerUrl = trailerUrl;
diff --git a/Kyoo.Common/Utility.cs b/Kyoo.Common/Utility.cs
index 19faf52f..7e1177e2 100644
--- a/Kyoo.Common/Utility.cs
+++ b/Kyoo.Common/Utility.cs
@@ -1,13 +1,14 @@
using System;
using System.Collections;
using System.Collections.Generic;
-using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Text.RegularExpressions;
+using JetBrains.Annotations;
using Kyoo.Models;
+using Kyoo.Models.Attributes;
namespace Kyoo
{
@@ -64,9 +65,9 @@ namespace Kyoo
return second;
if (second == null)
return first;
- List list = first.ToList();
if (isEqual == null)
- isEqual = (x, y) => x.Equals(y);
+ return first.Concat(second).ToList();
+ List list = first.ToList();
return list.Concat(second.Where(x => !list.Any(y => isEqual(x, y)))).ToList();
}
@@ -92,9 +93,18 @@ namespace Kyoo
public static T Merge(T first, T second)
{
+ // TODO During the merge, reference to the second values are not set to the first value (for child objects).
+ if (first == null)
+ return second;
+ if (second == null)
+ return first;
+
Type type = typeof(T);
foreach (PropertyInfo property in type.GetProperties().Where(x => x.CanRead && x.CanWrite))
{
+ if (Attribute.GetCustomAttribute(property, typeof(NotMergableAttribute)) != null)
+ continue;
+
object oldValue = property.GetValue(first);
object newValue = property.GetValue(second);
object defaultValue = property.PropertyType.IsValueType
@@ -106,7 +116,11 @@ namespace Kyoo
else if (typeof(IEnumerable).IsAssignableFrom(property.PropertyType)
&& property.PropertyType != typeof(string))
{
- property.SetValue((IEnumerable