YearsHandler.cs 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using MediaBrowser.Common.Net.Handlers;
  5. using MediaBrowser.Controller;
  6. using MediaBrowser.Model.DTO;
  7. using MediaBrowser.Model.Entities;
  8. namespace MediaBrowser.Api.HttpHandlers
  9. {
  10. public class YearsHandler : BaseJsonHandler<IEnumerable<IBNItem<Year>>>
  11. {
  12. protected override IEnumerable<IBNItem<Year>> GetObjectToSerialize()
  13. {
  14. Folder parent = ApiService.GetItemById(QueryString["id"]) as Folder;
  15. Guid userId = Guid.Parse(QueryString["userid"]);
  16. User user = Kernel.Instance.Users.First(u => u.Id == userId);
  17. return GetAllYears(parent, user);
  18. }
  19. /// <summary>
  20. /// Gets all years from all recursive children of a folder
  21. /// The CategoryInfo class is used to keep track of the number of times each year appears
  22. /// </summary>
  23. private IEnumerable<IBNItem<Year>> GetAllYears(Folder parent, User user)
  24. {
  25. Dictionary<int, int> data = new Dictionary<int, int>();
  26. // Get all the allowed recursive children
  27. IEnumerable<BaseItem> allItems = parent.GetParentalAllowedRecursiveChildren(user);
  28. foreach (var item in allItems)
  29. {
  30. // Add the year from the item to the data dictionary
  31. // If the year already exists, increment the count
  32. if (item.ProductionYear == null)
  33. {
  34. continue;
  35. }
  36. if (!data.ContainsKey(item.ProductionYear.Value))
  37. {
  38. data.Add(item.ProductionYear.Value, 1);
  39. }
  40. else
  41. {
  42. data[item.ProductionYear.Value]++;
  43. }
  44. }
  45. // Now go through the dictionary and create a Category for each studio
  46. List<IBNItem<Year>> list = new List<IBNItem<Year>>();
  47. foreach (int key in data.Keys)
  48. {
  49. // Get the original entity so that we can also supply the PrimaryImagePath
  50. Year entity = Kernel.Instance.ItemController.GetYear(key);
  51. if (entity != null)
  52. {
  53. list.Add(new IBNItem<Year>()
  54. {
  55. Item = entity,
  56. BaseItemCount = data[key]
  57. });
  58. }
  59. }
  60. return list;
  61. }
  62. }
  63. }