using MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.Library;
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; }
}
[Route("/Users/{UserId}/FavoritePersons/{Name}", "POST")]
[Api(Description = "Marks a person as a favorite")]
public class MarkFavoritePerson : IReturnVoid
{
///
/// Gets or sets the user id.
///
/// The user id.
[ApiMember(Name = "UserId", Description = "User Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "POST")]
public Guid UserId { get; set; }
///
/// Gets or sets the name.
///
/// The name.
[ApiMember(Name = "Name", Description = "Name", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "DELETE")]
public string Name { get; set; }
}
[Route("/Users/{UserId}/FavoritePersons/{Name}", "DELETE")]
[Api(Description = "Unmarks a person as a favorite")]
public class UnmarkFavoritePerson : IReturnVoid
{
///
/// Gets or sets the user id.
///
/// The user id.
[ApiMember(Name = "UserId", Description = "User Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "DELETE")]
public Guid UserId { get; set; }
///
/// Gets or sets the name.
///
/// The name.
[ApiMember(Name = "Name", Description = "Name", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "DELETE")]
public string Name { get; set; }
}
///
/// Class PersonsService
///
public class PersonsService : BaseItemsByNameService
{
public PersonsService(IUserManager userManager, ILibraryManager libraryManager)
: base(userManager, libraryManager)
{
}
///
/// Gets the specified request.
///
/// The request.
/// System.Object.
public object Get(GetPersons request)
{
var result = GetResult(request).Result;
return ToOptimizedResult(result);
}
///
/// Posts the specified request.
///
/// The request.
public void Post(MarkFavoritePerson request)
{
var task = MarkFavorite(() => LibraryManager.GetPerson(request.Name), request.UserId, true);
Task.WaitAll(task);
}
///
/// Deletes the specified request.
///
/// The request.
public void Delete(UnmarkFavoritePerson request)
{
var task = MarkFavorite(() => LibraryManager.GetPerson(request.Name), request.UserId, false);
Task.WaitAll(task);
}
///
/// 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.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)));
})
);
}
///
/// 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 override Task GetEntity(string name)
{
return LibraryManager.GetPerson(name);
}
}
}