2018-12-27 23:27:57 +00:00
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
2021-02-19 15:06:54 +00:00
|
|
|
using System.Diagnostics.CodeAnalysis;
|
2022-04-28 00:16:31 +00:00
|
|
|
using System.Linq;
|
2018-12-27 23:27:57 +00:00
|
|
|
|
|
|
|
namespace MediaBrowser.Model.Entities
|
|
|
|
{
|
|
|
|
/// <summary>
|
2020-02-04 00:49:27 +00:00
|
|
|
/// Class ProviderIdsExtensions.
|
2018-12-27 23:27:57 +00:00
|
|
|
/// </summary>
|
|
|
|
public static class ProviderIdsExtensions
|
|
|
|
{
|
2022-04-28 00:16:31 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Case insensitive dictionary of <see cref="MetadataProvider"/> string representation.
|
|
|
|
/// </summary>
|
|
|
|
private static readonly Dictionary<string, string> _metadataProviderEnumDictionary =
|
|
|
|
Enum.GetValues<MetadataProvider>()
|
|
|
|
.ToDictionary(
|
|
|
|
enumValue => enumValue.ToString(),
|
|
|
|
enumValue => enumValue.ToString(),
|
|
|
|
StringComparer.OrdinalIgnoreCase);
|
|
|
|
|
2021-03-01 19:35:38 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Checks if this instance has an id for the given provider.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="instance">The instance.</param>
|
|
|
|
/// <param name="name">The of the provider name.</param>
|
|
|
|
/// <returns><c>true</c> if a provider id with the given name was found; otherwise <c>false</c>.</returns>
|
|
|
|
public static bool HasProviderId(this IHasProviderIds instance, string name)
|
|
|
|
{
|
2022-10-06 18:21:23 +00:00
|
|
|
ArgumentNullException.ThrowIfNull(instance);
|
2021-03-01 19:35:38 +00:00
|
|
|
|
2021-03-03 11:28:40 +00:00
|
|
|
return instance.TryGetProviderId(name, out _);
|
2021-03-01 19:35:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Checks if this instance has an id for the given provider.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="instance">The instance.</param>
|
|
|
|
/// <param name="provider">The provider.</param>
|
|
|
|
/// <returns><c>true</c> if a provider id with the given name was found; otherwise <c>false</c>.</returns>
|
|
|
|
public static bool HasProviderId(this IHasProviderIds instance, MetadataProvider provider)
|
|
|
|
{
|
|
|
|
return instance.HasProviderId(provider.ToString());
|
|
|
|
}
|
|
|
|
|
2018-12-27 23:27:57 +00:00
|
|
|
/// <summary>
|
2020-04-05 16:10:56 +00:00
|
|
|
/// Gets a provider id.
|
2018-12-27 23:27:57 +00:00
|
|
|
/// </summary>
|
|
|
|
/// <param name="instance">The instance.</param>
|
|
|
|
/// <param name="name">The name.</param>
|
2021-02-19 15:06:54 +00:00
|
|
|
/// <param name="id">The provider id.</param>
|
|
|
|
/// <returns><c>true</c> if a provider id with the given name was found; otherwise <c>false</c>.</returns>
|
2021-02-27 23:10:36 +00:00
|
|
|
public static bool TryGetProviderId(this IHasProviderIds instance, string name, [NotNullWhen(true)] out string? id)
|
2018-12-27 23:27:57 +00:00
|
|
|
{
|
2022-10-06 18:21:23 +00:00
|
|
|
ArgumentNullException.ThrowIfNull(instance);
|
2018-12-27 23:27:57 +00:00
|
|
|
|
2022-12-05 14:00:20 +00:00
|
|
|
if (instance.ProviderIds is null)
|
2021-03-03 08:37:21 +00:00
|
|
|
{
|
|
|
|
id = null;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-03-03 08:09:57 +00:00
|
|
|
var foundProviderId = instance.ProviderIds.TryGetValue(name, out id);
|
|
|
|
// This occurs when searching with Identify (and possibly in other places)
|
|
|
|
if (string.IsNullOrEmpty(id))
|
2018-12-27 23:27:57 +00:00
|
|
|
{
|
2021-02-19 16:01:52 +00:00
|
|
|
id = null;
|
2021-03-03 08:09:57 +00:00
|
|
|
foundProviderId = false;
|
2018-12-27 23:27:57 +00:00
|
|
|
}
|
|
|
|
|
2021-03-03 08:09:57 +00:00
|
|
|
return foundProviderId;
|
2021-02-19 15:06:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Gets a provider id.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="instance">The instance.</param>
|
|
|
|
/// <param name="provider">The provider.</param>
|
|
|
|
/// <param name="id">The provider id.</param>
|
|
|
|
/// <returns><c>true</c> if a provider id with the given name was found; otherwise <c>false</c>.</returns>
|
2021-02-27 23:10:36 +00:00
|
|
|
public static bool TryGetProviderId(this IHasProviderIds instance, MetadataProvider provider, [NotNullWhen(true)] out string? id)
|
2021-02-19 15:06:54 +00:00
|
|
|
{
|
|
|
|
return instance.TryGetProviderId(provider.ToString(), out id);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Gets a provider id.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="instance">The instance.</param>
|
|
|
|
/// <param name="name">The name.</param>
|
|
|
|
/// <returns>System.String.</returns>
|
|
|
|
public static string? GetProviderId(this IHasProviderIds instance, string name)
|
|
|
|
{
|
|
|
|
instance.TryGetProviderId(name, out string? id);
|
|
|
|
return id;
|
2018-12-27 23:27:57 +00:00
|
|
|
}
|
|
|
|
|
2021-02-19 16:01:52 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Gets a provider id.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="instance">The instance.</param>
|
|
|
|
/// <param name="provider">The provider.</param>
|
|
|
|
/// <returns>System.String.</returns>
|
|
|
|
public static string? GetProviderId(this IHasProviderIds instance, MetadataProvider provider)
|
|
|
|
{
|
|
|
|
return instance.GetProviderId(provider.ToString());
|
|
|
|
}
|
|
|
|
|
2018-12-27 23:27:57 +00:00
|
|
|
/// <summary>
|
2020-04-05 16:10:56 +00:00
|
|
|
/// Sets a provider id.
|
2018-12-27 23:27:57 +00:00
|
|
|
/// </summary>
|
|
|
|
/// <param name="instance">The instance.</param>
|
|
|
|
/// <param name="name">The name.</param>
|
|
|
|
/// <param name="value">The value.</param>
|
2022-04-28 00:16:31 +00:00
|
|
|
public static void SetProviderId(this IHasProviderIds instance, string name, string? value)
|
2018-12-27 23:27:57 +00:00
|
|
|
{
|
2022-10-06 18:21:23 +00:00
|
|
|
ArgumentNullException.ThrowIfNull(instance);
|
2019-01-07 23:27:46 +00:00
|
|
|
|
2018-12-27 23:27:57 +00:00
|
|
|
// If it's null remove the key from the dictionary
|
|
|
|
if (string.IsNullOrEmpty(value))
|
|
|
|
{
|
2021-02-19 16:01:52 +00:00
|
|
|
instance.ProviderIds?.Remove(name);
|
2018-12-27 23:27:57 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Ensure it exists
|
2021-05-05 11:51:14 +00:00
|
|
|
instance.ProviderIds ??= new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
|
2018-12-27 23:27:57 +00:00
|
|
|
|
2022-04-28 00:16:31 +00:00
|
|
|
// Match on internal MetadataProvider enum string values before adding arbitrary providers
|
|
|
|
if (_metadataProviderEnumDictionary.TryGetValue(name, out var enumValue))
|
|
|
|
{
|
|
|
|
instance.ProviderIds[enumValue] = value;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
instance.ProviderIds[name] = value;
|
|
|
|
}
|
2018-12-27 23:27:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
2020-04-05 16:10:56 +00:00
|
|
|
/// Sets a provider id.
|
2018-12-27 23:27:57 +00:00
|
|
|
/// </summary>
|
|
|
|
/// <param name="instance">The instance.</param>
|
|
|
|
/// <param name="provider">The provider.</param>
|
|
|
|
/// <param name="value">The value.</param>
|
2020-06-06 19:17:49 +00:00
|
|
|
public static void SetProviderId(this IHasProviderIds instance, MetadataProvider provider, string value)
|
2018-12-27 23:27:57 +00:00
|
|
|
{
|
|
|
|
instance.SetProviderId(provider.ToString(), value);
|
|
|
|
}
|
|
|
|
}
|
2019-01-13 19:31:15 +00:00
|
|
|
}
|