2012-07-16 18:50:44 +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-19 22:38:31 +02:00
|
|
|
|
using System.Threading.Tasks;
|
2012-08-15 15:20:29 +02:00
|
|
|
|
using MediaBrowser.Common.Net.Handlers;
|
2012-07-20 04:22:44 +02:00
|
|
|
|
using MediaBrowser.Controller;
|
2012-08-17 15:16:50 +02:00
|
|
|
|
using MediaBrowser.Model.DTO;
|
2012-07-13 05:50:50 +02:00
|
|
|
|
using MediaBrowser.Model.Entities;
|
|
|
|
|
|
|
|
|
|
namespace MediaBrowser.Api.HttpHandlers
|
|
|
|
|
{
|
2012-08-17 18:47:35 +02:00
|
|
|
|
public class GenresHandler : BaseJsonHandler<IEnumerable<IBNItem<Genre>>>
|
2012-07-13 05:50:50 +02:00
|
|
|
|
{
|
2012-08-19 22:38:31 +02:00
|
|
|
|
protected override async Task<IEnumerable<IBNItem<Genre>>> GetObjectToSerialize()
|
2012-07-13 05:50:50 +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-07-13 05:50:50 +02:00
|
|
|
|
|
2012-08-19 22:38:31 +02:00
|
|
|
|
return await GetAllGenres(parent, user);
|
2012-08-17 21:07:21 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets all genres from all recursive children of a folder
|
|
|
|
|
/// The CategoryInfo class is used to keep track of the number of times each genres appears
|
|
|
|
|
/// </summary>
|
2012-08-19 22:38:31 +02:00
|
|
|
|
private async Task<IEnumerable<IBNItem<Genre>>> GetAllGenres(Folder parent, User user)
|
2012-08-17 21:07:21 +02: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 genre from the item to the data dictionary
|
|
|
|
|
// If the genre already exists, increment the count
|
|
|
|
|
if (item.Genres == null)
|
|
|
|
|
{
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
foreach (string val in item.Genres)
|
|
|
|
|
{
|
|
|
|
|
if (!data.ContainsKey(val))
|
|
|
|
|
{
|
|
|
|
|
data.Add(val, 1);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
data[val]++;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-08-19 22:38:31 +02:00
|
|
|
|
IEnumerable<Genre> entities = await Task.WhenAll<Genre>(data.Keys.Select(key => { return Kernel.Instance.ItemController.GetGenre(key); }));
|
2012-08-17 21:07:21 +02:00
|
|
|
|
|
2012-08-19 22:38:31 +02:00
|
|
|
|
return entities.Select(e => new IBNItem<Genre>() { Item = e, BaseItemCount = data[e.Name] });
|
2012-07-13 05:50:50 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|