PersonsService.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. using MediaBrowser.Controller.Entities;
  2. using MediaBrowser.Controller.Library;
  3. using ServiceStack.ServiceHost;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using System.Threading.Tasks;
  8. namespace MediaBrowser.Api.UserLibrary
  9. {
  10. /// <summary>
  11. /// Class GetPersons
  12. /// </summary>
  13. [Route("/Users/{UserId}/Items/{ParentId}/Persons", "GET")]
  14. [Route("/Users/{UserId}/Items/Root/Persons", "GET")]
  15. [ServiceStack.ServiceHost.Api(Description = "Gets all persons from a given item, folder, or the entire library")]
  16. public class GetPersons : GetItemsByName
  17. {
  18. /// <summary>
  19. /// Gets or sets the person types.
  20. /// </summary>
  21. /// <value>The person types.</value>
  22. public string PersonTypes { get; set; }
  23. }
  24. /// <summary>
  25. /// Class PersonsService
  26. /// </summary>
  27. public class PersonsService : BaseItemsByNameService<Person>
  28. {
  29. public PersonsService(IUserManager userManager, ILibraryManager libraryManager)
  30. : base(userManager, libraryManager)
  31. {
  32. }
  33. /// <summary>
  34. /// Gets the specified request.
  35. /// </summary>
  36. /// <param name="request">The request.</param>
  37. /// <returns>System.Object.</returns>
  38. public object Get(GetPersons request)
  39. {
  40. var result = GetResult(request).Result;
  41. return ToOptimizedResult(result);
  42. }
  43. /// <summary>
  44. /// Gets all items.
  45. /// </summary>
  46. /// <param name="request">The request.</param>
  47. /// <param name="items">The items.</param>
  48. /// <param name="user">The user.</param>
  49. /// <returns>IEnumerable{Tuple{System.StringFunc{System.Int32}}}.</returns>
  50. protected override IEnumerable<Tuple<string, Func<int>>> GetAllItems(GetItemsByName request, IEnumerable<BaseItem> items, User user)
  51. {
  52. var inputPersonTypes = ((GetPersons) request).PersonTypes;
  53. var personTypes = string.IsNullOrEmpty(inputPersonTypes) ? new string[] { } : inputPersonTypes.Split(',');
  54. var itemsList = items.Where(i => i.People != null).ToList();
  55. // Either get all people, or all people filtered by a specific person type
  56. var allPeople = GetAllPeople(itemsList, personTypes);
  57. return allPeople
  58. .Select(i => i.Name)
  59. .Distinct(StringComparer.OrdinalIgnoreCase)
  60. .Select(name => new Tuple<string, Func<int>>(name, () =>
  61. {
  62. if (personTypes.Length == 0)
  63. {
  64. return itemsList.Count(i => i.People.Any(p => p.Name.Equals(name, StringComparison.OrdinalIgnoreCase)));
  65. }
  66. return itemsList.Count(i => i.People.Any(p => p.Name.Equals(name, StringComparison.OrdinalIgnoreCase) && personTypes.Contains(p.Type ?? string.Empty, StringComparer.OrdinalIgnoreCase)));
  67. })
  68. );
  69. }
  70. /// <summary>
  71. /// Gets all people.
  72. /// </summary>
  73. /// <param name="itemsList">The items list.</param>
  74. /// <param name="personTypes">The person types.</param>
  75. /// <returns>IEnumerable{PersonInfo}.</returns>
  76. private IEnumerable<PersonInfo> GetAllPeople(IEnumerable<BaseItem> itemsList, string[] personTypes)
  77. {
  78. return personTypes.Length == 0 ?
  79. itemsList.SelectMany(i => i.People) :
  80. itemsList.SelectMany(i => i.People.Where(p => personTypes.Contains(p.Type ?? string.Empty, StringComparer.OrdinalIgnoreCase)));
  81. }
  82. /// <summary>
  83. /// Gets the entity.
  84. /// </summary>
  85. /// <param name="name">The name.</param>
  86. /// <returns>Task{Genre}.</returns>
  87. protected override Task<Person> GetEntity(string name)
  88. {
  89. return LibraryManager.GetPerson(name);
  90. }
  91. }
  92. }