2012-08-14 18:06:46 +02:00
|
|
|
|
using System;
|
2012-08-17 15:16:50 +02:00
|
|
|
|
using System.Collections.Generic;
|
2012-08-17 19:37:26 +02:00
|
|
|
|
using System.Linq;
|
2012-08-15 15:20:29 +02:00
|
|
|
|
using MediaBrowser.Common.Net.Handlers;
|
2012-08-14 18:06:46 +02:00
|
|
|
|
using MediaBrowser.Controller;
|
2012-08-17 15:16:50 +02:00
|
|
|
|
using MediaBrowser.Model.DTO;
|
2012-08-14 18:06:46 +02:00
|
|
|
|
using MediaBrowser.Model.Entities;
|
|
|
|
|
|
|
|
|
|
namespace MediaBrowser.Api.HttpHandlers
|
|
|
|
|
{
|
2012-08-17 18:47:35 +02:00
|
|
|
|
public class YearsHandler : BaseJsonHandler<IEnumerable<IBNItem<Year>>>
|
2012-08-14 18:06:46 +02:00
|
|
|
|
{
|
2012-08-17 18:47:35 +02:00
|
|
|
|
protected override IEnumerable<IBNItem<Year>> GetObjectToSerialize()
|
2012-08-14 18:06:46 +02:00
|
|
|
|
{
|
2012-08-15 15:20:29 +02:00
|
|
|
|
Folder parent = ApiService.GetItemById(QueryString["id"]) as Folder;
|
|
|
|
|
Guid userId = Guid.Parse(QueryString["userid"]);
|
2012-08-17 19:37:26 +02:00
|
|
|
|
User user = Kernel.Instance.Users.First(u => u.Id == userId);
|
2012-08-14 18:06:46 +02:00
|
|
|
|
|
2012-08-17 21:07:21 +02:00
|
|
|
|
return GetAllYears(parent, user);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets all years from all recursive children of a folder
|
|
|
|
|
/// The CategoryInfo class is used to keep track of the number of times each year appears
|
|
|
|
|
/// </summary>
|
|
|
|
|
private IEnumerable<IBNItem<Year>> GetAllYears(Folder parent, User user)
|
|
|
|
|
{
|
|
|
|
|
Dictionary<int, int> data = new Dictionary<int, int>();
|
|
|
|
|
|
|
|
|
|
// Get all the allowed recursive children
|
|
|
|
|
IEnumerable<BaseItem> allItems = parent.GetParentalAllowedRecursiveChildren(user);
|
|
|
|
|
|
|
|
|
|
foreach (var item in allItems)
|
|
|
|
|
{
|
|
|
|
|
// Add the year from the item to the data dictionary
|
|
|
|
|
// If the year already exists, increment the count
|
|
|
|
|
if (item.ProductionYear == null)
|
|
|
|
|
{
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!data.ContainsKey(item.ProductionYear.Value))
|
|
|
|
|
{
|
|
|
|
|
data.Add(item.ProductionYear.Value, 1);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
data[item.ProductionYear.Value]++;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Now go through the dictionary and create a Category for each studio
|
|
|
|
|
List<IBNItem<Year>> list = new List<IBNItem<Year>>();
|
|
|
|
|
|
|
|
|
|
foreach (int key in data.Keys)
|
|
|
|
|
{
|
|
|
|
|
// Get the original entity so that we can also supply the PrimaryImagePath
|
|
|
|
|
Year entity = Kernel.Instance.ItemController.GetYear(key);
|
|
|
|
|
|
|
|
|
|
if (entity != null)
|
|
|
|
|
{
|
|
|
|
|
list.Add(new IBNItem<Year>()
|
|
|
|
|
{
|
|
|
|
|
Item = entity,
|
|
|
|
|
BaseItemCount = data[key]
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return list;
|
2012-08-14 18:06:46 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|