jellyfin/MediaBrowser.Model/Entities/ProviderIdsExtensions.cs

112 lines
3.9 KiB
C#
Raw Normal View History

2018-12-28 00:27:57 +01:00
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
2018-12-28 00:27:57 +01:00
namespace MediaBrowser.Model.Entities
{
/// <summary>
2020-02-04 01:49:27 +01:00
/// Class ProviderIdsExtensions.
2018-12-28 00:27:57 +01:00
/// </summary>
public static class ProviderIdsExtensions
{
/// <summary>
/// Gets a provider id.
2018-12-28 00:27:57 +01:00
/// </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)
2018-12-28 00:27:57 +01:00
{
if (instance == null)
{
throw new ArgumentNullException(nameof(instance));
2018-12-28 00:27:57 +01:00
}
if (instance.ProviderIds == null)
{
2021-02-19 17:01:52 +01:00
id = null;
return false;
2018-12-28 00:27:57 +01:00
}
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;
2018-12-28 00:27:57 +01:00
}
2021-02-19 17:01:52 +01: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-28 00:27:57 +01:00
/// <summary>
/// Sets a provider id.
2018-12-28 00:27:57 +01: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)
{
throw new ArgumentNullException(nameof(instance));
2018-12-28 00:27:57 +01:00
}
2019-01-08 00:27:46 +01:00
2018-12-28 00:27:57 +01:00
// If it's null remove the key from the dictionary
if (string.IsNullOrEmpty(value))
{
2021-02-19 17:01:52 +01:00
instance.ProviderIds?.Remove(name);
2018-12-28 00:27:57 +01:00
}
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.
2018-12-28 00:27:57 +01:00
/// </summary>
/// <param name="instance">The instance.</param>
/// <param name="provider">The provider.</param>
/// <param name="value">The value.</param>
2020-06-06 21:17:49 +02:00
public static void SetProviderId(this IHasProviderIds instance, MetadataProvider provider, string value)
2018-12-28 00:27:57 +01:00
{
instance.SetProviderId(provider.ToString(), value);
}
}
}