ProviderIdsExtensions.cs 5.0 KB

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