StudiosHandler.cs 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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 StudiosHandler : BaseJsonHandler<IEnumerable<IBNItem<Studio>>>
  11. {
  12. protected override IEnumerable<IBNItem<Studio>> 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 GetAllStudios(parent, user);
  18. }
  19. /// <summary>
  20. /// Gets all studios from all recursive children of a folder
  21. /// The CategoryInfo class is used to keep track of the number of times each studio appears
  22. /// </summary>
  23. private IEnumerable<IBNItem<Studio>> GetAllStudios(Folder parent, User user)
  24. {
  25. Dictionary<string, int> data = new Dictionary<string, int>();
  26. // Get all the allowed recursive children
  27. IEnumerable<BaseItem> allItems = parent.GetParentalAllowedRecursiveChildren(user);
  28. foreach (var item in allItems)
  29. {
  30. // Add each studio from the item to the data dictionary
  31. // If the studio already exists, increment the count
  32. if (item.Studios == null)
  33. {
  34. continue;
  35. }
  36. foreach (string val in item.Studios)
  37. {
  38. if (!data.ContainsKey(val))
  39. {
  40. data.Add(val, 1);
  41. }
  42. else
  43. {
  44. data[val]++;
  45. }
  46. }
  47. }
  48. // Now go through the dictionary and create a Category for each studio
  49. List<IBNItem<Studio>> list = new List<IBNItem<Studio>>();
  50. foreach (string key in data.Keys)
  51. {
  52. // Get the original entity so that we can also supply the PrimaryImagePath
  53. Studio entity = Kernel.Instance.ItemController.GetStudio(key);
  54. if (entity != null)
  55. {
  56. list.Add(new IBNItem<Studio>()
  57. {
  58. Item = entity,
  59. BaseItemCount = data[key]
  60. });
  61. }
  62. }
  63. return list;
  64. }
  65. }
  66. }