StudiosHandler.cs 2.6 KB

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