using MediaBrowser.Common.Net; using MediaBrowser.Controller; using MediaBrowser.Controller.Entities; using ServiceStack.ServiceHost; using System; using System.Collections.Generic; using System.ComponentModel.Composition; using System.Linq; using System.Threading.Tasks; namespace MediaBrowser.Api.UserLibrary { /// /// Class GetPersons /// [Route("/Users/{UserId}/Items/{Id}/Persons", "GET")] [Route("/Users/{UserId}/Items/Root/Persons", "GET")] public class GetPersons : GetItemsByName { /// /// Gets or sets the person types. /// /// The person types. public string PersonTypes { get; set; } } /// /// Class PersonsService /// [Export(typeof(IRestfulService))] public class PersonsService : BaseItemsByNameService { /// /// Gets the specified request. /// /// The request. /// System.Object. public object Get(GetPersons request) { var result = GetResult(request).Result; return ToOptimizedResult(result); } /// /// 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 Tuple>(name, () => { if (personTypes.Length == 0) { return itemsList.Count(i => i.People.Any(p => p.Name.Equals(name, StringComparison.OrdinalIgnoreCase))); } return itemsList.Count(i => i.People.Any(p => p.Name.Equals(name, StringComparison.OrdinalIgnoreCase) && personTypes.Contains(p.Type ?? string.Empty, StringComparer.OrdinalIgnoreCase))); }) ); } /// /// Gets all people. /// /// The items list. /// The person types. /// IEnumerable{PersonInfo}. private IEnumerable GetAllPeople(IEnumerable itemsList, string[] personTypes) { return personTypes.Length == 0 ? itemsList.SelectMany(i => i.People) : itemsList.SelectMany(i => i.People.Where(p => personTypes.Contains(p.Type ?? string.Empty, StringComparer.OrdinalIgnoreCase))); } /// /// Gets the entity. /// /// The name. /// Task{Genre}. protected override Task GetEntity(string name) { var kernel = (Kernel)Kernel; return kernel.LibraryManager.GetPerson(name); } } }