jellyfin/MediaBrowser.Api/HttpHandlers/StudiosHandler.cs

79 lines
2.6 KiB
C#
Raw Normal View History

2012-09-07 16:17:39 +00:00
using MediaBrowser.Common.Net.Handlers;
using MediaBrowser.Controller;
using MediaBrowser.Controller.Entities;
using MediaBrowser.Model.DTO;
2012-09-07 16:17:39 +00:00
using System.Collections.Generic;
using System.ComponentModel.Composition;
2012-09-07 16:17:39 +00:00
using System.Linq;
using System.Net;
2012-09-07 16:17:39 +00:00
using System.Threading.Tasks;
namespace MediaBrowser.Api.HttpHandlers
{
[Export(typeof(BaseHandler))]
2012-09-02 05:30:25 +00:00
public class StudiosHandler : BaseSerializationHandler<IBNItem[]>
{
public override bool HandlesRequest(HttpListenerRequest request)
{
return ApiService.IsApiUrlMatch("studios", request);
}
2012-08-29 19:37:05 +00:00
protected override Task<IBNItem[]> GetObjectToSerialize()
{
Folder parent = ApiService.GetItemById(QueryString["id"]) as Folder;
2012-09-07 16:17:39 +00:00
User user = ApiService.GetUserById(QueryString["userid"], true);
2012-08-22 02:50:59 +00:00
return GetAllStudios(parent, user);
}
/// <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-29 19:37:05 +00:00
private async Task<IBNItem[]> GetAllStudios(Folder parent, User user)
{
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-29 19:37:05 +00:00
// Get the Studio objects
Studio[] entities = await Task.WhenAll<Studio>(data.Keys.Select(key => { return Kernel.Instance.ItemController.GetStudio(key); })).ConfigureAwait(false);
2012-08-29 19:37:05 +00:00
// Convert to an array of IBNItem
IBNItem[] items = new IBNItem[entities.Length];
for (int i = 0; i < entities.Length; i++)
{
Studio e = entities[i];
items[i] = ApiService.GetIBNItem(e, data[e.Name]);
}
return items;
}
}
}