using MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.Library;
using MediaBrowser.Controller.Persistence;
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 PersonsService
///
public class PersonsService : BaseItemsByNameService
{
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 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);
}
}
}