ProviderIdsExtensions.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. /// Checks if this instance has an id for the given provider.
  13. /// </summary>
  14. /// <param name="instance">The instance.</param>
  15. /// <param name="name">The of the provider name.</param>
  16. /// <returns><c>true</c> if a provider id with the given name was found; otherwise <c>false</c>.</returns>
  17. public static bool HasProviderId(this IHasProviderIds instance, string name)
  18. {
  19. if (instance == null)
  20. {
  21. throw new ArgumentNullException(nameof(instance));
  22. }
  23. return instance.ProviderIds?.ContainsKey(name) ?? false;
  24. }
  25. /// <summary>
  26. /// Checks if this instance has an id for the given provider.
  27. /// </summary>
  28. /// <param name="instance">The instance.</param>
  29. /// <param name="provider">The provider.</param>
  30. /// <returns><c>true</c> if a provider id with the given name was found; otherwise <c>false</c>.</returns>
  31. public static bool HasProviderId(this IHasProviderIds instance, MetadataProvider provider)
  32. {
  33. return instance.HasProviderId(provider.ToString());
  34. }
  35. /// <summary>
  36. /// Gets a provider id.
  37. /// </summary>
  38. /// <param name="instance">The instance.</param>
  39. /// <param name="name">The name.</param>
  40. /// <param name="id">The provider id.</param>
  41. /// <returns><c>true</c> if a provider id with the given name was found; otherwise <c>false</c>.</returns>
  42. public static bool TryGetProviderId(this IHasProviderIds instance, string name, [NotNullWhen(true)] out string? id)
  43. {
  44. if (instance == null)
  45. {
  46. throw new ArgumentNullException(nameof(instance));
  47. }
  48. var foundProviderId = instance.ProviderIds.TryGetValue(name, out id);
  49. // This occurs when searching with Identify (and possibly in other places)
  50. if (string.IsNullOrEmpty(id))
  51. {
  52. id = null;
  53. foundProviderId = false;
  54. }
  55. return foundProviderId;
  56. }
  57. /// <summary>
  58. /// Gets a provider id.
  59. /// </summary>
  60. /// <param name="instance">The instance.</param>
  61. /// <param name="provider">The provider.</param>
  62. /// <param name="id">The provider id.</param>
  63. /// <returns><c>true</c> if a provider id with the given name was found; otherwise <c>false</c>.</returns>
  64. public static bool TryGetProviderId(this IHasProviderIds instance, MetadataProvider provider, [NotNullWhen(true)] out string? id)
  65. {
  66. return instance.TryGetProviderId(provider.ToString(), out id);
  67. }
  68. /// <summary>
  69. /// Gets a provider id.
  70. /// </summary>
  71. /// <param name="instance">The instance.</param>
  72. /// <param name="name">The name.</param>
  73. /// <returns>System.String.</returns>
  74. public static string? GetProviderId(this IHasProviderIds instance, string name)
  75. {
  76. instance.TryGetProviderId(name, out string? id);
  77. return id;
  78. }
  79. /// <summary>
  80. /// Gets a provider id.
  81. /// </summary>
  82. /// <param name="instance">The instance.</param>
  83. /// <param name="provider">The provider.</param>
  84. /// <returns>System.String.</returns>
  85. public static string? GetProviderId(this IHasProviderIds instance, MetadataProvider provider)
  86. {
  87. return instance.GetProviderId(provider.ToString());
  88. }
  89. /// <summary>
  90. /// Sets a provider id.
  91. /// </summary>
  92. /// <param name="instance">The instance.</param>
  93. /// <param name="name">The name.</param>
  94. /// <param name="value">The value.</param>
  95. public static void SetProviderId(this IHasProviderIds instance, string name, string value)
  96. {
  97. if (instance == null)
  98. {
  99. throw new ArgumentNullException(nameof(instance));
  100. }
  101. // If it's null remove the key from the dictionary
  102. if (string.IsNullOrEmpty(value))
  103. {
  104. instance.ProviderIds?.Remove(name);
  105. }
  106. else
  107. {
  108. // Ensure it exists
  109. if (instance.ProviderIds == null)
  110. {
  111. instance.ProviderIds = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
  112. }
  113. instance.ProviderIds[name] = value;
  114. }
  115. }
  116. /// <summary>
  117. /// Sets a provider id.
  118. /// </summary>
  119. /// <param name="instance">The instance.</param>
  120. /// <param name="provider">The provider.</param>
  121. /// <param name="value">The value.</param>
  122. public static void SetProviderId(this IHasProviderIds instance, MetadataProvider provider, string value)
  123. {
  124. instance.SetProviderId(provider.ToString(), value);
  125. }
  126. }
  127. }