using MediaBrowser.Controller.Entities; using MediaBrowser.Controller.Entities.Movies; using MediaBrowser.Controller.Entities.TV; using MediaBrowser.Controller.Library; using MediaBrowser.Controller.Persistence; using MediaBrowser.Model.Dto; using MediaBrowser.Model.Entities; using ServiceStack.ServiceHost; using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; namespace MediaBrowser.Api.UserLibrary { /// /// Class GetPersons /// [Route("/Users/{UserId}/Items/{ParentId}/Persons", "GET")] [Route("/Users/{UserId}/Items/Root/Persons", "GET")] [Api(Description = "Gets all persons from a given item, folder, or the entire library")] public class GetPersons : GetItemsByName { /// /// Gets or sets the person types. /// /// The person types. public string PersonTypes { get; set; } } /// /// Class GetPersonItemCounts /// [Route("/Users/{UserId}/Persons/{Name}/Counts", "GET")] [Api(Description = "Gets item counts of library items that a person appears in")] public class GetPersonItemCounts : IReturn { /// /// Gets or sets the user id. /// /// The user id. public Guid UserId { get; set; } /// /// Gets or sets the name. /// /// The name. public string Name { get; set; } } /// /// Class PersonsService /// public class PersonsService : BaseItemsByNameService { /// /// Initializes a new instance of the class. /// /// The user manager. /// The library manager. /// The user data repository. public PersonsService(IUserManager userManager, ILibraryManager libraryManager, IUserDataRepository userDataRepository) : base(userManager, libraryManager, userDataRepository) { } /// /// Gets the specified request. /// /// The request. /// System.Object. public object Get(GetPersons request) { var result = GetResult(request).Result; return ToOptimizedResult(result); } /// /// Gets the specified request. /// /// The request. /// System.Object. public object Get(GetPersonItemCounts request) { var user = UserManager.GetUserById(request.UserId); var items = user.RootFolder.GetRecursiveChildren(user).Where(i => i.People != null && i.People.Any(p => string.Equals(p.Name, request.Name, StringComparison.OrdinalIgnoreCase))).ToList(); var counts = new ItemByNameCounts { TotalCount = items.Count, MovieCount = items.OfType().Count(), SeriesCount = items.OfType().Count(), GameCount = items.OfType().Count(), EpisodeGuestStarCount = items.OfType().Count(i => i.People.Any(p => string.Equals(p.Name, request.Name, StringComparison.OrdinalIgnoreCase) && string.Equals(p.Type, PersonType.GuestStar))) }; return ToOptimizedResult(counts); } /// /// Gets all items. /// /// The request. /// The items. /// The user. /// IEnumerable{Tuple{System.StringFunc{System.Int32}}}. protected override IEnumerable> GetAllItems(GetItemsByName request, IEnumerable items, User user) { var inputPersonTypes = ((GetPersons)request).PersonTypes; var personTypes = string.IsNullOrEmpty(inputPersonTypes) ? new string[] { } : inputPersonTypes.Split(','); var itemsList = items.Where(i => i.People != null).ToList(); // Either get all people, or all people filtered by a specific person type var allPeople = GetAllPeople(itemsList, personTypes); return allPeople .Select(i => i.Name) .Distinct(StringComparer.OrdinalIgnoreCase) .Select(name => new IbnStub(name, () => { if (personTypes.Length == 0) { return itemsList.Where(i => i.People.Any(p => p.Name.Equals(name, StringComparison.OrdinalIgnoreCase))); } return itemsList.Where(i => i.People.Any(p => p.Name.Equals(name, StringComparison.OrdinalIgnoreCase) && personTypes.Contains(p.Type ?? string.Empty, StringComparer.OrdinalIgnoreCase))); }, GetEntity) ); } /// /// Gets all people. /// /// The items list. /// The person types. /// IEnumerable{PersonInfo}. private IEnumerable GetAllPeople(IEnumerable itemsList, string[] personTypes) { var people = itemsList.SelectMany(i => i.People.OrderBy(p => p.Type)); return personTypes.Length == 0 ? people : people.Where(p => personTypes.Contains(p.Type ?? string.Empty, StringComparer.OrdinalIgnoreCase)); } /// /// Gets the entity. /// /// The name. /// Task{Genre}. protected Task GetEntity(string name) { return LibraryManager.GetPerson(name); } } }