aboutsummaryrefslogtreecommitdiff
path: root/Jellyfin.Server.Implementations/Item/ItemCountBuilder.cs
blob: 0f8a1b9dfe547dca881a33ce9587536f137aaed6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
using System;
using System.Collections.Generic;
using Jellyfin.Data.Enums;
using MediaBrowser.Controller.Persistence;
using MediaBrowser.Model.Dto;

namespace Jellyfin.Server.Implementations.Item;

/// <summary>
/// Turns per-type counts into an <see cref="ItemCounts"/>.
/// </summary>
internal static class ItemCountBuilder
{
    /// <summary>
    /// Builds the counts of one by-name item.
    /// </summary>
    /// <param name="itemTypeLookup">The item type lookup.</param>
    /// <param name="counts">The counted items, by type name. A type may repeat.</param>
    /// <returns>The counts.</returns>
    public static ItemCounts Build(IItemTypeLookup itemTypeLookup, IEnumerable<(string Type, int Count)> counts)
    {
        ArgumentNullException.ThrowIfNull(itemTypeLookup);
        ArgumentNullException.ThrowIfNull(counts);

        var lookup = itemTypeLookup.BaseItemKindNames;
        var result = new ItemCounts();

        foreach (var (type, count) in counts)
        {
            // Accumulated rather than assigned: a caller may group by something finer than the
            // type and hand the same type over more than once.
            if (string.Equals(type, lookup[BaseItemKind.MusicAlbum], StringComparison.Ordinal))
            {
                result.AlbumCount += count;
            }
            else if (string.Equals(type, lookup[BaseItemKind.MusicArtist], StringComparison.Ordinal))
            {
                result.ArtistCount += count;
            }
            else if (string.Equals(type, lookup[BaseItemKind.Episode], StringComparison.Ordinal))
            {
                result.EpisodeCount += count;
            }
            else if (string.Equals(type, lookup[BaseItemKind.Movie], StringComparison.Ordinal))
            {
                result.MovieCount += count;
            }
            else if (string.Equals(type, lookup[BaseItemKind.MusicVideo], StringComparison.Ordinal))
            {
                result.MusicVideoCount += count;
            }
            else if (string.Equals(type, lookup[BaseItemKind.LiveTvProgram], StringComparison.Ordinal))
            {
                result.ProgramCount += count;
            }
            else if (string.Equals(type, lookup[BaseItemKind.Series], StringComparison.Ordinal))
            {
                result.SeriesCount += count;
            }
            else if (string.Equals(type, lookup[BaseItemKind.Audio], StringComparison.Ordinal))
            {
                result.SongCount += count;
            }
            else if (string.Equals(type, lookup[BaseItemKind.Trailer], StringComparison.Ordinal))
            {
                result.TrailerCount += count;
            }
            else if (string.Equals(type, lookup[BaseItemKind.BoxSet], StringComparison.Ordinal))
            {
                result.BoxSetCount += count;
            }
            else if (string.Equals(type, lookup[BaseItemKind.Book], StringComparison.Ordinal))
            {
                result.BookCount += count;
            }
        }

        result.ItemCount = result.TotalItemCount();

        return result;
    }

    /// <summary>
    /// Replaces the episode count, which both by-name paths decide separately from the other
    /// types because a genre or studio is usually written on the series rather than its episodes.
    /// </summary>
    /// <param name="counts">The counts to update.</param>
    /// <param name="episodeCount">The episode count.</param>
    public static void SetEpisodeCount(ItemCounts counts, int episodeCount)
    {
        ArgumentNullException.ThrowIfNull(counts);

        counts.EpisodeCount = episodeCount;
        counts.ItemCount = counts.TotalItemCount();
    }
}