2012-09-07 16:17:39 +00:00
|
|
|
|
using MediaBrowser.Common.Net.Handlers;
|
2012-08-14 16:06:46 +00:00
|
|
|
|
using MediaBrowser.Controller;
|
2012-09-11 01:34:02 +00:00
|
|
|
|
using MediaBrowser.Controller.Entities;
|
2012-08-17 13:16:50 +00:00
|
|
|
|
using MediaBrowser.Model.DTO;
|
2012-09-07 16:17:39 +00:00
|
|
|
|
using System.Collections.Generic;
|
2012-09-08 14:52:13 +00:00
|
|
|
|
using System.ComponentModel.Composition;
|
2012-09-07 16:17:39 +00:00
|
|
|
|
using System.Linq;
|
2012-09-08 14:52:13 +00:00
|
|
|
|
using System.Net;
|
2012-09-07 16:17:39 +00:00
|
|
|
|
using System.Threading.Tasks;
|
2012-08-14 16:06:46 +00:00
|
|
|
|
|
|
|
|
|
namespace MediaBrowser.Api.HttpHandlers
|
|
|
|
|
{
|
2012-09-08 14:52:13 +00:00
|
|
|
|
[Export(typeof(BaseHandler))]
|
2012-09-11 19:37:14 +00:00
|
|
|
|
public class YearsHandler : BaseSerializationHandler<IbnItem[]>
|
2012-08-14 16:06:46 +00:00
|
|
|
|
{
|
2012-09-08 14:52:13 +00:00
|
|
|
|
public override bool HandlesRequest(HttpListenerRequest request)
|
|
|
|
|
{
|
|
|
|
|
return ApiService.IsApiUrlMatch("years", request);
|
|
|
|
|
}
|
2012-09-11 19:37:14 +00:00
|
|
|
|
|
|
|
|
|
protected override Task<IbnItem[]> GetObjectToSerialize()
|
2012-08-14 16:06:46 +00:00
|
|
|
|
{
|
2012-09-11 19:37:14 +00:00
|
|
|
|
var parent = ApiService.GetItemById(QueryString["id"]) as Folder;
|
2012-09-07 16:17:39 +00:00
|
|
|
|
User user = ApiService.GetUserById(QueryString["userid"], true);
|
2012-08-14 16:06:46 +00:00
|
|
|
|
|
2012-08-22 02:50:59 +00:00
|
|
|
|
return GetAllYears(parent, user);
|
2012-08-17 19:07:21 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <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>
|
2012-09-11 19:37:14 +00:00
|
|
|
|
private async Task<IbnItem[]> GetAllYears(Folder parent, User user)
|
2012-08-17 19:07:21 +00:00
|
|
|
|
{
|
2012-09-11 19:37:14 +00:00
|
|
|
|
var data = new Dictionary<int, int>();
|
2012-08-17 19:07:21 +00:00
|
|
|
|
|
|
|
|
|
// Get all the allowed recursive children
|
2012-09-17 15:12:43 +00:00
|
|
|
|
IEnumerable<BaseItem> allItems = parent.GetRecursiveChildren(user);
|
2012-08-17 19:07:21 +00:00
|
|
|
|
|
|
|
|
|
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]++;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-08-29 19:37:05 +00:00
|
|
|
|
// Get the Year objects
|
2012-09-11 19:37:14 +00:00
|
|
|
|
Year[] entities = await Task.WhenAll(data.Keys.Select(key => Kernel.Instance.ItemController.GetYear(key))).ConfigureAwait(false);
|
2012-08-17 19:07:21 +00:00
|
|
|
|
|
2012-08-29 19:37:05 +00:00
|
|
|
|
// Convert to an array of IBNItem
|
2012-09-11 19:37:14 +00:00
|
|
|
|
var items = new IbnItem[entities.Length];
|
2012-08-29 19:37:05 +00:00
|
|
|
|
|
|
|
|
|
for (int i = 0; i < entities.Length; i++)
|
|
|
|
|
{
|
|
|
|
|
Year e = entities[i];
|
|
|
|
|
|
2012-09-11 19:37:14 +00:00
|
|
|
|
items[i] = ApiService.GetIbnItem(e, data[int.Parse(e.Name)]);
|
2012-08-29 19:37:05 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return items;
|
2012-08-14 16:06:46 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|