YearsHandler.cs 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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. using MediaBrowser.Model.Users;
  9. namespace MediaBrowser.Api.HttpHandlers
  10. {
  11. public class YearsHandler : BaseJsonHandler<IEnumerable<IBNItem<Year>>>
  12. {
  13. protected override IEnumerable<IBNItem<Year>> 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 IEnumerable<IBNItem<Year>> 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. // Now go through the dictionary and create a Category for each studio
  47. List<IBNItem<Year>> list = new List<IBNItem<Year>>();
  48. foreach (int key in data.Keys)
  49. {
  50. // Get the original entity so that we can also supply the PrimaryImagePath
  51. Year entity = Kernel.Instance.ItemController.GetYear(key);
  52. if (entity != null)
  53. {
  54. list.Add(new IBNItem<Year>()
  55. {
  56. Item = entity,
  57. BaseItemCount = data[key]
  58. });
  59. }
  60. }
  61. return list;
  62. }
  63. }
  64. }