Fixing episodes defaults

This commit is contained in:
Zoe Roux 2021-06-23 18:33:40 +02:00
parent 37c752229e
commit 8a3b9e1dec
5 changed files with 26 additions and 18 deletions

View File

@ -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, @"(?<show>.+)-s(?<season>\d+)e(?<episode>\d+)");
if (match.Success)
@ -76,19 +80,21 @@ namespace Kyoo.Models
[LoadableRelation(nameof(SeasonID))] public Season Season { get; set; }
/// <summary>
/// The season in witch this episode is in. This defaults to -1 if not specified.
/// The season in witch this episode is in.
/// </summary>
[DefaultValue(-1)]
public int SeasonNumber { get; set; } = -1;
/// <summary>
/// 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.
/// </summary>
[DefaultValue(-1)]
public int EpisodeNumber { get; set; } = -1;
/// <summary>
/// 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.
/// </summary>
[DefaultValue(-1)]
public int AbsoluteNumber { get; set; } = -1;
/// <summary>

View File

@ -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 ?? "", @"(?<show>.+)-s(?<season>\d+)");

View File

@ -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<DefaultValueAttribute>()?.Value
?? property.PropertyType.GetClrDefault();
if (value?.Equals(defaultValue) == false && value != property.GetValue(first))
property.SetValue(first, value);

View File

@ -96,6 +96,18 @@ namespace Kyoo
return str;
}
/// <summary>
/// Get the default value of a type.
/// </summary>
/// <param name="type">The type to get the default value</param>
/// <returns>The default value of the given type.</returns>
public static object GetClrDefault(this Type type)
{
return type.IsValueType
? Activator.CreateInstance(type)
: null;
}
/// <summary>
/// Return every <see cref="Type"/> in the inheritance tree of the parameter (interfaces are not returned)
/// </summary>

View File

@ -330,16 +330,6 @@ namespace Kyoo
modelBuilder.Entity<Track>()
.Property(x => x.Slug)
.ValueGeneratedOnAddOrUpdate();
modelBuilder.Entity<Episode>()
.Property(x => x.EpisodeNumber)
.HasDefaultValue(-1);
modelBuilder.Entity<Episode>()
.Property(x => x.SeasonNumber)
.HasDefaultValue(-1);
modelBuilder.Entity<Episode>()
.Property(x => x.AbsoluteNumber)
.HasDefaultValue(-1);
}
/// <summary>