ProviderIdsExtensions.cs 3.4 KB

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