2018-12-27 16:27:57 -07:00
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
|
|
namespace MediaBrowser.Model.Entities
|
|
|
|
{
|
|
|
|
/// <summary>
|
2020-02-03 17:49:27 -07:00
|
|
|
/// Class ProviderIdsExtensions.
|
2018-12-27 16:27:57 -07:00
|
|
|
/// </summary>
|
|
|
|
public static class ProviderIdsExtensions
|
|
|
|
{
|
|
|
|
/// <summary>
|
|
|
|
/// Determines whether [has provider identifier] [the specified instance].
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="instance">The instance.</param>
|
|
|
|
/// <param name="provider">The provider.</param>
|
|
|
|
/// <returns><c>true</c> if [has provider identifier] [the specified instance]; otherwise, <c>false</c>.</returns>
|
2020-06-06 12:17:49 -07:00
|
|
|
public static bool HasProviderId(this IHasProviderIds instance, MetadataProvider provider)
|
2018-12-27 16:27:57 -07:00
|
|
|
{
|
|
|
|
return !string.IsNullOrEmpty(instance.GetProviderId(provider.ToString()));
|
|
|
|
}
|
2019-01-07 16:27:46 -07:00
|
|
|
|
2018-12-27 16:27:57 -07:00
|
|
|
/// <summary>
|
2020-04-05 09:10:56 -07:00
|
|
|
/// Gets a provider id.
|
2018-12-27 16:27:57 -07:00
|
|
|
/// </summary>
|
|
|
|
/// <param name="instance">The instance.</param>
|
|
|
|
/// <param name="provider">The provider.</param>
|
|
|
|
/// <returns>System.String.</returns>
|
2020-06-06 12:17:49 -07:00
|
|
|
public static string? GetProviderId(this IHasProviderIds instance, MetadataProvider provider)
|
2018-12-27 16:27:57 -07:00
|
|
|
{
|
|
|
|
return instance.GetProviderId(provider.ToString());
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
2020-04-05 09:10:56 -07:00
|
|
|
/// Gets a provider id.
|
2018-12-27 16:27:57 -07:00
|
|
|
/// </summary>
|
|
|
|
/// <param name="instance">The instance.</param>
|
|
|
|
/// <param name="name">The name.</param>
|
|
|
|
/// <returns>System.String.</returns>
|
2020-04-05 09:10:56 -07:00
|
|
|
public static string? GetProviderId(this IHasProviderIds instance, string name)
|
2018-12-27 16:27:57 -07:00
|
|
|
{
|
|
|
|
if (instance == null)
|
|
|
|
{
|
2019-01-06 13:50:43 -07:00
|
|
|
throw new ArgumentNullException(nameof(instance));
|
2018-12-27 16:27:57 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if (instance.ProviderIds == null)
|
|
|
|
{
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2020-11-13 09:36:17 -07:00
|
|
|
instance.ProviderIds.TryGetValue(name, out string? id);
|
2020-12-14 07:20:16 -07:00
|
|
|
return string.IsNullOrEmpty(id) ? null : id;
|
2018-12-27 16:27:57 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
2020-04-05 09:10:56 -07:00
|
|
|
/// Sets a provider id.
|
2018-12-27 16:27:57 -07:00
|
|
|
/// </summary>
|
|
|
|
/// <param name="instance">The instance.</param>
|
|
|
|
/// <param name="name">The name.</param>
|
|
|
|
/// <param name="value">The value.</param>
|
|
|
|
public static void SetProviderId(this IHasProviderIds instance, string name, string value)
|
|
|
|
{
|
|
|
|
if (instance == null)
|
|
|
|
{
|
2019-01-06 13:50:43 -07:00
|
|
|
throw new ArgumentNullException(nameof(instance));
|
2018-12-27 16:27:57 -07:00
|
|
|
}
|
2019-01-07 16:27:46 -07:00
|
|
|
|
2018-12-27 16:27:57 -07:00
|
|
|
// If it's null remove the key from the dictionary
|
|
|
|
if (string.IsNullOrEmpty(value))
|
|
|
|
{
|
|
|
|
if (instance.ProviderIds != null)
|
|
|
|
{
|
|
|
|
if (instance.ProviderIds.ContainsKey(name))
|
|
|
|
{
|
|
|
|
instance.ProviderIds.Remove(name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Ensure it exists
|
|
|
|
if (instance.ProviderIds == null)
|
|
|
|
{
|
|
|
|
instance.ProviderIds = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
|
|
|
|
}
|
|
|
|
|
|
|
|
instance.ProviderIds[name] = value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
2020-04-05 09:10:56 -07:00
|
|
|
/// Sets a provider id.
|
2018-12-27 16:27:57 -07:00
|
|
|
/// </summary>
|
|
|
|
/// <param name="instance">The instance.</param>
|
|
|
|
/// <param name="provider">The provider.</param>
|
|
|
|
/// <param name="value">The value.</param>
|
2020-06-06 12:17:49 -07:00
|
|
|
public static void SetProviderId(this IHasProviderIds instance, MetadataProvider provider, string value)
|
2018-12-27 16:27:57 -07:00
|
|
|
{
|
|
|
|
instance.SetProviderId(provider.ToString(), value);
|
|
|
|
}
|
|
|
|
}
|
2019-01-13 12:31:15 -07:00
|
|
|
}
|