CountHelpers.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. using MediaBrowser.Controller.Entities;
  2. using MediaBrowser.Controller.Entities.Audio;
  3. using MediaBrowser.Controller.Entities.Movies;
  4. using MediaBrowser.Controller.Entities.TV;
  5. using MediaBrowser.Model.Dto;
  6. using System;
  7. using System.Collections.Generic;
  8. namespace MediaBrowser.Server.Implementations.Library.Validators
  9. {
  10. /// <summary>
  11. /// Class CountHelpers
  12. /// </summary>
  13. internal static class CountHelpers
  14. {
  15. /// <summary>
  16. /// Adds to dictionary.
  17. /// </summary>
  18. /// <param name="item">The item.</param>
  19. /// <param name="counts">The counts.</param>
  20. internal static void AddToDictionary(BaseItem item, Dictionary<CountType, int> counts)
  21. {
  22. if (item is Movie)
  23. {
  24. IncrementCount(counts, CountType.Movie);
  25. }
  26. else if (item is Trailer)
  27. {
  28. IncrementCount(counts, CountType.Trailer);
  29. }
  30. else if (item is Series)
  31. {
  32. IncrementCount(counts, CountType.Series);
  33. }
  34. else if (item is Game)
  35. {
  36. IncrementCount(counts, CountType.Game);
  37. }
  38. else if (item is Audio)
  39. {
  40. IncrementCount(counts, CountType.Song);
  41. }
  42. else if (item is MusicAlbum)
  43. {
  44. IncrementCount(counts, CountType.MusicAlbum);
  45. }
  46. else if (item is Episode)
  47. {
  48. IncrementCount(counts, CountType.Episode);
  49. }
  50. else if (item is MusicVideo)
  51. {
  52. IncrementCount(counts, CountType.MusicVideo);
  53. }
  54. else if (item is AdultVideo)
  55. {
  56. IncrementCount(counts, CountType.AdultVideo);
  57. }
  58. IncrementCount(counts, CountType.Total);
  59. }
  60. /// <summary>
  61. /// Increments the count.
  62. /// </summary>
  63. /// <param name="counts">The counts.</param>
  64. /// <param name="key">The key.</param>
  65. internal static void IncrementCount(Dictionary<CountType, int> counts, CountType key)
  66. {
  67. int count;
  68. if (counts.TryGetValue(key, out count))
  69. {
  70. count++;
  71. counts[key] = count;
  72. }
  73. else
  74. {
  75. counts.Add(key, 1);
  76. }
  77. }
  78. /// <summary>
  79. /// Gets the counts.
  80. /// </summary>
  81. /// <param name="counts">The counts.</param>
  82. /// <returns>ItemByNameCounts.</returns>
  83. internal static ItemByNameCounts GetCounts(Dictionary<CountType, int> counts)
  84. {
  85. return new ItemByNameCounts
  86. {
  87. AdultVideoCount = GetCount(counts, CountType.AdultVideo),
  88. AlbumCount = GetCount(counts, CountType.MusicAlbum),
  89. EpisodeCount = GetCount(counts, CountType.Episode),
  90. GameCount = GetCount(counts, CountType.Game),
  91. MovieCount = GetCount(counts, CountType.Movie),
  92. MusicVideoCount = GetCount(counts, CountType.MusicVideo),
  93. SeriesCount = GetCount(counts, CountType.Series),
  94. SongCount = GetCount(counts, CountType.Song),
  95. TrailerCount = GetCount(counts, CountType.Trailer),
  96. TotalCount = GetCount(counts, CountType.Total)
  97. };
  98. }
  99. /// <summary>
  100. /// Gets the count.
  101. /// </summary>
  102. /// <param name="counts">The counts.</param>
  103. /// <param name="key">The key.</param>
  104. /// <returns>System.Int32.</returns>
  105. internal static int GetCount(Dictionary<CountType, int> counts, CountType key)
  106. {
  107. int count;
  108. if (counts.TryGetValue(key, out count))
  109. {
  110. return count;
  111. }
  112. return 0;
  113. }
  114. /// <summary>
  115. /// Sets the item counts.
  116. /// </summary>
  117. /// <param name="userId">The user id.</param>
  118. /// <param name="media">The media.</param>
  119. /// <param name="names">The names.</param>
  120. /// <param name="masterDictionary">The master dictionary.</param>
  121. internal static void SetItemCounts(Guid userId, BaseItem media, IEnumerable<string> names, Dictionary<string, Dictionary<Guid, Dictionary<CountType, int>>> masterDictionary)
  122. {
  123. foreach (var name in names)
  124. {
  125. Dictionary<Guid, Dictionary<CountType, int>> libraryCounts;
  126. if (!masterDictionary.TryGetValue(name, out libraryCounts))
  127. {
  128. libraryCounts = new Dictionary<Guid, Dictionary<CountType, int>>();
  129. masterDictionary.Add(name, libraryCounts);
  130. }
  131. var userLibId = userId/* ?? Guid.Empty*/;
  132. Dictionary<CountType, int> userDictionary;
  133. if (!libraryCounts.TryGetValue(userLibId, out userDictionary))
  134. {
  135. userDictionary = new Dictionary<CountType, int>();
  136. libraryCounts.Add(userLibId, userDictionary);
  137. }
  138. AddToDictionary(media, userDictionary);
  139. }
  140. }
  141. }
  142. internal enum CountType
  143. {
  144. AdultVideo,
  145. MusicAlbum,
  146. Episode,
  147. Game,
  148. Movie,
  149. MusicVideo,
  150. Series,
  151. Song,
  152. Trailer,
  153. Total
  154. }
  155. }