2012-07-16 16:50:44 +00:00
|
|
|
|
using System;
|
2012-08-17 13:16:50 +00:00
|
|
|
|
using System.Collections.Generic;
|
2012-08-17 17:37:26 +00:00
|
|
|
|
using System.Linq;
|
2012-08-19 20:38:31 +00:00
|
|
|
|
using System.Threading.Tasks;
|
2012-08-15 13:20:29 +00:00
|
|
|
|
using MediaBrowser.Common.Net.Handlers;
|
2012-07-20 02:22:44 +00:00
|
|
|
|
using MediaBrowser.Controller;
|
2012-08-17 13:16:50 +00:00
|
|
|
|
using MediaBrowser.Model.DTO;
|
2012-07-16 16:50:44 +00:00
|
|
|
|
using MediaBrowser.Model.Entities;
|
|
|
|
|
|
|
|
|
|
namespace MediaBrowser.Api.HttpHandlers
|
|
|
|
|
{
|
2012-08-17 16:47:35 +00:00
|
|
|
|
public class StudiosHandler : BaseJsonHandler<IEnumerable<IBNItem<Studio>>>
|
2012-07-16 16:50:44 +00:00
|
|
|
|
{
|
2012-08-22 02:50:59 +00:00
|
|
|
|
protected override Task<IEnumerable<IBNItem<Studio>>> GetObjectToSerialize()
|
2012-07-16 16:50:44 +00:00
|
|
|
|
{
|
2012-08-15 13:20:29 +00:00
|
|
|
|
Folder parent = ApiService.GetItemById(QueryString["id"]) as Folder;
|
|
|
|
|
Guid userId = Guid.Parse(QueryString["userid"]);
|
2012-08-17 17:37:26 +00:00
|
|
|
|
User user = Kernel.Instance.Users.First(u => u.Id == userId);
|
|
|
|
|
|
2012-08-22 02:50:59 +00:00
|
|
|
|
return GetAllStudios(parent, user);
|
2012-08-17 19:07:21 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets all studios from all recursive children of a folder
|
|
|
|
|
/// The CategoryInfo class is used to keep track of the number of times each studio appears
|
|
|
|
|
/// </summary>
|
2012-08-19 20:38:31 +00:00
|
|
|
|
private async Task<IEnumerable<IBNItem<Studio>>> GetAllStudios(Folder parent, User user)
|
2012-08-17 19:07:21 +00:00
|
|
|
|
{
|
|
|
|
|
Dictionary<string, int> data = new Dictionary<string, int>();
|
|
|
|
|
|
|
|
|
|
// Get all the allowed recursive children
|
|
|
|
|
IEnumerable<BaseItem> allItems = parent.GetParentalAllowedRecursiveChildren(user);
|
|
|
|
|
|
|
|
|
|
foreach (var item in allItems)
|
|
|
|
|
{
|
|
|
|
|
// Add each studio from the item to the data dictionary
|
|
|
|
|
// If the studio already exists, increment the count
|
|
|
|
|
if (item.Studios == null)
|
|
|
|
|
{
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
foreach (string val in item.Studios)
|
|
|
|
|
{
|
|
|
|
|
if (!data.ContainsKey(val))
|
|
|
|
|
{
|
|
|
|
|
data.Add(val, 1);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
data[val]++;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-08-22 02:50:59 +00:00
|
|
|
|
IEnumerable<Studio> entities = await Task.WhenAll<Studio>(data.Keys.Select(key => { return Kernel.Instance.ItemController.GetStudio(key); })).ConfigureAwait(false);
|
2012-08-17 19:07:21 +00:00
|
|
|
|
|
2012-08-19 20:38:31 +00:00
|
|
|
|
return entities.Select(e => new IBNItem<Studio>() { Item = e, BaseItemCount = data[e.Name] });
|
2012-07-16 16:50:44 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|