2017-04-15 19:45:33 +00:00
|
|
|
|
using MediaBrowser.Controller.Dto;
|
|
|
|
|
using MediaBrowser.Controller.Entities;
|
|
|
|
|
using MediaBrowser.Controller.Net;
|
|
|
|
|
using MediaBrowser.Model.Dto;
|
|
|
|
|
using MediaBrowser.Model.Querying;
|
|
|
|
|
using MediaBrowser.Model.Services;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using MediaBrowser.Controller.Library;
|
|
|
|
|
|
|
|
|
|
namespace MediaBrowser.Api
|
|
|
|
|
{
|
|
|
|
|
[Route("/Users/{UserId}/Suggestions", "GET", Summary = "Gets items based on a query.")]
|
2017-04-17 01:45:44 +00:00
|
|
|
|
[Route("/Users/{UserId}/Suggestions", "POST", Summary = "Gets items based on a query.")]
|
2017-04-15 19:45:33 +00:00
|
|
|
|
public class GetSuggestedItems : IReturn<QueryResult<BaseItem>>
|
|
|
|
|
{
|
|
|
|
|
public string MediaType { get; set; }
|
|
|
|
|
public string Type { get; set; }
|
|
|
|
|
public string UserId { get; set; }
|
|
|
|
|
public bool EnableTotalRecordCount { get; set; }
|
|
|
|
|
public int? StartIndex { get; set; }
|
|
|
|
|
public int? Limit { get; set; }
|
2017-04-17 01:45:44 +00:00
|
|
|
|
public string Name { get; set; }
|
2017-04-15 19:45:33 +00:00
|
|
|
|
|
|
|
|
|
public string[] GetMediaTypes()
|
|
|
|
|
{
|
|
|
|
|
return (MediaType ?? string.Empty).Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public string[] GetIncludeItemTypes()
|
|
|
|
|
{
|
|
|
|
|
return (Type ?? string.Empty).Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public class SuggestionsService : BaseApiService
|
|
|
|
|
{
|
|
|
|
|
private readonly IDtoService _dtoService;
|
|
|
|
|
private readonly IAuthorizationContext _authContext;
|
|
|
|
|
private readonly IUserManager _userManager;
|
|
|
|
|
private readonly ILibraryManager _libraryManager;
|
|
|
|
|
|
|
|
|
|
public SuggestionsService(IDtoService dtoService, IAuthorizationContext authContext, IUserManager userManager, ILibraryManager libraryManager)
|
|
|
|
|
{
|
|
|
|
|
_dtoService = dtoService;
|
|
|
|
|
_authContext = authContext;
|
|
|
|
|
_userManager = userManager;
|
|
|
|
|
_libraryManager = libraryManager;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task<object> Get(GetSuggestedItems request)
|
|
|
|
|
{
|
|
|
|
|
var result = await GetResultItems(request).ConfigureAwait(false);
|
|
|
|
|
|
|
|
|
|
return ToOptimizedResult(result);
|
|
|
|
|
}
|
|
|
|
|
|
2017-04-17 01:45:44 +00:00
|
|
|
|
public async Task<object> Post(GetSuggestedItems request)
|
|
|
|
|
{
|
|
|
|
|
var result = await GetResultItems(request).ConfigureAwait(false);
|
|
|
|
|
|
|
|
|
|
return ToOptimizedResult(result);
|
|
|
|
|
}
|
|
|
|
|
|
2017-04-15 19:45:33 +00:00
|
|
|
|
private async Task<QueryResult<BaseItemDto>> GetResultItems(GetSuggestedItems request)
|
|
|
|
|
{
|
|
|
|
|
var user = !string.IsNullOrWhiteSpace(request.UserId) ? _userManager.GetUserById(request.UserId) : null;
|
|
|
|
|
|
|
|
|
|
var dtoOptions = GetDtoOptions(_authContext, request);
|
|
|
|
|
var result = GetItems(request, user, dtoOptions);
|
|
|
|
|
|
|
|
|
|
var dtoList = await _dtoService.GetBaseItemDtos(result.Items, dtoOptions, user).ConfigureAwait(false);
|
|
|
|
|
|
|
|
|
|
if (dtoList == null)
|
|
|
|
|
{
|
|
|
|
|
throw new InvalidOperationException("GetBaseItemDtos returned null");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return new QueryResult<BaseItemDto>
|
|
|
|
|
{
|
|
|
|
|
TotalRecordCount = result.TotalRecordCount,
|
|
|
|
|
Items = dtoList.ToArray()
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private QueryResult<BaseItem> GetItems(GetSuggestedItems request, User user, DtoOptions dtoOptions)
|
|
|
|
|
{
|
2017-04-17 01:45:44 +00:00
|
|
|
|
BaseItem similarToItem = null;
|
|
|
|
|
|
|
|
|
|
if (!string.IsNullOrWhiteSpace(request.Name))
|
|
|
|
|
{
|
|
|
|
|
// get item by name, then get similar items from that
|
|
|
|
|
similarToItem = _libraryManager.GetItemList(new InternalItemsQuery(user)
|
|
|
|
|
{
|
|
|
|
|
SortBy = new string[] {ItemSortBy.Random},
|
|
|
|
|
MediaTypes = request.GetMediaTypes(),
|
|
|
|
|
IncludeItemTypes = request.GetIncludeItemTypes(),
|
|
|
|
|
IsVirtualItem = false,
|
|
|
|
|
Name = request.Name,
|
|
|
|
|
Recursive = true,
|
|
|
|
|
Limit = 1
|
|
|
|
|
|
|
|
|
|
}).FirstOrDefault();
|
|
|
|
|
|
|
|
|
|
if (similarToItem == null)
|
|
|
|
|
{
|
|
|
|
|
return new QueryResult<BaseItem>();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return _libraryManager.GetItemsResult(new InternalItemsQuery(user)
|
2017-04-15 19:45:33 +00:00
|
|
|
|
{
|
|
|
|
|
SortBy = new string[] { ItemSortBy.Random },
|
|
|
|
|
MediaTypes = request.GetMediaTypes(),
|
|
|
|
|
IncludeItemTypes = request.GetIncludeItemTypes(),
|
|
|
|
|
IsVirtualItem = false,
|
|
|
|
|
StartIndex = request.StartIndex,
|
|
|
|
|
Limit = request.Limit,
|
2017-04-17 01:45:44 +00:00
|
|
|
|
DtoOptions = dtoOptions,
|
|
|
|
|
EnableTotalRecordCount = request.EnableTotalRecordCount,
|
|
|
|
|
Recursive = true,
|
|
|
|
|
SimilarTo = similarToItem
|
|
|
|
|
});
|
2017-04-15 19:45:33 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|