jellyfin/MediaBrowser.Api/Movies/TrailersService.cs

128 lines
4.3 KiB
C#
Raw Normal View History

2014-10-20 20:23:40 +00:00
using MediaBrowser.Api.UserLibrary;
using MediaBrowser.Controller.Channels;
using MediaBrowser.Controller.Dto;
2013-09-04 17:02:19 +00:00
using MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.Library;
2014-07-02 18:34:08 +00:00
using MediaBrowser.Controller.Net;
2014-10-20 20:23:40 +00:00
using MediaBrowser.Model.Channels;
using MediaBrowser.Model.Entities;
using MediaBrowser.Model.Querying;
2013-12-07 15:52:38 +00:00
using ServiceStack;
2014-10-20 20:23:40 +00:00
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
namespace MediaBrowser.Api.Movies
{
2014-10-20 20:23:40 +00:00
[Route("/Trailers", "GET", Summary = "Finds movies and trailers similar to a given trailer.")]
public class Getrailers : BaseItemsRequest, IReturn<ItemsResult>
{
}
/// <summary>
/// Class TrailersService
/// </summary>
2014-07-02 18:34:08 +00:00
[Authenticated]
public class TrailersService : BaseApiService
{
/// <summary>
/// The _user manager
/// </summary>
private readonly IUserManager _userManager;
/// <summary>
/// The _user data repository
/// </summary>
2013-10-02 16:08:58 +00:00
private readonly IUserDataManager _userDataRepository;
/// <summary>
/// The _library manager
/// </summary>
private readonly ILibraryManager _libraryManager;
2013-09-04 17:02:19 +00:00
private readonly IDtoService _dtoService;
2014-10-20 20:23:40 +00:00
private readonly IChannelManager _channelManager;
2013-09-04 17:02:19 +00:00
/// <summary>
/// Initializes a new instance of the <see cref="TrailersService"/> class.
/// </summary>
/// <param name="userManager">The user manager.</param>
/// <param name="userDataRepository">The user data repository.</param>
/// <param name="libraryManager">The library manager.</param>
2015-08-21 02:36:30 +00:00
public TrailersService(IUserManager userManager, IUserDataManager userDataRepository, ILibraryManager libraryManager, IDtoService dtoService, IChannelManager channelManager)
{
_userManager = userManager;
_userDataRepository = userDataRepository;
_libraryManager = libraryManager;
2013-09-04 17:02:19 +00:00
_dtoService = dtoService;
2014-10-20 20:23:40 +00:00
_channelManager = channelManager;
}
2014-10-20 20:23:40 +00:00
public async Task<object> Get(Getrailers request)
{
2015-05-29 23:51:33 +00:00
var user = !string.IsNullOrWhiteSpace(request.UserId) ? _userManager.GetUserById(request.UserId) : null;
2014-10-20 20:23:40 +00:00
var result = await GetAllTrailers(user).ConfigureAwait(false);
IEnumerable<BaseItem> items = result.Items;
// Apply filters
// Run them starting with the ones that are likely to reduce the list the most
foreach (var filter in request.GetFilters().OrderByDescending(f => (int)f))
{
items = ItemsService.ApplyFilter(items, filter, user, _userDataRepository);
}
items = _libraryManager.Sort(items, user, request.GetOrderBy(), request.SortOrder ?? SortOrder.Ascending);
var itemsArray = items.ToList();
var pagedItems = ApplyPaging(request, itemsArray);
2015-01-24 19:03:55 +00:00
var dtoOptions = GetDtoOptions(request);
2014-10-20 20:23:40 +00:00
2015-01-24 19:03:55 +00:00
var returnItems = _dtoService.GetBaseItemDtos(pagedItems, dtoOptions, user).ToArray();
2014-10-20 20:23:40 +00:00
return new ItemsResult
{
TotalRecordCount = itemsArray.Count,
Items = returnItems
};
}
private IEnumerable<BaseItem> ApplyPaging(Getrailers request, IEnumerable<BaseItem> items)
{
// Start at
if (request.StartIndex.HasValue)
{
items = items.Skip(request.StartIndex.Value);
}
// Return limit
if (request.Limit.HasValue)
{
items = items.Take(request.Limit.Value);
}
return items;
}
private async Task<QueryResult<BaseItem>> GetAllTrailers(User user)
{
var trailerResult = await _channelManager.GetAllMediaInternal(new AllChannelMediaQuery
{
ContentTypes = new[] { ChannelMediaContentType.MovieExtra },
ExtraTypes = new[] { ExtraType.Trailer },
UserId = user.Id.ToString("N")
}, CancellationToken.None).ConfigureAwait(false);
return new QueryResult<BaseItem>
{
Items = trailerResult.Items,
TotalRecordCount = trailerResult.TotalRecordCount
};
}
}
}