You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
jellyfin/MediaBrowser.Model/Entities/ProviderIdsExtensions.cs

129 lines
4.6 KiB

6 years ago
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
6 years ago
namespace MediaBrowser.Model.Entities
{
/// <summary>
/// Class ProviderIdsExtensions.
6 years ago
/// </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>
public static bool HasProviderId(this IHasProviderIds instance, MetadataProvider provider)
6 years ago
{
return !string.IsNullOrEmpty(instance.GetProviderId(provider.ToString()));
}
6 years ago
/// <summary>
/// Gets a provider id.
6 years ago
/// </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)
6 years ago
{
return instance.GetProviderId(provider.ToString());
}
/// <summary>
/// Gets a provider id.
6 years ago
/// </summary>
/// <param name="instance">The instance.</param>
/// <param name="name">The name.</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>
public static bool TryGetProviderId(this IHasProviderIds instance, string name, [MaybeNullWhen(false)] out string id)
6 years ago
{
if (instance == null)
{
throw new ArgumentNullException(nameof(instance));
6 years ago
}
if (instance.ProviderIds == null)
{
id = string.Empty;
return false;
6 years ago
}
return instance.ProviderIds.TryGetValue(name, out id);
}
/// <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>
public static bool TryGetProviderId(this IHasProviderIds instance, MetadataProvider provider, [MaybeNullWhen(false)] out string id)
{
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;
6 years ago
}
/// <summary>
/// Sets a provider id.
6 years ago
/// </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)
{
throw new ArgumentNullException(nameof(instance));
6 years ago
}
6 years ago
// 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>
/// Sets a provider id.
6 years ago
/// </summary>
/// <param name="instance">The instance.</param>
/// <param name="provider">The provider.</param>
/// <param name="value">The value.</param>
public static void SetProviderId(this IHasProviderIds instance, MetadataProvider provider, string value)
6 years ago
{
instance.SetProviderId(provider.ToString(), value);
}
}
}