< Summary - Jellyfin

Information
Class: Jellyfin.Api.Controllers.MusicGenresController
Assembly: Jellyfin.Api
File(s): /srv/git/jellyfin/Jellyfin.Api/Controllers/MusicGenresController.cs
Line coverage
86%
Covered lines: 33
Uncovered lines: 5
Coverable lines: 38
Total lines: 204
Line coverage: 86.8%
Branch coverage
60%
Covered branches: 6
Total branches: 10
Branch coverage: 60%
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%11100%
GetMusicGenre(...)33.33%9.38654.54%
GetItemFromSlugName(...)100%44100%

File(s)

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

#LineLine coverage
 1using System;
 2using System.ComponentModel.DataAnnotations;
 3using System.Linq;
 4using Jellyfin.Api.Extensions;
 5using Jellyfin.Api.Helpers;
 6using Jellyfin.Api.ModelBinders;
 7using Jellyfin.Data.Entities;
 8using Jellyfin.Data.Enums;
 9using Jellyfin.Extensions;
 10using MediaBrowser.Controller.Dto;
 11using MediaBrowser.Controller.Entities;
 12using MediaBrowser.Controller.Entities.Audio;
 13using MediaBrowser.Controller.Library;
 14using MediaBrowser.Model.Dto;
 15using MediaBrowser.Model.Entities;
 16using MediaBrowser.Model.Querying;
 17using Microsoft.AspNetCore.Authorization;
 18using Microsoft.AspNetCore.Http;
 19using Microsoft.AspNetCore.Mvc;
 20
 21namespace Jellyfin.Api.Controllers;
 22
 23/// <summary>
 24/// The music genres controller.
 25/// </summary>
 26[Authorize]
 27public class MusicGenresController : BaseJellyfinApiController
 28{
 29    private readonly ILibraryManager _libraryManager;
 30    private readonly IDtoService _dtoService;
 31    private readonly IUserManager _userManager;
 32
 33    /// <summary>
 34    /// Initializes a new instance of the <see cref="MusicGenresController"/> class.
 35    /// </summary>
 36    /// <param name="libraryManager">Instance of <see cref="ILibraryManager"/> interface.</param>
 37    /// <param name="userManager">Instance of <see cref="IUserManager"/> interface.</param>
 38    /// <param name="dtoService">Instance of <see cref="IDtoService"/> interface.</param>
 139    public MusicGenresController(
 140        ILibraryManager libraryManager,
 141        IUserManager userManager,
 142        IDtoService dtoService)
 43    {
 144        _libraryManager = libraryManager;
 145        _userManager = userManager;
 146        _dtoService = dtoService;
 147    }
 48
 49    /// <summary>
 50    /// Gets all music genres from a given item, folder, or the entire library.
 51    /// </summary>
 52    /// <param name="startIndex">Optional. The record index to start at. All items with a lower index will be dropped fr
 53    /// <param name="limit">Optional. The maximum number of records to return.</param>
 54    /// <param name="searchTerm">The search term.</param>
 55    /// <param name="parentId">Specify this to localize the search to a specific item or folder. Omit to use the root.</
 56    /// <param name="fields">Optional. Specify additional fields of information to return in the output.</param>
 57    /// <param name="excludeItemTypes">Optional. If specified, results will be filtered out based on item type. This all
 58    /// <param name="includeItemTypes">Optional. If specified, results will be filtered in based on item type. This allo
 59    /// <param name="isFavorite">Optional filter by items that are marked as favorite, or not.</param>
 60    /// <param name="imageTypeLimit">Optional, the max number of images to return, per image type.</param>
 61    /// <param name="enableImageTypes">Optional. The image types to include in the output.</param>
 62    /// <param name="userId">User id.</param>
 63    /// <param name="nameStartsWithOrGreater">Optional filter by items whose name is sorted equally or greater than a gi
 64    /// <param name="nameStartsWith">Optional filter by items whose name is sorted equally than a given input string.</p
 65    /// <param name="nameLessThan">Optional filter by items whose name is equally or lesser than a given input string.</
 66    /// <param name="sortBy">Optional. Specify one or more sort orders, comma delimited.</param>
 67    /// <param name="sortOrder">Sort Order - Ascending,Descending.</param>
 68    /// <param name="enableImages">Optional, include image information in output.</param>
 69    /// <param name="enableTotalRecordCount">Optional. Include total record count.</param>
 70    /// <response code="200">Music genres returned.</response>
 71    /// <returns>An <see cref="OkResult"/> containing the queryresult of music genres.</returns>
 72    [HttpGet]
 73    [Obsolete("Use GetGenres instead")]
 74    public ActionResult<QueryResult<BaseItemDto>> GetMusicGenres(
 75        [FromQuery] int? startIndex,
 76        [FromQuery] int? limit,
 77        [FromQuery] string? searchTerm,
 78        [FromQuery] Guid? parentId,
 79        [FromQuery, ModelBinder(typeof(CommaDelimitedArrayModelBinder))] ItemFields[] fields,
 80        [FromQuery, ModelBinder(typeof(CommaDelimitedArrayModelBinder))] BaseItemKind[] excludeItemTypes,
 81        [FromQuery, ModelBinder(typeof(CommaDelimitedArrayModelBinder))] BaseItemKind[] includeItemTypes,
 82        [FromQuery] bool? isFavorite,
 83        [FromQuery] int? imageTypeLimit,
 84        [FromQuery, ModelBinder(typeof(CommaDelimitedArrayModelBinder))] ImageType[] enableImageTypes,
 85        [FromQuery] Guid? userId,
 86        [FromQuery] string? nameStartsWithOrGreater,
 87        [FromQuery] string? nameStartsWith,
 88        [FromQuery] string? nameLessThan,
 89        [FromQuery, ModelBinder(typeof(CommaDelimitedArrayModelBinder))] ItemSortBy[] sortBy,
 90        [FromQuery, ModelBinder(typeof(CommaDelimitedArrayModelBinder))] SortOrder[] sortOrder,
 91        [FromQuery] bool? enableImages = true,
 92        [FromQuery] bool enableTotalRecordCount = true)
 93    {
 94        userId = RequestHelpers.GetUserId(User, userId);
 95        var dtoOptions = new DtoOptions { Fields = fields }
 96            .AddClientFields(User)
 97            .AddAdditionalDtoOptions(enableImages, false, imageTypeLimit, enableImageTypes);
 98
 99        User? user = userId.IsNullOrEmpty()
 100            ? null
 101            : _userManager.GetUserById(userId.Value);
 102
 103        var parentItem = _libraryManager.GetParentItem(parentId, userId);
 104
 105        var query = new InternalItemsQuery(user)
 106        {
 107            ExcludeItemTypes = excludeItemTypes,
 108            IncludeItemTypes = includeItemTypes,
 109            StartIndex = startIndex,
 110            Limit = limit,
 111            IsFavorite = isFavorite,
 112            NameLessThan = nameLessThan,
 113            NameStartsWith = nameStartsWith,
 114            NameStartsWithOrGreater = nameStartsWithOrGreater,
 115            DtoOptions = dtoOptions,
 116            SearchTerm = searchTerm,
 117            EnableTotalRecordCount = enableTotalRecordCount,
 118            OrderBy = RequestHelpers.GetOrderBy(sortBy, sortOrder)
 119        };
 120
 121        if (parentId.HasValue)
 122        {
 123            if (parentItem is Folder)
 124            {
 125                query.AncestorIds = new[] { parentId.Value };
 126            }
 127            else
 128            {
 129                query.ItemIds = new[] { parentId.Value };
 130            }
 131        }
 132
 133        var result = _libraryManager.GetMusicGenres(query);
 134
 135        var shouldIncludeItemTypes = includeItemTypes.Length != 0;
 136        return RequestHelpers.CreateQueryResult(result, dtoOptions, _dtoService, shouldIncludeItemTypes, user);
 137    }
 138
 139    /// <summary>
 140    /// Gets a music genre, by name.
 141    /// </summary>
 142    /// <param name="genreName">The genre name.</param>
 143    /// <param name="userId">Optional. Filter by user id, and attach user data.</param>
 144    /// <returns>An <see cref="OkResult"/> containing a <see cref="BaseItemDto"/> with the music genre.</returns>
 145    [HttpGet("{genreName}")]
 146    [ProducesResponseType(StatusCodes.Status200OK)]
 147    public ActionResult<BaseItemDto> GetMusicGenre([FromRoute, Required] string genreName, [FromQuery] Guid? userId)
 148    {
 1149        userId = RequestHelpers.GetUserId(User, userId);
 1150        var dtoOptions = new DtoOptions().AddClientFields(User);
 151
 152        MusicGenre? item;
 153
 1154        if (genreName.Contains(BaseItem.SlugChar, StringComparison.OrdinalIgnoreCase))
 155        {
 1156            item = GetItemFromSlugName<MusicGenre>(_libraryManager, genreName, dtoOptions, BaseItemKind.MusicGenre);
 157        }
 158        else
 159        {
 0160            item = _libraryManager.GetMusicGenre(genreName);
 161        }
 162
 1163        if (item is null)
 164        {
 1165            return NotFound();
 166        }
 167
 0168        if (!userId.IsNullOrEmpty())
 169        {
 0170            var user = _userManager.GetUserById(userId.Value);
 171
 0172            return _dtoService.GetBaseItemDto(item, dtoOptions, user);
 173        }
 174
 0175        return _dtoService.GetBaseItemDto(item, dtoOptions);
 176    }
 177
 178    private T? GetItemFromSlugName<T>(ILibraryManager libraryManager, string name, DtoOptions dtoOptions, BaseItemKind b
 179        where T : BaseItem, new()
 180    {
 1181        var result = libraryManager.GetItemList(new InternalItemsQuery
 1182        {
 1183            Name = name.Replace(BaseItem.SlugChar, '&'),
 1184            IncludeItemTypes = new[] { baseItemKind },
 1185            DtoOptions = dtoOptions
 1186        }).OfType<T>().FirstOrDefault();
 187
 1188        result ??= libraryManager.GetItemList(new InternalItemsQuery
 1189        {
 1190            Name = name.Replace(BaseItem.SlugChar, '/'),
 1191            IncludeItemTypes = new[] { baseItemKind },
 1192            DtoOptions = dtoOptions
 1193        }).OfType<T>().FirstOrDefault();
 194
 1195        result ??= libraryManager.GetItemList(new InternalItemsQuery
 1196        {
 1197            Name = name.Replace(BaseItem.SlugChar, '?'),
 1198            IncludeItemTypes = new[] { baseItemKind },
 1199            DtoOptions = dtoOptions
 1200        }).OfType<T>().FirstOrDefault();
 201
 1202        return result;
 203    }
 204}