YearsHandler.cs 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Threading.Tasks;
  5. using MediaBrowser.Common.Net.Handlers;
  6. using MediaBrowser.Controller;
  7. using MediaBrowser.Model.DTO;
  8. using MediaBrowser.Model.Entities;
  9. namespace MediaBrowser.Api.HttpHandlers
  10. {
  11. public class YearsHandler : BaseSerializationHandler<IBNItem[]>
  12. {
  13. protected override Task<IBNItem[]> GetObjectToSerialize()
  14. {
  15. Folder parent = ApiService.GetItemById(QueryString["id"]) as Folder;
  16. Guid userId = Guid.Parse(QueryString["userid"]);
  17. User user = Kernel.Instance.Users.First(u => u.Id == userId);
  18. return GetAllYears(parent, user);
  19. }
  20. /// <summary>
  21. /// Gets all years from all recursive children of a folder
  22. /// The CategoryInfo class is used to keep track of the number of times each year appears
  23. /// </summary>
  24. private async Task<IBNItem[]> GetAllYears(Folder parent, User user)
  25. {
  26. Dictionary<int, int> data = new Dictionary<int, int>();
  27. // Get all the allowed recursive children
  28. IEnumerable<BaseItem> allItems = parent.GetParentalAllowedRecursiveChildren(user);
  29. foreach (var item in allItems)
  30. {
  31. // Add the year from the item to the data dictionary
  32. // If the year already exists, increment the count
  33. if (item.ProductionYear == null)
  34. {
  35. continue;
  36. }
  37. if (!data.ContainsKey(item.ProductionYear.Value))
  38. {
  39. data.Add(item.ProductionYear.Value, 1);
  40. }
  41. else
  42. {
  43. data[item.ProductionYear.Value]++;
  44. }
  45. }
  46. // Get the Year objects
  47. Year[] entities = await Task.WhenAll<Year>(data.Keys.Select(key => { return Kernel.Instance.ItemController.GetYear(key); })).ConfigureAwait(false);
  48. // Convert to an array of IBNItem
  49. IBNItem[] items = new IBNItem[entities.Length];
  50. for (int i = 0; i < entities.Length; i++)
  51. {
  52. Year e = entities[i];
  53. items[i] = ApiService.GetIBNItem(e, data[int.Parse(e.Name)]);
  54. }
  55. return items;
  56. }
  57. }
  58. }