aboutsummaryrefslogtreecommitdiff
path: root/MediaBrowser.Api/HttpHandlers/YearHandler.cs
blob: 1b0c7f6704d4762387f64cdf8fc968b6ca3b36ac (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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using MediaBrowser.Common.Net.Handlers;
using MediaBrowser.Controller;
using MediaBrowser.Model.DTO;
using MediaBrowser.Model.Entities;

namespace MediaBrowser.Api.HttpHandlers
{
    /// <summary>
    /// Gets a single year
    /// </summary>
    public class YearHandler : BaseSerializationHandler<IBNItem>
    {
        protected override Task<IBNItem> GetObjectToSerialize()
        {
            Folder parent = ApiService.GetItemById(QueryString["id"]) as Folder;
            Guid userId = Guid.Parse(QueryString["userid"]);
            User user = Kernel.Instance.Users.First(u => u.Id == userId);

            string year = QueryString["year"];

            return GetYear(parent, user, int.Parse(year));
        }

        /// <summary>
        /// Gets a Year
        /// </summary>
        private async Task<IBNItem> GetYear(Folder parent, User user, int year)
        {
            int count = 0;

            // Get all the allowed recursive children
            IEnumerable<BaseItem> allItems = parent.GetParentalAllowedRecursiveChildren(user);

            foreach (var item in allItems)
            {
                if (item.ProductionYear.HasValue && item.ProductionYear.Value == year)
                {
                    count++;
                }
            }

            // Get the original entity so that we can also supply the PrimaryImagePath
            return ApiService.GetIBNItem(await Kernel.Instance.ItemController.GetYear(year).ConfigureAwait(false), count);
        }
    }
}