ProviderIdsExtensions.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics.CodeAnalysis;
  4. namespace MediaBrowser.Model.Entities
  5. {
  6. /// <summary>
  7. /// Class ProviderIdsExtensions.
  8. /// </summary>
  9. public static class ProviderIdsExtensions
  10. {
  11. /// <summary>
  12. /// Gets a provider id.
  13. /// </summary>
  14. /// <param name="instance">The instance.</param>
  15. /// <param name="name">The name.</param>
  16. /// <param name="id">The provider id.</param>
  17. /// <returns><c>true</c> if a provider id with the given name was found; otherwise <c>false</c>.</returns>
  18. public static bool TryGetProviderId(this IHasProviderIds instance, string name, [MaybeNullWhen(false)] out string id)
  19. {
  20. if (instance == null)
  21. {
  22. throw new ArgumentNullException(nameof(instance));
  23. }
  24. if (instance.ProviderIds == null)
  25. {
  26. id = null;
  27. return false;
  28. }
  29. return instance.ProviderIds.TryGetValue(name, out id);
  30. }
  31. /// <summary>
  32. /// Gets a provider id.
  33. /// </summary>
  34. /// <param name="instance">The instance.</param>
  35. /// <param name="provider">The provider.</param>
  36. /// <param name="id">The provider id.</param>
  37. /// <returns><c>true</c> if a provider id with the given name was found; otherwise <c>false</c>.</returns>
  38. public static bool TryGetProviderId(this IHasProviderIds instance, MetadataProvider provider, [MaybeNullWhen(false)] out string id)
  39. {
  40. return instance.TryGetProviderId(provider.ToString(), out id);
  41. }
  42. /// <summary>
  43. /// Gets a provider id.
  44. /// </summary>
  45. /// <param name="instance">The instance.</param>
  46. /// <param name="name">The name.</param>
  47. /// <returns>System.String.</returns>
  48. public static string? GetProviderId(this IHasProviderIds instance, string name)
  49. {
  50. instance.TryGetProviderId(name, out string? id);
  51. return id;
  52. }
  53. /// <summary>
  54. /// Gets a provider id.
  55. /// </summary>
  56. /// <param name="instance">The instance.</param>
  57. /// <param name="provider">The provider.</param>
  58. /// <returns>System.String.</returns>
  59. public static string? GetProviderId(this IHasProviderIds instance, MetadataProvider provider)
  60. {
  61. return instance.GetProviderId(provider.ToString());
  62. }
  63. /// <summary>
  64. /// Sets a provider id.
  65. /// </summary>
  66. /// <param name="instance">The instance.</param>
  67. /// <param name="name">The name.</param>
  68. /// <param name="value">The value.</param>
  69. public static void SetProviderId(this IHasProviderIds instance, string name, string value)
  70. {
  71. if (instance == null)
  72. {
  73. throw new ArgumentNullException(nameof(instance));
  74. }
  75. // If it's null remove the key from the dictionary
  76. if (string.IsNullOrEmpty(value))
  77. {
  78. instance.ProviderIds?.Remove(name);
  79. }
  80. else
  81. {
  82. // Ensure it exists
  83. if (instance.ProviderIds == null)
  84. {
  85. instance.ProviderIds = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
  86. }
  87. instance.ProviderIds[name] = value;
  88. }
  89. }
  90. /// <summary>
  91. /// Sets a provider id.
  92. /// </summary>
  93. /// <param name="instance">The instance.</param>
  94. /// <param name="provider">The provider.</param>
  95. /// <param name="value">The value.</param>
  96. public static void SetProviderId(this IHasProviderIds instance, MetadataProvider provider, string value)
  97. {
  98. instance.SetProviderId(provider.ToString(), value);
  99. }
  100. }
  101. }