< Summary - Jellyfin

Information
Class: Jellyfin.Api.Controllers.MusicGenresController
Assembly: Jellyfin.Api
File(s): /srv/git/jellyfin/Jellyfin.Api/Controllers/MusicGenresController.cs
Line coverage
100%
Covered lines: 27
Uncovered lines: 0
Coverable lines: 27
Total lines: 212
Line coverage: 100%
Branch coverage
50%
Covered branches: 2
Total branches: 4
Branch coverage: 50%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Coverage history

Coverage history 0 25 50 75 100 5/16/2026 - 12:15:55 AM Line coverage: 100% (27/27) Branch coverage: 100% (4/4) Total lines: 2075/20/2026 - 12:15:44 AM Line coverage: 100% (27/27) Branch coverage: 50% (2/4) Total lines: 20710/29/2026 - 12:17:11 AM Line coverage: 100% (27/27) Branch coverage: 50% (2/4) Total lines: 212 5/16/2026 - 12:15:55 AM Line coverage: 100% (27/27) Branch coverage: 100% (4/4) Total lines: 2075/20/2026 - 12:15:44 AM Line coverage: 100% (27/27) Branch coverage: 50% (2/4) Total lines: 20710/29/2026 - 12:17:11 AM Line coverage: 100% (27/27) Branch coverage: 50% (2/4) Total lines: 212

Coverage delta

Coverage delta 50 -50

Metrics

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