ProviderIdsExtensions.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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.TryGetProviderId(name, out _);
  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. if (instance.ProviderIds == null)
  49. {
  50. id = null;
  51. return false;
  52. }
  53. var foundProviderId = instance.ProviderIds.TryGetValue(name, out id);
  54. // This occurs when searching with Identify (and possibly in other places)
  55. if (string.IsNullOrEmpty(id))
  56. {
  57. id = null;
  58. foundProviderId = false;
  59. }
  60. return foundProviderId;
  61. }
  62. /// <summary>
  63. /// Gets a provider id.
  64. /// </summary>
  65. /// <param name="instance">The instance.</param>
  66. /// <param name="provider">The provider.</param>
  67. /// <param name="id">The provider id.</param>
  68. /// <returns><c>true</c> if a provider id with the given name was found; otherwise <c>false</c>.</returns>
  69. public static bool TryGetProviderId(this IHasProviderIds instance, MetadataProvider provider, [NotNullWhen(true)] out string? id)
  70. {
  71. return instance.TryGetProviderId(provider.ToString(), out id);
  72. }
  73. /// <summary>
  74. /// Gets a provider id.
  75. /// </summary>
  76. /// <param name="instance">The instance.</param>
  77. /// <param name="name">The name.</param>
  78. /// <returns>System.String.</returns>
  79. public static string? GetProviderId(this IHasProviderIds instance, string name)
  80. {
  81. instance.TryGetProviderId(name, out string? id);
  82. return id;
  83. }
  84. /// <summary>
  85. /// Gets a provider id.
  86. /// </summary>
  87. /// <param name="instance">The instance.</param>
  88. /// <param name="provider">The provider.</param>
  89. /// <returns>System.String.</returns>
  90. public static string? GetProviderId(this IHasProviderIds instance, MetadataProvider provider)
  91. {
  92. return instance.GetProviderId(provider.ToString());
  93. }
  94. /// <summary>
  95. /// Sets a provider id.
  96. /// </summary>
  97. /// <param name="instance">The instance.</param>
  98. /// <param name="name">The name.</param>
  99. /// <param name="value">The value.</param>
  100. public static void SetProviderId(this IHasProviderIds instance, string name, string value)
  101. {
  102. if (instance == null)
  103. {
  104. throw new ArgumentNullException(nameof(instance));
  105. }
  106. // If it's null remove the key from the dictionary
  107. if (string.IsNullOrEmpty(value))
  108. {
  109. instance.ProviderIds?.Remove(name);
  110. }
  111. else
  112. {
  113. // Ensure it exists
  114. instance.ProviderIds ??= new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
  115. instance.ProviderIds[name] = value;
  116. }
  117. }
  118. /// <summary>
  119. /// Sets a provider id.
  120. /// </summary>
  121. /// <param name="instance">The instance.</param>
  122. /// <param name="provider">The provider.</param>
  123. /// <param name="value">The value.</param>
  124. public static void SetProviderId(this IHasProviderIds instance, MetadataProvider provider, string value)
  125. {
  126. instance.SetProviderId(provider.ToString(), value);
  127. }
  128. }
  129. }