| | | 1 | | using System; |
| | | 2 | | using System.Collections.Generic; |
| | | 3 | | using Jellyfin.Data.Enums; |
| | | 4 | | using MediaBrowser.Controller.Persistence; |
| | | 5 | | using MediaBrowser.Model.Dto; |
| | | 6 | | |
| | | 7 | | namespace Jellyfin.Server.Implementations.Item; |
| | | 8 | | |
| | | 9 | | /// <summary> |
| | | 10 | | /// Turns per-type counts into an <see cref="ItemCounts"/>. |
| | | 11 | | /// </summary> |
| | | 12 | | internal static class ItemCountBuilder |
| | | 13 | | { |
| | | 14 | | /// <summary> |
| | | 15 | | /// Builds the counts of one by-name item. |
| | | 16 | | /// </summary> |
| | | 17 | | /// <param name="itemTypeLookup">The item type lookup.</param> |
| | | 18 | | /// <param name="counts">The counted items, by type name. A type may repeat.</param> |
| | | 19 | | /// <returns>The counts.</returns> |
| | | 20 | | public static ItemCounts Build(IItemTypeLookup itemTypeLookup, IEnumerable<(string Type, int Count)> counts) |
| | | 21 | | { |
| | 57 | 22 | | ArgumentNullException.ThrowIfNull(itemTypeLookup); |
| | 57 | 23 | | ArgumentNullException.ThrowIfNull(counts); |
| | | 24 | | |
| | 57 | 25 | | var lookup = itemTypeLookup.BaseItemKindNames; |
| | 57 | 26 | | var result = new ItemCounts(); |
| | | 27 | | |
| | 244 | 28 | | foreach (var (type, count) in counts) |
| | | 29 | | { |
| | | 30 | | // Accumulated rather than assigned: a caller may group by something finer than the |
| | | 31 | | // type and hand the same type over more than once. |
| | 65 | 32 | | if (string.Equals(type, lookup[BaseItemKind.MusicAlbum], StringComparison.Ordinal)) |
| | | 33 | | { |
| | 8 | 34 | | result.AlbumCount += count; |
| | | 35 | | } |
| | 57 | 36 | | else if (string.Equals(type, lookup[BaseItemKind.MusicArtist], StringComparison.Ordinal)) |
| | | 37 | | { |
| | 1 | 38 | | result.ArtistCount += count; |
| | | 39 | | } |
| | 56 | 40 | | else if (string.Equals(type, lookup[BaseItemKind.Episode], StringComparison.Ordinal)) |
| | | 41 | | { |
| | 8 | 42 | | result.EpisodeCount += count; |
| | | 43 | | } |
| | 48 | 44 | | else if (string.Equals(type, lookup[BaseItemKind.Movie], StringComparison.Ordinal)) |
| | | 45 | | { |
| | 29 | 46 | | result.MovieCount += count; |
| | | 47 | | } |
| | 19 | 48 | | else if (string.Equals(type, lookup[BaseItemKind.MusicVideo], StringComparison.Ordinal)) |
| | | 49 | | { |
| | 0 | 50 | | result.MusicVideoCount += count; |
| | | 51 | | } |
| | 19 | 52 | | else if (string.Equals(type, lookup[BaseItemKind.LiveTvProgram], StringComparison.Ordinal)) |
| | | 53 | | { |
| | 0 | 54 | | result.ProgramCount += count; |
| | | 55 | | } |
| | 19 | 56 | | else if (string.Equals(type, lookup[BaseItemKind.Series], StringComparison.Ordinal)) |
| | | 57 | | { |
| | 7 | 58 | | result.SeriesCount += count; |
| | | 59 | | } |
| | 12 | 60 | | else if (string.Equals(type, lookup[BaseItemKind.Audio], StringComparison.Ordinal)) |
| | | 61 | | { |
| | 10 | 62 | | result.SongCount += count; |
| | | 63 | | } |
| | 2 | 64 | | else if (string.Equals(type, lookup[BaseItemKind.Trailer], StringComparison.Ordinal)) |
| | | 65 | | { |
| | 0 | 66 | | result.TrailerCount += count; |
| | | 67 | | } |
| | 2 | 68 | | else if (string.Equals(type, lookup[BaseItemKind.BoxSet], StringComparison.Ordinal)) |
| | | 69 | | { |
| | 1 | 70 | | result.BoxSetCount += count; |
| | | 71 | | } |
| | 1 | 72 | | else if (string.Equals(type, lookup[BaseItemKind.Book], StringComparison.Ordinal)) |
| | | 73 | | { |
| | 1 | 74 | | result.BookCount += count; |
| | | 75 | | } |
| | | 76 | | } |
| | | 77 | | |
| | 57 | 78 | | result.ItemCount = result.TotalItemCount(); |
| | | 79 | | |
| | 57 | 80 | | return result; |
| | | 81 | | } |
| | | 82 | | |
| | | 83 | | /// <summary> |
| | | 84 | | /// Replaces the episode count, which both by-name paths decide separately from the other |
| | | 85 | | /// types because a genre or studio is usually written on the series rather than its episodes. |
| | | 86 | | /// </summary> |
| | | 87 | | /// <param name="counts">The counts to update.</param> |
| | | 88 | | /// <param name="episodeCount">The episode count.</param> |
| | | 89 | | public static void SetEpisodeCount(ItemCounts counts, int episodeCount) |
| | | 90 | | { |
| | 28 | 91 | | ArgumentNullException.ThrowIfNull(counts); |
| | | 92 | | |
| | 28 | 93 | | counts.EpisodeCount = episodeCount; |
| | 28 | 94 | | counts.ItemCount = counts.TotalItemCount(); |
| | 28 | 95 | | } |
| | | 96 | | } |