< Summary - Jellyfin

Information
Class: Jellyfin.Api.Controllers.SuggestionsController
Assembly: Jellyfin.Api
File(s): /srv/git/jellyfin/Jellyfin.Api/Controllers/SuggestionsController.cs
Line coverage
0%
Covered lines: 0
Uncovered lines: 30
Coverable lines: 30
Total lines: 125
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 2
Branch coverage: 0%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Coverage history

Coverage history 0 25 50 75 100

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%210%
GetSuggestions(...)0%620%

File(s)

/srv/git/jellyfin/Jellyfin.Api/Controllers/SuggestionsController.cs

#LineLine coverage
 1using System;
 2using System.ComponentModel.DataAnnotations;
 3using Jellyfin.Api.Extensions;
 4using Jellyfin.Api.Helpers;
 5using Jellyfin.Api.ModelBinders;
 6using Jellyfin.Data.Enums;
 7using Jellyfin.Database.Implementations.Entities;
 8using Jellyfin.Database.Implementations.Enums;
 9using Jellyfin.Extensions;
 10using MediaBrowser.Controller.Dto;
 11using MediaBrowser.Controller.Entities;
 12using MediaBrowser.Controller.Library;
 13using MediaBrowser.Model.Dto;
 14using MediaBrowser.Model.Querying;
 15using Microsoft.AspNetCore.Authorization;
 16using Microsoft.AspNetCore.Http;
 17using Microsoft.AspNetCore.Mvc;
 18
 19namespace Jellyfin.Api.Controllers;
 20
 21/// <summary>
 22/// The suggestions controller.
 23/// </summary>
 24[Route("")]
 25[Authorize]
 26public class SuggestionsController : BaseJellyfinApiController
 27{
 28    private readonly IDtoService _dtoService;
 29    private readonly IUserManager _userManager;
 30    private readonly ILibraryManager _libraryManager;
 31
 32    /// <summary>
 33    /// Initializes a new instance of the <see cref="SuggestionsController"/> class.
 34    /// </summary>
 35    /// <param name="dtoService">Instance of the <see cref="IDtoService"/> interface.</param>
 36    /// <param name="userManager">Instance of the <see cref="IUserManager"/> interface.</param>
 37    /// <param name="libraryManager">Instance of the <see cref="ILibraryManager"/> interface.</param>
 038    public SuggestionsController(
 039        IDtoService dtoService,
 040        IUserManager userManager,
 041        ILibraryManager libraryManager)
 42    {
 043        _dtoService = dtoService;
 044        _userManager = userManager;
 045        _libraryManager = libraryManager;
 046    }
 47
 48    /// <summary>
 49    /// Gets suggestions.
 50    /// </summary>
 51    /// <param name="userId">The user id.</param>
 52    /// <param name="mediaType">The media types.</param>
 53    /// <param name="type">The type.</param>
 54    /// <param name="startIndex">Optional. The start index.</param>
 55    /// <param name="limit">Optional. The limit.</param>
 56    /// <param name="enableTotalRecordCount">Whether to enable the total record count.</param>
 57    /// <response code="200">Suggestions returned.</response>
 58    /// <returns>A <see cref="QueryResult{BaseItemDto}"/> with the suggestions.</returns>
 59    [HttpGet("Items/Suggestions")]
 60    [ProducesResponseType(StatusCodes.Status200OK)]
 61    public ActionResult<QueryResult<BaseItemDto>> GetSuggestions(
 62        [FromQuery] Guid? userId,
 63        [FromQuery, ModelBinder(typeof(CommaDelimitedCollectionModelBinder))] MediaType[] mediaType,
 64        [FromQuery, ModelBinder(typeof(CommaDelimitedCollectionModelBinder))] BaseItemKind[] type,
 65        [FromQuery] int? startIndex,
 66        [FromQuery] int? limit,
 67        [FromQuery] bool enableTotalRecordCount = false)
 68    {
 69        User? user;
 070        if (userId.IsNullOrEmpty())
 71        {
 072            user = null;
 73        }
 74        else
 75        {
 076            var requestUserId = RequestHelpers.GetUserId(User, userId);
 077            user = _userManager.GetUserById(requestUserId);
 78        }
 79
 080        var dtoOptions = new DtoOptions().AddClientFields(User);
 081        var result = _libraryManager.GetItemsResult(new InternalItemsQuery(user)
 082        {
 083            OrderBy = new[] { (ItemSortBy.Random, SortOrder.Descending) },
 084            MediaTypes = mediaType,
 085            IncludeItemTypes = type,
 086            IsVirtualItem = false,
 087            StartIndex = startIndex,
 088            Limit = limit,
 089            DtoOptions = dtoOptions,
 090            EnableTotalRecordCount = enableTotalRecordCount,
 091            Recursive = true
 092        });
 93
 094        var dtoList = _dtoService.GetBaseItemDtos(result.Items, dtoOptions, user);
 95
 096        return new QueryResult<BaseItemDto>(
 097            startIndex,
 098            result.TotalRecordCount,
 099            dtoList);
 100    }
 101
 102    /// <summary>
 103    /// Gets suggestions.
 104    /// </summary>
 105    /// <param name="userId">The user id.</param>
 106    /// <param name="mediaType">The media types.</param>
 107    /// <param name="type">The type.</param>
 108    /// <param name="startIndex">Optional. The start index.</param>
 109    /// <param name="limit">Optional. The limit.</param>
 110    /// <param name="enableTotalRecordCount">Whether to enable the total record count.</param>
 111    /// <response code="200">Suggestions returned.</response>
 112    /// <returns>A <see cref="QueryResult{BaseItemDto}"/> with the suggestions.</returns>
 113    [HttpGet("Users/{userId}/Suggestions")]
 114    [ProducesResponseType(StatusCodes.Status200OK)]
 115    [Obsolete("Kept for backwards compatibility")]
 116    [ApiExplorerSettings(IgnoreApi = true)]
 117    public ActionResult<QueryResult<BaseItemDto>> GetSuggestionsLegacy(
 118        [FromRoute, Required] Guid userId,
 119        [FromQuery, ModelBinder(typeof(CommaDelimitedCollectionModelBinder))] MediaType[] mediaType,
 120        [FromQuery, ModelBinder(typeof(CommaDelimitedCollectionModelBinder))] BaseItemKind[] type,
 121        [FromQuery] int? startIndex,
 122        [FromQuery] int? limit,
 123        [FromQuery] bool enableTotalRecordCount = false)
 124        => GetSuggestions(userId, mediaType, type, startIndex, limit, enableTotalRecordCount);
 125}