diff --git a/Kyoo.Common/Models/Resources/Episode.cs b/Kyoo.Common/Models/Resources/Episode.cs index 307ab115..37cd51aa 100644 --- a/Kyoo.Common/Models/Resources/Episode.cs +++ b/Kyoo.Common/Models/Resources/Episode.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.ComponentModel; using System.Text.RegularExpressions; using JetBrains.Annotations; using Kyoo.Controllers; @@ -25,8 +26,11 @@ namespace Kyoo.Models return GetSlug(ShowID.ToString(), SeasonNumber, EpisodeNumber, AbsoluteNumber); return GetSlug(ShowSlug ?? Show.Slug, SeasonNumber, EpisodeNumber, AbsoluteNumber); } - [UsedImplicitly] private set + [UsedImplicitly] [NotNull] private set { + if (value == null) + throw new ArgumentNullException(nameof(value)); + Match match = Regex.Match(value, @"(?.+)-s(?\d+)e(?\d+)"); if (match.Success) @@ -76,19 +80,21 @@ namespace Kyoo.Models [LoadableRelation(nameof(SeasonID))] public Season Season { get; set; } /// - /// The season in witch this episode is in. This defaults to -1 if not specified. + /// The season in witch this episode is in. /// + [DefaultValue(-1)] public int SeasonNumber { get; set; } = -1; /// - /// The number of this episode is it's season. This defaults to -1 if not specified. + /// The number of this episode is it's season. /// + [DefaultValue(-1)] public int EpisodeNumber { get; set; } = -1; /// /// The absolute number of this episode. It's an episode number that is not reset to 1 after a new season. - /// This defaults to -1 if not specified. /// + [DefaultValue(-1)] public int AbsoluteNumber { get; set; } = -1; /// diff --git a/Kyoo.Common/Models/Resources/Season.cs b/Kyoo.Common/Models/Resources/Season.cs index e3440670..028022b6 100644 --- a/Kyoo.Common/Models/Resources/Season.cs +++ b/Kyoo.Common/Models/Resources/Season.cs @@ -24,7 +24,7 @@ namespace Kyoo.Models return $"{ShowID}-s{SeasonNumber}"; return $"{ShowSlug ?? Show?.Slug}-s{SeasonNumber}"; } - [UsedImplicitly] private set + [UsedImplicitly] [NotNull] private set { Match match = Regex.Match(value ?? "", @"(?.+)-s(?\d+)"); diff --git a/Kyoo.Common/Utility/Merger.cs b/Kyoo.Common/Utility/Merger.cs index d6378ca9..417ea944 100644 --- a/Kyoo.Common/Utility/Merger.cs +++ b/Kyoo.Common/Utility/Merger.cs @@ -1,6 +1,7 @@ using System; using System.Collections; using System.Collections.Generic; +using System.ComponentModel; using System.Linq; using System.Reflection; using JetBrains.Annotations; @@ -88,9 +89,8 @@ namespace Kyoo foreach (PropertyInfo property in properties) { object value = property.GetValue(second); - object defaultValue = property.PropertyType.IsValueType - ? Activator.CreateInstance(property.PropertyType) - : null; + object defaultValue = property.GetCustomAttribute()?.Value + ?? property.PropertyType.GetClrDefault(); if (value?.Equals(defaultValue) == false && value != property.GetValue(first)) property.SetValue(first, value); diff --git a/Kyoo.Common/Utility/Utility.cs b/Kyoo.Common/Utility/Utility.cs index 28699035..f7041c51 100644 --- a/Kyoo.Common/Utility/Utility.cs +++ b/Kyoo.Common/Utility/Utility.cs @@ -96,6 +96,18 @@ namespace Kyoo return str; } + /// + /// Get the default value of a type. + /// + /// The type to get the default value + /// The default value of the given type. + public static object GetClrDefault(this Type type) + { + return type.IsValueType + ? Activator.CreateInstance(type) + : null; + } + /// /// Return every in the inheritance tree of the parameter (interfaces are not returned) /// diff --git a/Kyoo.CommonAPI/DatabaseContext.cs b/Kyoo.CommonAPI/DatabaseContext.cs index 2e425415..a2b7eeb2 100644 --- a/Kyoo.CommonAPI/DatabaseContext.cs +++ b/Kyoo.CommonAPI/DatabaseContext.cs @@ -330,16 +330,6 @@ namespace Kyoo modelBuilder.Entity() .Property(x => x.Slug) .ValueGeneratedOnAddOrUpdate(); - - modelBuilder.Entity() - .Property(x => x.EpisodeNumber) - .HasDefaultValue(-1); - modelBuilder.Entity() - .Property(x => x.SeasonNumber) - .HasDefaultValue(-1); - modelBuilder.Entity() - .Property(x => x.AbsoluteNumber) - .HasDefaultValue(-1); } ///