2013-03-08 21:56:07 +00:00
|
|
|
|
using MediaBrowser.Controller.Entities;
|
2013-02-27 20:25:45 +00:00
|
|
|
|
using MediaBrowser.Controller.Library;
|
2013-02-21 01:33:05 +00:00
|
|
|
|
using ServiceStack.ServiceHost;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
namespace MediaBrowser.Api.UserLibrary
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Class GetPersons
|
|
|
|
|
/// </summary>
|
2013-03-16 19:27:54 +00:00
|
|
|
|
[Route("/Users/{UserId}/Items/{ParentId}/Persons", "GET")]
|
2013-02-21 01:33:05 +00:00
|
|
|
|
[Route("/Users/{UserId}/Items/Root/Persons", "GET")]
|
2013-04-10 15:32:09 +00:00
|
|
|
|
[Api(Description = "Gets all persons from a given item, folder, or the entire library")]
|
2013-02-21 01:33:05 +00:00
|
|
|
|
public class GetPersons : GetItemsByName
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets or sets the person types.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <value>The person types.</value>
|
|
|
|
|
public string PersonTypes { get; set; }
|
|
|
|
|
}
|
|
|
|
|
|
2013-04-13 00:42:51 +00:00
|
|
|
|
[Route("/Users/{UserId}/FavoritePersons/{Name}", "POST")]
|
|
|
|
|
[Api(Description = "Marks a person as a favorite")]
|
|
|
|
|
public class MarkFavoritePerson : IReturnVoid
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets or sets the user id.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <value>The user id.</value>
|
|
|
|
|
[ApiMember(Name = "UserId", Description = "User Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "POST")]
|
|
|
|
|
public Guid UserId { get; set; }
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets or sets the name.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <value>The name.</value>
|
|
|
|
|
[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
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets or sets the user id.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <value>The user id.</value>
|
|
|
|
|
[ApiMember(Name = "UserId", Description = "User Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "DELETE")]
|
|
|
|
|
public Guid UserId { get; set; }
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets or sets the name.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <value>The name.</value>
|
|
|
|
|
[ApiMember(Name = "Name", Description = "Name", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "DELETE")]
|
|
|
|
|
public string Name { get; set; }
|
|
|
|
|
}
|
|
|
|
|
|
2013-02-21 01:33:05 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Class PersonsService
|
|
|
|
|
/// </summary>
|
|
|
|
|
public class PersonsService : BaseItemsByNameService<Person>
|
|
|
|
|
{
|
2013-02-28 19:32:41 +00:00
|
|
|
|
public PersonsService(IUserManager userManager, ILibraryManager libraryManager)
|
|
|
|
|
: base(userManager, libraryManager)
|
2013-02-27 20:25:45 +00:00
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2013-02-21 01:33:05 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets the specified request.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="request">The request.</param>
|
|
|
|
|
/// <returns>System.Object.</returns>
|
|
|
|
|
public object Get(GetPersons request)
|
|
|
|
|
{
|
|
|
|
|
var result = GetResult(request).Result;
|
|
|
|
|
|
|
|
|
|
return ToOptimizedResult(result);
|
|
|
|
|
}
|
|
|
|
|
|
2013-04-13 00:42:51 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Posts the specified request.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="request">The request.</param>
|
|
|
|
|
public void Post(MarkFavoritePerson request)
|
|
|
|
|
{
|
|
|
|
|
var task = MarkFavorite(() => LibraryManager.GetPerson(request.Name), request.UserId, true);
|
|
|
|
|
|
|
|
|
|
Task.WaitAll(task);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Deletes the specified request.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="request">The request.</param>
|
|
|
|
|
public void Delete(UnmarkFavoritePerson request)
|
|
|
|
|
{
|
|
|
|
|
var task = MarkFavorite(() => LibraryManager.GetPerson(request.Name), request.UserId, false);
|
|
|
|
|
|
|
|
|
|
Task.WaitAll(task);
|
|
|
|
|
}
|
|
|
|
|
|
2013-02-21 01:33:05 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets all items.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="request">The request.</param>
|
|
|
|
|
/// <param name="items">The items.</param>
|
|
|
|
|
/// <param name="user">The user.</param>
|
|
|
|
|
/// <returns>IEnumerable{Tuple{System.StringFunc{System.Int32}}}.</returns>
|
2013-04-11 22:09:08 +00:00
|
|
|
|
protected override IEnumerable<Tuple<string, Func<IEnumerable<BaseItem>>>> GetAllItems(GetItemsByName request, IEnumerable<BaseItem> items, User user)
|
2013-02-21 01:33:05 +00:00
|
|
|
|
{
|
|
|
|
|
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)
|
|
|
|
|
|
2013-04-11 22:09:08 +00:00
|
|
|
|
.Select(name => new Tuple<string, Func<IEnumerable<BaseItem>>>(name, () =>
|
2013-02-21 01:33:05 +00:00
|
|
|
|
{
|
|
|
|
|
if (personTypes.Length == 0)
|
|
|
|
|
{
|
2013-04-11 22:09:08 +00:00
|
|
|
|
return itemsList.Where(i => i.People.Any(p => p.Name.Equals(name, StringComparison.OrdinalIgnoreCase)));
|
2013-02-21 01:33:05 +00:00
|
|
|
|
}
|
|
|
|
|
|
2013-04-11 22:09:08 +00:00
|
|
|
|
return itemsList.Where(i => i.People.Any(p => p.Name.Equals(name, StringComparison.OrdinalIgnoreCase) && personTypes.Contains(p.Type ?? string.Empty, StringComparer.OrdinalIgnoreCase)));
|
2013-02-21 01:33:05 +00:00
|
|
|
|
})
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets all people.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="itemsList">The items list.</param>
|
|
|
|
|
/// <param name="personTypes">The person types.</param>
|
|
|
|
|
/// <returns>IEnumerable{PersonInfo}.</returns>
|
|
|
|
|
private IEnumerable<PersonInfo> GetAllPeople(IEnumerable<BaseItem> itemsList, string[] personTypes)
|
|
|
|
|
{
|
2013-04-04 19:06:09 +00:00
|
|
|
|
var people = itemsList.SelectMany(i => i.People.OrderBy(p => p.Type));
|
|
|
|
|
|
|
|
|
|
|
2013-02-21 01:33:05 +00:00
|
|
|
|
return personTypes.Length == 0 ?
|
2013-04-04 19:06:09 +00:00
|
|
|
|
|
|
|
|
|
people :
|
|
|
|
|
|
|
|
|
|
people.Where(p => personTypes.Contains(p.Type ?? string.Empty, StringComparer.OrdinalIgnoreCase));
|
2013-02-21 01:33:05 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets the entity.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="name">The name.</param>
|
|
|
|
|
/// <returns>Task{Genre}.</returns>
|
|
|
|
|
protected override Task<Person> GetEntity(string name)
|
|
|
|
|
{
|
2013-02-28 19:32:41 +00:00
|
|
|
|
return LibraryManager.GetPerson(name);
|
2013-02-21 01:33:05 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|