< Summary - Jellyfin

Information
Class: Jellyfin.Api.Controllers.GenresController
Assembly: Jellyfin.Api
File(s): /srv/git/jellyfin/Jellyfin.Api/Controllers/GenresController.cs
Line coverage
0%
Covered lines: 0
Uncovered lines: 70
Coverable lines: 70
Total lines: 208
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 22
Branch coverage: 0%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Coverage history

Coverage history 0 25 50 75 100 9/15/2025 - 12:10:55 AM Line coverage: 0% (0/72) Branch coverage: 0% (0/22) Total lines: 21012/1/2025 - 12:11:46 AM Line coverage: 0% (0/70) Branch coverage: 0% (0/22) Total lines: 208 9/15/2025 - 12:10:55 AM Line coverage: 0% (0/72) Branch coverage: 0% (0/22) Total lines: 21012/1/2025 - 12:11:46 AM Line coverage: 0% (0/70) Branch coverage: 0% (0/22) Total lines: 208

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%210%
GetGenres(...)0%156120%
GetGenre(...)0%4260%
GetItemFromSlugName(...)0%2040%

File(s)

/srv/git/jellyfin/Jellyfin.Api/Controllers/GenresController.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.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;
 20using Genre = MediaBrowser.Controller.Entities.Genre;
 21
 22namespace Jellyfin.Api.Controllers;
 23
 24/// <summary>
 25/// The genres controller.
 26/// </summary>
 27[Authorize]
 28public class GenresController : BaseJellyfinApiController
 29{
 30    private readonly IUserManager _userManager;
 31    private readonly ILibraryManager _libraryManager;
 32    private readonly IDtoService _dtoService;
 33
 34    /// <summary>
 35    /// Initializes a new instance of the <see cref="GenresController"/> class.
 36    /// </summary>
 37    /// <param name="userManager">Instance of the <see cref="IUserManager"/> interface.</param>
 38    /// <param name="libraryManager">Instance of the <see cref="ILibraryManager"/> interface.</param>
 39    /// <param name="dtoService">Instance of the <see cref="IDtoService"/> interface.</param>
 040    public GenresController(
 041        IUserManager userManager,
 042        ILibraryManager libraryManager,
 043        IDtoService dtoService)
 44    {
 045        _userManager = userManager;
 046        _libraryManager = libraryManager;
 047        _dtoService = dtoService;
 048    }
 49
 50    /// <summary>
 51    /// Gets all genres from a given item, folder, or the entire library.
 52    /// </summary>
 53    /// <param name="startIndex">Optional. The record index to start at. All items with a lower index will be dropped fr
 54    /// <param name="limit">Optional. The maximum number of records to return.</param>
 55    /// <param name="searchTerm">The search term.</param>
 56    /// <param name="parentId">Specify this to localize the search to a specific item or folder. Omit to use the root.</
 57    /// <param name="fields">Optional. Specify additional fields of information to return in the output.</param>
 58    /// <param name="excludeItemTypes">Optional. If specified, results will be filtered out based on item type. This all
 59    /// <param name="includeItemTypes">Optional. If specified, results will be filtered in based on item type. This allo
 60    /// <param name="isFavorite">Optional filter by items that are marked as favorite, or not.</param>
 61    /// <param name="imageTypeLimit">Optional, the max number of images to return, per image type.</param>
 62    /// <param name="enableImageTypes">Optional. The image types to include in the output.</param>
 63    /// <param name="userId">User id.</param>
 64    /// <param name="nameStartsWithOrGreater">Optional filter by items whose name is sorted equally or greater than a gi
 65    /// <param name="nameStartsWith">Optional filter by items whose name is sorted equally than a given input string.</p
 66    /// <param name="nameLessThan">Optional filter by items whose name is equally or lesser than a given input string.</
 67    /// <param name="sortBy">Optional. Specify one or more sort orders, comma delimited.</param>
 68    /// <param name="sortOrder">Sort Order - Ascending,Descending.</param>
 69    /// <param name="enableImages">Optional, include image information in output.</param>
 70    /// <param name="enableTotalRecordCount">Optional. Include total record count.</param>
 71    /// <response code="200">Genres returned.</response>
 72    /// <returns>An <see cref="OkResult"/> containing the queryresult of genres.</returns>
 73    [HttpGet]
 74    [ProducesResponseType(StatusCodes.Status200OK)]
 75    public ActionResult<QueryResult<BaseItemDto>> GetGenres(
 76        [FromQuery] int? startIndex,
 77        [FromQuery] int? limit,
 78        [FromQuery] string? searchTerm,
 79        [FromQuery] Guid? parentId,
 80        [FromQuery, ModelBinder(typeof(CommaDelimitedCollectionModelBinder))] ItemFields[] fields,
 81        [FromQuery, ModelBinder(typeof(CommaDelimitedCollectionModelBinder))] BaseItemKind[] excludeItemTypes,
 82        [FromQuery, ModelBinder(typeof(CommaDelimitedCollectionModelBinder))] BaseItemKind[] includeItemTypes,
 83        [FromQuery] bool? isFavorite,
 84        [FromQuery] int? imageTypeLimit,
 85        [FromQuery, ModelBinder(typeof(CommaDelimitedCollectionModelBinder))] ImageType[] enableImageTypes,
 86        [FromQuery] Guid? userId,
 87        [FromQuery] string? nameStartsWithOrGreater,
 88        [FromQuery] string? nameStartsWith,
 89        [FromQuery] string? nameLessThan,
 90        [FromQuery, ModelBinder(typeof(CommaDelimitedCollectionModelBinder))] ItemSortBy[] sortBy,
 91        [FromQuery, ModelBinder(typeof(CommaDelimitedCollectionModelBinder))] SortOrder[] sortOrder,
 92        [FromQuery] bool? enableImages = true,
 93        [FromQuery] bool enableTotalRecordCount = true)
 94    {
 095        userId = RequestHelpers.GetUserId(User, userId);
 096        var dtoOptions = new DtoOptions { Fields = fields }
 097            .AddAdditionalDtoOptions(enableImages, false, imageTypeLimit, enableImageTypes);
 98
 099        User? user = userId.IsNullOrEmpty()
 0100            ? null
 0101            : _userManager.GetUserById(userId.Value);
 102
 0103        var parentItem = _libraryManager.GetParentItem(parentId, userId);
 104
 0105        var query = new InternalItemsQuery(user)
 0106        {
 0107            ExcludeItemTypes = excludeItemTypes,
 0108            IncludeItemTypes = includeItemTypes,
 0109            StartIndex = startIndex,
 0110            Limit = limit,
 0111            IsFavorite = isFavorite,
 0112            NameLessThan = nameLessThan,
 0113            NameStartsWith = nameStartsWith,
 0114            NameStartsWithOrGreater = nameStartsWithOrGreater,
 0115            DtoOptions = dtoOptions,
 0116            SearchTerm = searchTerm,
 0117            EnableTotalRecordCount = enableTotalRecordCount,
 0118            OrderBy = RequestHelpers.GetOrderBy(sortBy, sortOrder)
 0119        };
 120
 0121        if (parentId.HasValue)
 122        {
 0123            if (parentItem is Folder)
 124            {
 0125                query.AncestorIds = new[] { parentId.Value };
 126            }
 127            else
 128            {
 0129                query.ItemIds = new[] { parentId.Value };
 130            }
 131        }
 132
 133        QueryResult<(BaseItem, ItemCounts)> result;
 0134        if (parentItem is ICollectionFolder parentCollectionFolder
 0135            && (parentCollectionFolder.CollectionType == CollectionType.music
 0136                || parentCollectionFolder.CollectionType == CollectionType.musicvideos))
 137        {
 0138            result = _libraryManager.GetMusicGenres(query);
 139        }
 140        else
 141        {
 0142            result = _libraryManager.GetGenres(query);
 143        }
 144
 0145        var shouldIncludeItemTypes = includeItemTypes.Length != 0;
 0146        return RequestHelpers.CreateQueryResult(result, dtoOptions, _dtoService, shouldIncludeItemTypes, user);
 147    }
 148
 149    /// <summary>
 150    /// Gets a genre, by name.
 151    /// </summary>
 152    /// <param name="genreName">The genre name.</param>
 153    /// <param name="userId">The user id.</param>
 154    /// <response code="200">Genres returned.</response>
 155    /// <returns>An <see cref="OkResult"/> containing the genre.</returns>
 156    [HttpGet("{genreName}")]
 157    [ProducesResponseType(StatusCodes.Status200OK)]
 158    public ActionResult<BaseItemDto> GetGenre([FromRoute, Required] string genreName, [FromQuery] Guid? userId)
 159    {
 0160        userId = RequestHelpers.GetUserId(User, userId);
 0161        var dtoOptions = new DtoOptions();
 162
 163        Genre? item;
 0164        if (genreName.Contains(BaseItem.SlugChar, StringComparison.OrdinalIgnoreCase))
 165        {
 0166            item = GetItemFromSlugName<Genre>(_libraryManager, genreName, dtoOptions, BaseItemKind.Genre);
 167        }
 168        else
 169        {
 0170            item = _libraryManager.GetGenre(genreName);
 171        }
 172
 0173        item ??= new Genre();
 174
 0175        var user = userId.IsNullOrEmpty()
 0176            ? null
 0177            : _userManager.GetUserById(userId.Value);
 178
 0179        return _dtoService.GetBaseItemDto(item, dtoOptions, user);
 180    }
 181
 182    private T? GetItemFromSlugName<T>(ILibraryManager libraryManager, string name, DtoOptions dtoOptions, BaseItemKind b
 183        where T : BaseItem, new()
 184    {
 0185        var result = libraryManager.GetItemList(new InternalItemsQuery
 0186        {
 0187            Name = name.Replace(BaseItem.SlugChar, '&'),
 0188            IncludeItemTypes = new[] { baseItemKind },
 0189            DtoOptions = dtoOptions
 0190        }).OfType<T>().FirstOrDefault();
 191
 0192        result ??= libraryManager.GetItemList(new InternalItemsQuery
 0193        {
 0194            Name = name.Replace(BaseItem.SlugChar, '/'),
 0195            IncludeItemTypes = new[] { baseItemKind },
 0196            DtoOptions = dtoOptions
 0197        }).OfType<T>().FirstOrDefault();
 198
 0199        result ??= libraryManager.GetItemList(new InternalItemsQuery
 0200        {
 0201            Name = name.Replace(BaseItem.SlugChar, '?'),
 0202            IncludeItemTypes = new[] { baseItemKind },
 0203            DtoOptions = dtoOptions
 0204        }).OfType<T>().FirstOrDefault();
 205
 0206        return result;
 207    }
 208}