aboutsummaryrefslogtreecommitdiff
path: root/MediaBrowser.Server.Implementations/Library/Validators/CountHelpers.cs
blob: ea4d887ea14d19a96d27c62faa08f1ade03826a0 (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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
using MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.Entities.Audio;
using MediaBrowser.Controller.Entities.Movies;
using MediaBrowser.Controller.Entities.TV;
using MediaBrowser.Model.Dto;
using System;
using System.Collections.Generic;

namespace MediaBrowser.Server.Implementations.Library.Validators
{
    /// <summary>
    /// Class CountHelpers
    /// </summary>
    internal static class CountHelpers
    {
        /// <summary>
        /// Adds to dictionary.
        /// </summary>
        /// <param name="item">The item.</param>
        /// <param name="counts">The counts.</param>
        internal static void AddToDictionary(BaseItem item, Dictionary<string, int> counts)
        {
            if (item is Movie)
            {
                IncrementCount(counts, "Movie");
            }
            else if (item is Trailer)
            {
                IncrementCount(counts, "Trailer");
            }
            else if (item is Series)
            {
                IncrementCount(counts, "Series");
            }
            else if (item is Game)
            {
                IncrementCount(counts, "Game");
            }
            else if (item is Audio)
            {
                IncrementCount(counts, "Audio");
            }
            else if (item is MusicAlbum)
            {
                IncrementCount(counts, "MusicAlbum");
            }
            else if (item is Episode)
            {
                IncrementCount(counts, "Episode");
            }
            else if (item is MusicVideo)
            {
                IncrementCount(counts, "MusicVideo");
            }
            else if (item is AdultVideo)
            {
                IncrementCount(counts, "AdultVideo");
            }

            IncrementCount(counts, "Total");
        }

        /// <summary>
        /// Increments the count.
        /// </summary>
        /// <param name="counts">The counts.</param>
        /// <param name="key">The key.</param>
        internal static void IncrementCount(Dictionary<string, int> counts, string key)
        {
            int count;

            if (counts.TryGetValue(key, out count))
            {
                count++;
                counts[key] = count;
            }
            else
            {
                counts.Add(key, 1);
            }
        }

        /// <summary>
        /// Gets the counts.
        /// </summary>
        /// <param name="counts">The counts.</param>
        /// <returns>ItemByNameCounts.</returns>
        internal static ItemByNameCounts GetCounts(Dictionary<string, int> counts)
        {
            return new ItemByNameCounts
            {
                AdultVideoCount = GetCount(counts, "AdultVideo"),
                AlbumCount = GetCount(counts, "MusicAlbum"),
                EpisodeCount = GetCount(counts, "Episode"),
                GameCount = GetCount(counts, "Game"),
                MovieCount = GetCount(counts, "Movie"),
                MusicVideoCount = GetCount(counts, "MusicVideo"),
                SeriesCount = GetCount(counts, "Series"),
                SongCount = GetCount(counts, "Audio"),
                TrailerCount = GetCount(counts, "Trailer"),
                TotalCount = GetCount(counts, "Total")
            };
        }

        /// <summary>
        /// Gets the count.
        /// </summary>
        /// <param name="counts">The counts.</param>
        /// <param name="key">The key.</param>
        /// <returns>System.Int32.</returns>
        internal static int GetCount(Dictionary<string, int> counts, string key)
        {
            int count;

            if (counts.TryGetValue(key, out count))
            {
                return count;
            }

            return 0;
        }

        /// <summary>
        /// Sets the item counts.
        /// </summary>
        /// <param name="userId">The user id.</param>
        /// <param name="media">The media.</param>
        /// <param name="names">The names.</param>
        /// <param name="masterDictionary">The master dictionary.</param>
        internal static void SetItemCounts(Guid? userId, BaseItem media, List<string> names, Dictionary<string, Dictionary<Guid, Dictionary<string, int>>> masterDictionary)
        {
            foreach (var name in names)
            {
                Dictionary<Guid, Dictionary<string, int>> libraryCounts;

                if (!masterDictionary.TryGetValue(name, out libraryCounts))
                {
                    libraryCounts = new Dictionary<Guid, Dictionary<string, int>>();
                    masterDictionary.Add(name, libraryCounts);
                }

                var userLibId = userId ?? Guid.Empty;
                Dictionary<string, int> userDictionary;

                if (!libraryCounts.TryGetValue(userLibId, out userDictionary))
                {
                    userDictionary = new Dictionary<string, int>();
                    libraryCounts.Add(userLibId, userDictionary);
                }

                AddToDictionary(media, userDictionary);
            }
        }
    }
}