Merge pull request #3382 from Ullmie02/api-suggestions

Move SuggestionsService to Jellyfin.Api
This commit is contained in:
Patrick Barron 2020-06-20 21:42:27 +00:00 committed by GitHub
commit deac459b62
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 87 additions and 98 deletions

View File

@ -0,0 +1,87 @@
using System;
using System.Linq;
using Jellyfin.Api.Extensions;
using Jellyfin.Api.Helpers;
using MediaBrowser.Controller.Dto;
using MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.Library;
using MediaBrowser.Model.Dto;
using MediaBrowser.Model.Entities;
using MediaBrowser.Model.Querying;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
namespace Jellyfin.Api.Controllers
{
/// <summary>
/// The suggestions controller.
/// </summary>
public class SuggestionsController : BaseJellyfinApiController
{
private readonly IDtoService _dtoService;
private readonly IUserManager _userManager;
private readonly ILibraryManager _libraryManager;
/// <summary>
/// Initializes a new instance of the <see cref="SuggestionsController"/> class.
/// </summary>
/// <param name="dtoService">Instance of the <see cref="IDtoService"/> interface.</param>
/// <param name="userManager">Instance of the <see cref="IUserManager"/> interface.</param>
/// <param name="libraryManager">Instance of the <see cref="ILibraryManager"/> interface.</param>
public SuggestionsController(
IDtoService dtoService,
IUserManager userManager,
ILibraryManager libraryManager)
{
_dtoService = dtoService;
_userManager = userManager;
_libraryManager = libraryManager;
}
/// <summary>
/// Gets suggestions.
/// </summary>
/// <param name="userId">The user id.</param>
/// <param name="mediaType">The media types.</param>
/// <param name="type">The type.</param>
/// <param name="enableTotalRecordCount">Whether to enable the total record count.</param>
/// <param name="startIndex">Optional. The start index.</param>
/// <param name="limit">Optional. The limit.</param>
/// <response code="200">Suggestions returned.</response>
/// <returns>A <see cref="QueryResult{BaseItemDto}"/> with the suggestions.</returns>
[HttpGet("/Users/{userId}/Suggestions")]
[ProducesResponseType(StatusCodes.Status200OK)]
public ActionResult<QueryResult<BaseItemDto>> GetSuggestions(
[FromRoute] Guid userId,
[FromQuery] string? mediaType,
[FromQuery] string? type,
[FromQuery] bool enableTotalRecordCount,
[FromQuery] int? startIndex,
[FromQuery] int? limit)
{
var user = !userId.Equals(Guid.Empty) ? _userManager.GetUserById(userId) : null;
var dtoOptions = new DtoOptions().AddClientFields(Request);
var result = _libraryManager.GetItemsResult(new InternalItemsQuery(user)
{
OrderBy = new[] { ItemSortBy.Random }.Select(i => new ValueTuple<string, SortOrder>(i, SortOrder.Descending)).ToArray(),
MediaTypes = RequestHelpers.Split(mediaType!, ',', true),
IncludeItemTypes = RequestHelpers.Split(type!, ',', true),
IsVirtualItem = false,
StartIndex = startIndex,
Limit = limit,
DtoOptions = dtoOptions,
EnableTotalRecordCount = enableTotalRecordCount,
Recursive = true
});
var dtoList = _dtoService.GetBaseItemDtos(result.Items, dtoOptions, user);
return new QueryResult<BaseItemDto>
{
TotalRecordCount = result.TotalRecordCount,
Items = dtoList
};
}
}
}

View File

@ -1,98 +0,0 @@
using System;
using System.Linq;
using Jellyfin.Data.Entities;
using MediaBrowser.Controller.Configuration;
using MediaBrowser.Controller.Dto;
using MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.Library;
using MediaBrowser.Controller.Net;
using MediaBrowser.Model.Dto;
using MediaBrowser.Model.Entities;
using MediaBrowser.Model.Querying;
using MediaBrowser.Model.Services;
using Microsoft.Extensions.Logging;
namespace MediaBrowser.Api
{
[Route("/Users/{UserId}/Suggestions", "GET", Summary = "Gets items based on a query.")]
public class GetSuggestedItems : IReturn<QueryResult<BaseItemDto>>
{
public string MediaType { get; set; }
public string Type { get; set; }
public Guid UserId { get; set; }
public bool EnableTotalRecordCount { get; set; }
public int? StartIndex { get; set; }
public int? Limit { get; set; }
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(
ILogger<SuggestionsService> logger,
IServerConfigurationManager serverConfigurationManager,
IHttpResultFactory httpResultFactory,
IDtoService dtoService,
IAuthorizationContext authContext,
IUserManager userManager,
ILibraryManager libraryManager)
: base(logger, serverConfigurationManager, httpResultFactory)
{
_dtoService = dtoService;
_authContext = authContext;
_userManager = userManager;
_libraryManager = libraryManager;
}
public object Get(GetSuggestedItems request)
{
return GetResultItems(request);
}
private QueryResult<BaseItemDto> GetResultItems(GetSuggestedItems request)
{
var user = !request.UserId.Equals(Guid.Empty) ? _userManager.GetUserById(request.UserId) : null;
var dtoOptions = GetDtoOptions(_authContext, request);
var result = GetItems(request, user, dtoOptions);
var dtoList = _dtoService.GetBaseItemDtos(result.Items, dtoOptions, user);
return new QueryResult<BaseItemDto>
{
TotalRecordCount = result.TotalRecordCount,
Items = dtoList
};
}
private QueryResult<BaseItem> GetItems(GetSuggestedItems request, User user, DtoOptions dtoOptions)
{
return _libraryManager.GetItemsResult(new InternalItemsQuery(user)
{
OrderBy = new[] { ItemSortBy.Random }.Select(i => new ValueTuple<string, SortOrder>(i, SortOrder.Descending)).ToArray(),
MediaTypes = request.GetMediaTypes(),
IncludeItemTypes = request.GetIncludeItemTypes(),
IsVirtualItem = false,
StartIndex = request.StartIndex,
Limit = request.Limit,
DtoOptions = dtoOptions,
EnableTotalRecordCount = request.EnableTotalRecordCount,
Recursive = true
});
}
}
}