CountHelpers.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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. private static CountType? GetCountType(BaseItem item)
  16. {
  17. if (item is Movie)
  18. {
  19. return CountType.Movie;
  20. }
  21. if (item is Episode)
  22. {
  23. return CountType.Episode;
  24. }
  25. if (item is Game)
  26. {
  27. return CountType.Game;
  28. }
  29. if (item is Audio)
  30. {
  31. return CountType.Song;
  32. }
  33. if (item is Trailer)
  34. {
  35. return CountType.Trailer;
  36. }
  37. if (item is Series)
  38. {
  39. return CountType.Series;
  40. }
  41. if (item is MusicAlbum)
  42. {
  43. return CountType.MusicAlbum;
  44. }
  45. if (item is MusicVideo)
  46. {
  47. return CountType.MusicVideo;
  48. }
  49. if (item is AdultVideo)
  50. {
  51. return CountType.AdultVideo;
  52. }
  53. return null;
  54. }
  55. /// <summary>
  56. /// Increments the count.
  57. /// </summary>
  58. /// <param name="counts">The counts.</param>
  59. /// <param name="key">The key.</param>
  60. internal static void IncrementCount(Dictionary<CountType, int> counts, CountType key)
  61. {
  62. int count;
  63. if (counts.TryGetValue(key, out count))
  64. {
  65. count++;
  66. counts[key] = count;
  67. }
  68. else
  69. {
  70. counts.Add(key, 1);
  71. }
  72. }
  73. /// <summary>
  74. /// Gets the counts.
  75. /// </summary>
  76. /// <param name="counts">The counts.</param>
  77. /// <returns>ItemByNameCounts.</returns>
  78. internal static ItemByNameCounts GetCounts(Dictionary<CountType, int> counts)
  79. {
  80. return new ItemByNameCounts
  81. {
  82. AdultVideoCount = GetCount(counts, CountType.AdultVideo),
  83. AlbumCount = GetCount(counts, CountType.MusicAlbum),
  84. EpisodeCount = GetCount(counts, CountType.Episode),
  85. GameCount = GetCount(counts, CountType.Game),
  86. MovieCount = GetCount(counts, CountType.Movie),
  87. MusicVideoCount = GetCount(counts, CountType.MusicVideo),
  88. SeriesCount = GetCount(counts, CountType.Series),
  89. SongCount = GetCount(counts, CountType.Song),
  90. TrailerCount = GetCount(counts, CountType.Trailer),
  91. TotalCount = GetCount(counts, CountType.Total)
  92. };
  93. }
  94. /// <summary>
  95. /// Gets the count.
  96. /// </summary>
  97. /// <param name="counts">The counts.</param>
  98. /// <param name="key">The key.</param>
  99. /// <returns>System.Int32.</returns>
  100. internal static int GetCount(Dictionary<CountType, int> counts, CountType key)
  101. {
  102. int count;
  103. if (counts.TryGetValue(key, out count))
  104. {
  105. return count;
  106. }
  107. return 0;
  108. }
  109. /// <summary>
  110. /// Sets the item counts.
  111. /// </summary>
  112. /// <param name="userId">The user id.</param>
  113. /// <param name="media">The media.</param>
  114. /// <param name="names">The names.</param>
  115. /// <param name="masterDictionary">The master dictionary.</param>
  116. internal static void SetItemCounts(Guid userId, BaseItem media, IEnumerable<string> names, Dictionary<string, Dictionary<Guid, Dictionary<CountType, int>>> masterDictionary)
  117. {
  118. var countType = GetCountType(media);
  119. foreach (var name in names)
  120. {
  121. Dictionary<Guid, Dictionary<CountType, int>> libraryCounts;
  122. if (!masterDictionary.TryGetValue(name, out libraryCounts))
  123. {
  124. libraryCounts = new Dictionary<Guid, Dictionary<CountType, int>>();
  125. masterDictionary.Add(name, libraryCounts);
  126. }
  127. var userLibId = userId/* ?? Guid.Empty*/;
  128. Dictionary<CountType, int> userDictionary;
  129. if (!libraryCounts.TryGetValue(userLibId, out userDictionary))
  130. {
  131. userDictionary = new Dictionary<CountType, int>();
  132. libraryCounts.Add(userLibId, userDictionary);
  133. }
  134. if (countType.HasValue)
  135. {
  136. IncrementCount(userDictionary, countType.Value);
  137. }
  138. IncrementCount(userDictionary, CountType.Total);
  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. }