< Summary - Jellyfin

Information
Class: Jellyfin.Api.Controllers.TvShowsController
Assembly: Jellyfin.Api
File(s): /srv/git/jellyfin/Jellyfin.Api/Controllers/TvShowsController.cs
Line coverage
0%
Covered lines: 0
Uncovered lines: 126
Coverable lines: 126
Total lines: 388
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 46
Branch coverage: 0%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Coverage history

Coverage history 0 25 50 75 100 1/23/2026 - 12:11:06 AM Line coverage: 0% (0/126) Branch coverage: 0% (0/46) Total lines: 3901/29/2026 - 12:13:32 AM Line coverage: 0% (0/126) Branch coverage: 0% (0/46) Total lines: 3874/27/2026 - 12:15:04 AM Line coverage: 0% (0/126) Branch coverage: 0% (0/46) Total lines: 388 1/23/2026 - 12:11:06 AM Line coverage: 0% (0/126) Branch coverage: 0% (0/46) Total lines: 3901/29/2026 - 12:13:32 AM Line coverage: 0% (0/126) Branch coverage: 0% (0/46) Total lines: 3874/27/2026 - 12:15:04 AM Line coverage: 0% (0/126) Branch coverage: 0% (0/46) Total lines: 388

Coverage delta

Coverage delta 1 -1

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%210%
GetNextUp(...)0%2040%
GetUpcomingEpisodes(...)0%2040%
GetEpisodes(...)0%930300%
GetSeasons(...)0%2040%
ApplyPaging(...)0%2040%

File(s)

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

#LineLine coverage
 1using System;
 2using System.Collections.Generic;
 3using System.ComponentModel.DataAnnotations;
 4using System.Linq;
 5using Jellyfin.Api.Extensions;
 6using Jellyfin.Api.Helpers;
 7using Jellyfin.Api.ModelBinders;
 8using Jellyfin.Data.Enums;
 9using Jellyfin.Database.Implementations.Enums;
 10using Jellyfin.Extensions;
 11using MediaBrowser.Controller.Dto;
 12using MediaBrowser.Controller.Entities;
 13using MediaBrowser.Controller.Entities.TV;
 14using MediaBrowser.Controller.Library;
 15using MediaBrowser.Controller.TV;
 16using MediaBrowser.Model.Dto;
 17using MediaBrowser.Model.Entities;
 18using MediaBrowser.Model.Querying;
 19using Microsoft.AspNetCore.Authorization;
 20using Microsoft.AspNetCore.Http;
 21using Microsoft.AspNetCore.Mvc;
 22
 23namespace Jellyfin.Api.Controllers;
 24
 25/// <summary>
 26/// The tv shows controller.
 27/// </summary>
 28[Route("Shows")]
 29[Authorize]
 30[Tags("Show")]
 31public class TvShowsController : BaseJellyfinApiController
 32{
 33    private readonly IUserManager _userManager;
 34    private readonly ILibraryManager _libraryManager;
 35    private readonly IDtoService _dtoService;
 36    private readonly ITVSeriesManager _tvSeriesManager;
 37
 38    /// <summary>
 39    /// Initializes a new instance of the <see cref="TvShowsController"/> class.
 40    /// </summary>
 41    /// <param name="userManager">Instance of the <see cref="IUserManager"/> interface.</param>
 42    /// <param name="libraryManager">Instance of the <see cref="ILibraryManager"/> interface.</param>
 43    /// <param name="dtoService">Instance of the <see cref="IDtoService"/> interface.</param>
 44    /// <param name="tvSeriesManager">Instance of the <see cref="ITVSeriesManager"/> interface.</param>
 045    public TvShowsController(
 046        IUserManager userManager,
 047        ILibraryManager libraryManager,
 048        IDtoService dtoService,
 049        ITVSeriesManager tvSeriesManager)
 50    {
 051        _userManager = userManager;
 052        _libraryManager = libraryManager;
 053        _dtoService = dtoService;
 054        _tvSeriesManager = tvSeriesManager;
 055    }
 56
 57    /// <summary>
 58    /// Gets a list of next up episodes.
 59    /// </summary>
 60    /// <param name="userId">The user id of the user to get the next up episodes for.</param>
 61    /// <param name="startIndex">Optional. The record index to start at. All items with a lower index will be dropped fr
 62    /// <param name="limit">Optional. The maximum number of records to return.</param>
 63    /// <param name="fields">Optional. Specify additional fields of information to return in the output.</param>
 64    /// <param name="seriesId">Optional. Filter by series id.</param>
 65    /// <param name="parentId">Optional. Specify this to localize the search to a specific item or folder. Omit to use t
 66    /// <param name="enableImages">Optional. Include image information in output.</param>
 67    /// <param name="imageTypeLimit">Optional. The max number of images to return, per image type.</param>
 68    /// <param name="enableImageTypes">Optional. The image types to include in the output.</param>
 69    /// <param name="enableUserData">Optional. Include user data.</param>
 70    /// <param name="nextUpDateCutoff">Optional. Starting date of shows to show in Next Up section.</param>
 71    /// <param name="enableTotalRecordCount">Whether to enable the total records count. Defaults to true.</param>
 72    /// <param name="enableResumable">Whether to include resumable episodes in next up results.</param>
 73    /// <param name="enableRewatching">Whether to include watched episodes in next up results.</param>
 74    /// <returns>A <see cref="QueryResult{BaseItemDto}"/> with the next up episodes.</returns>
 75    [HttpGet("NextUp")]
 76    [ProducesResponseType(StatusCodes.Status200OK)]
 77    public ActionResult<QueryResult<BaseItemDto>> GetNextUp(
 78        [FromQuery] Guid? userId,
 79        [FromQuery] int? startIndex,
 80        [FromQuery] int? limit,
 81        [FromQuery, ModelBinder(typeof(CommaDelimitedCollectionModelBinder))] ItemFields[] fields,
 82        [FromQuery] Guid? seriesId,
 83        [FromQuery] Guid? parentId,
 84        [FromQuery] bool? enableImages,
 85        [FromQuery] int? imageTypeLimit,
 86        [FromQuery, ModelBinder(typeof(CommaDelimitedCollectionModelBinder))] ImageType[] enableImageTypes,
 87        [FromQuery] bool? enableUserData,
 88        [FromQuery] DateTime? nextUpDateCutoff,
 89        [FromQuery] bool enableTotalRecordCount = true,
 90        [FromQuery] bool enableResumable = true,
 91        [FromQuery] bool enableRewatching = false)
 92    {
 093        var user = _userManager.GetUserById(RequestHelpers.GetUserId(User, userId));
 094        if (user is null)
 95        {
 096            return NotFound();
 97        }
 98
 099        var options = new DtoOptions { Fields = fields }
 0100            .AddAdditionalDtoOptions(enableImages, enableUserData, imageTypeLimit, enableImageTypes);
 101
 0102        var result = _tvSeriesManager.GetNextUp(
 0103            new NextUpQuery
 0104            {
 0105                Limit = limit,
 0106                ParentId = parentId,
 0107                SeriesId = seriesId,
 0108                StartIndex = startIndex,
 0109                User = user,
 0110                EnableTotalRecordCount = enableTotalRecordCount,
 0111                NextUpDateCutoff = nextUpDateCutoff ?? DateTime.MinValue,
 0112                EnableResumable = enableResumable,
 0113                EnableRewatching = enableRewatching
 0114            },
 0115            options);
 116
 0117        var returnItems = _dtoService.GetBaseItemDtos(result.Items, options, user);
 118
 0119        return new QueryResult<BaseItemDto>(
 0120            startIndex,
 0121            result.TotalRecordCount,
 0122            returnItems);
 123    }
 124
 125    /// <summary>
 126    /// Gets a list of upcoming episodes.
 127    /// </summary>
 128    /// <param name="userId">The user id of the user to get the upcoming episodes for.</param>
 129    /// <param name="startIndex">Optional. The record index to start at. All items with a lower index will be dropped fr
 130    /// <param name="limit">Optional. The maximum number of records to return.</param>
 131    /// <param name="fields">Optional. Specify additional fields of information to return in the output.</param>
 132    /// <param name="parentId">Optional. Specify this to localize the search to a specific item or folder. Omit to use t
 133    /// <param name="enableImages">Optional. Include image information in output.</param>
 134    /// <param name="imageTypeLimit">Optional. The max number of images to return, per image type.</param>
 135    /// <param name="enableImageTypes">Optional. The image types to include in the output.</param>
 136    /// <param name="enableUserData">Optional. Include user data.</param>
 137    /// <returns>A <see cref="QueryResult{BaseItemDto}"/> with the upcoming episodes.</returns>
 138    [HttpGet("Upcoming")]
 139    [ProducesResponseType(StatusCodes.Status200OK)]
 140    public ActionResult<QueryResult<BaseItemDto>> GetUpcomingEpisodes(
 141        [FromQuery] Guid? userId,
 142        [FromQuery] int? startIndex,
 143        [FromQuery] int? limit,
 144        [FromQuery, ModelBinder(typeof(CommaDelimitedCollectionModelBinder))] ItemFields[] fields,
 145        [FromQuery] Guid? parentId,
 146        [FromQuery] bool? enableImages,
 147        [FromQuery] int? imageTypeLimit,
 148        [FromQuery, ModelBinder(typeof(CommaDelimitedCollectionModelBinder))] ImageType[] enableImageTypes,
 149        [FromQuery] bool? enableUserData)
 150    {
 0151        userId = RequestHelpers.GetUserId(User, userId);
 0152        var user = userId.IsNullOrEmpty()
 0153            ? null
 0154            : _userManager.GetUserById(userId.Value);
 155
 0156        var minPremiereDate = DateTime.UtcNow.Date.AddDays(-1);
 157
 0158        var parentIdGuid = parentId ?? Guid.Empty;
 159
 0160        var options = new DtoOptions { Fields = fields }
 0161            .AddAdditionalDtoOptions(enableImages, enableUserData, imageTypeLimit, enableImageTypes);
 162
 0163        var itemsResult = _libraryManager.GetItemList(new InternalItemsQuery(user)
 0164        {
 0165            IncludeItemTypes = new[] { BaseItemKind.Episode },
 0166            OrderBy = new[] { (ItemSortBy.PremiereDate, SortOrder.Ascending), (ItemSortBy.SortName, SortOrder.Ascending)
 0167            MinPremiereDate = minPremiereDate,
 0168            StartIndex = startIndex,
 0169            Limit = limit,
 0170            ParentId = parentIdGuid,
 0171            Recursive = true,
 0172            DtoOptions = options
 0173        });
 174
 0175        var returnItems = _dtoService.GetBaseItemDtos(itemsResult, options, user);
 176
 0177        return new QueryResult<BaseItemDto>(
 0178            startIndex,
 0179            itemsResult.Count,
 0180            returnItems);
 181    }
 182
 183    /// <summary>
 184    /// Gets episodes for a tv season.
 185    /// </summary>
 186    /// <param name="seriesId">The series id.</param>
 187    /// <param name="userId">The user id.</param>
 188    /// <param name="fields">Optional. Specify additional fields of information to return in the output. This allows mul
 189    /// <param name="season">Optional filter by season number.</param>
 190    /// <param name="seasonId">Optional. Filter by season id.</param>
 191    /// <param name="isMissing">Optional. Filter by items that are missing episodes or not.</param>
 192    /// <param name="adjacentTo">Optional. Return items that are siblings of a supplied item.</param>
 193    /// <param name="startItemId">Optional. Skip through the list until a given item is found.</param>
 194    /// <param name="startIndex">Optional. The record index to start at. All items with a lower index will be dropped fr
 195    /// <param name="limit">Optional. The maximum number of records to return.</param>
 196    /// <param name="enableImages">Optional, include image information in output.</param>
 197    /// <param name="imageTypeLimit">Optional, the max number of images to return, per image type.</param>
 198    /// <param name="enableImageTypes">Optional. The image types to include in the output.</param>
 199    /// <param name="enableUserData">Optional. Include user data.</param>
 200    /// <param name="sortBy">Optional. Specify one or more sort orders, comma delimited. Options: Album, AlbumArtist, Ar
 201    /// <returns>A <see cref="QueryResult{BaseItemDto}"/> with the episodes on success or a <see cref="NotFoundResult"/>
 202    [HttpGet("{seriesId}/Episodes")]
 203    [ProducesResponseType(StatusCodes.Status200OK)]
 204    [ProducesResponseType(StatusCodes.Status404NotFound)]
 205    public ActionResult<QueryResult<BaseItemDto>> GetEpisodes(
 206        [FromRoute, Required] Guid seriesId,
 207        [FromQuery] Guid? userId,
 208        [FromQuery, ModelBinder(typeof(CommaDelimitedCollectionModelBinder))] ItemFields[] fields,
 209        [FromQuery] int? season,
 210        [FromQuery] Guid? seasonId,
 211        [FromQuery] bool? isMissing,
 212        [FromQuery] Guid? adjacentTo,
 213        [FromQuery] Guid? startItemId,
 214        [FromQuery] int? startIndex,
 215        [FromQuery] int? limit,
 216        [FromQuery] bool? enableImages,
 217        [FromQuery] int? imageTypeLimit,
 218        [FromQuery, ModelBinder(typeof(CommaDelimitedCollectionModelBinder))] ImageType[] enableImageTypes,
 219        [FromQuery] bool? enableUserData,
 220        [FromQuery] ItemSortBy? sortBy)
 221    {
 0222        userId = RequestHelpers.GetUserId(User, userId);
 0223        var user = userId.IsNullOrEmpty()
 0224            ? null
 0225            : _userManager.GetUserById(userId.Value);
 226
 227        List<BaseItem> episodes;
 228
 0229        var dtoOptions = new DtoOptions { Fields = fields }
 0230            .AddAdditionalDtoOptions(enableImages, enableUserData, imageTypeLimit, enableImageTypes);
 0231        var shouldIncludeMissingEpisodes = (user is not null && user.DisplayMissingEpisodes) || User.GetIsApiKey();
 232
 0233        if (seasonId.HasValue) // Season id was supplied. Get episodes by season id.
 234        {
 0235            var item = _libraryManager.GetItemById<BaseItem>(seasonId.Value);
 0236            if (item is not Season seasonItem)
 237            {
 0238                return NotFound("No season exists with Id " + seasonId);
 239            }
 240
 0241            episodes = seasonItem.GetEpisodes(user, dtoOptions, shouldIncludeMissingEpisodes);
 242        }
 0243        else if (season.HasValue) // Season number was supplied. Get episodes by season number
 244        {
 0245            var series = _libraryManager.GetItemById<Series>(seriesId);
 0246            if (series is null)
 247            {
 0248                return NotFound("Series not found");
 249            }
 250
 0251            var seasonItem = series
 0252                .GetSeasons(user, dtoOptions)
 0253                .FirstOrDefault(i => i.IndexNumber == season.Value);
 254
 0255            episodes = seasonItem is null ?
 0256                new List<BaseItem>()
 0257                : ((Season)seasonItem).GetEpisodes(user, dtoOptions, shouldIncludeMissingEpisodes);
 258        }
 259        else // No season number or season id was supplied. Returning all episodes.
 260        {
 0261            if (_libraryManager.GetItemById<BaseItem>(seriesId) is not Series series)
 262            {
 0263                return NotFound("Series not found");
 264            }
 265
 0266            episodes = series.GetEpisodes(user, dtoOptions, shouldIncludeMissingEpisodes).ToList();
 267        }
 268
 269        // Filter after the fact in case the ui doesn't want them
 0270        if (isMissing.HasValue)
 271        {
 0272            var val = isMissing.Value;
 0273            episodes = episodes
 0274                .Where(i => ((Episode)i).IsMissingEpisode == val)
 0275                .ToList();
 276        }
 277
 0278        if (startItemId.HasValue)
 279        {
 0280            episodes = episodes
 0281                .SkipWhile(i => !startItemId.Value.Equals(i.Id))
 0282                .ToList();
 283        }
 284
 285        // This must be the last filter
 0286        if (!adjacentTo.IsNullOrEmpty())
 287        {
 0288            episodes = UserViewBuilder.FilterForAdjacency(episodes, adjacentTo.Value).ToList();
 289        }
 290
 0291        if (sortBy == ItemSortBy.Random)
 292        {
 0293            episodes.Shuffle();
 294        }
 295
 0296        var returnItems = episodes;
 297
 0298        if (startIndex.HasValue || limit.HasValue)
 299        {
 0300            returnItems = ApplyPaging(episodes, startIndex, limit).ToList();
 301        }
 302
 0303        var dtos = _dtoService.GetBaseItemDtos(returnItems, dtoOptions, user);
 304
 0305        return new QueryResult<BaseItemDto>(
 0306            startIndex,
 0307            episodes.Count,
 0308            dtos);
 309    }
 310
 311    /// <summary>
 312    /// Gets seasons for a tv series.
 313    /// </summary>
 314    /// <param name="seriesId">The series id.</param>
 315    /// <param name="userId">The user id.</param>
 316    /// <param name="fields">Optional. Specify additional fields of information to return in the output. This allows mul
 317    /// <param name="isSpecialSeason">Optional. Filter by special season.</param>
 318    /// <param name="isMissing">Optional. Filter by items that are missing episodes or not.</param>
 319    /// <param name="adjacentTo">Optional. Return items that are siblings of a supplied item.</param>
 320    /// <param name="enableImages">Optional. Include image information in output.</param>
 321    /// <param name="imageTypeLimit">Optional. The max number of images to return, per image type.</param>
 322    /// <param name="enableImageTypes">Optional. The image types to include in the output.</param>
 323    /// <param name="enableUserData">Optional. Include user data.</param>
 324    /// <returns>A <see cref="QueryResult{BaseItemDto}"/> on success or a <see cref="NotFoundResult"/> if the series was
 325    [HttpGet("{seriesId}/Seasons")]
 326    [ProducesResponseType(StatusCodes.Status200OK)]
 327    [ProducesResponseType(StatusCodes.Status404NotFound)]
 328    public ActionResult<QueryResult<BaseItemDto>> GetSeasons(
 329        [FromRoute, Required] Guid seriesId,
 330        [FromQuery] Guid? userId,
 331        [FromQuery, ModelBinder(typeof(CommaDelimitedCollectionModelBinder))] ItemFields[] fields,
 332        [FromQuery] bool? isSpecialSeason,
 333        [FromQuery] bool? isMissing,
 334        [FromQuery] Guid? adjacentTo,
 335        [FromQuery] bool? enableImages,
 336        [FromQuery] int? imageTypeLimit,
 337        [FromQuery, ModelBinder(typeof(CommaDelimitedCollectionModelBinder))] ImageType[] enableImageTypes,
 338        [FromQuery] bool? enableUserData)
 339    {
 0340        userId = RequestHelpers.GetUserId(User, userId);
 0341        var user = userId.IsNullOrEmpty()
 0342            ? null
 0343            : _userManager.GetUserById(userId.Value);
 0344        var item = _libraryManager.GetItemById<Series>(seriesId, user);
 0345        if (item is null)
 346        {
 0347            return NotFound();
 348        }
 349
 0350        var seasons = item.GetItemList(new InternalItemsQuery(user)
 0351        {
 0352            IsMissing = isMissing,
 0353            IsSpecialSeason = isSpecialSeason,
 0354            AdjacentTo = adjacentTo
 0355        });
 356
 0357        var dtoOptions = new DtoOptions { Fields = fields }
 0358            .AddAdditionalDtoOptions(enableImages, enableUserData, imageTypeLimit, enableImageTypes);
 359
 0360        var returnItems = _dtoService.GetBaseItemDtos(seasons, dtoOptions, user);
 361
 0362        return new QueryResult<BaseItemDto>(returnItems);
 363    }
 364
 365    /// <summary>
 366    /// Applies the paging.
 367    /// </summary>
 368    /// <param name="items">The items.</param>
 369    /// <param name="startIndex">The start index.</param>
 370    /// <param name="limit">The limit.</param>
 371    /// <returns>IEnumerable{BaseItem}.</returns>
 372    private IEnumerable<BaseItem> ApplyPaging(IEnumerable<BaseItem> items, int? startIndex, int? limit)
 373    {
 374        // Start at
 0375        if (startIndex.HasValue)
 376        {
 0377            items = items.Skip(startIndex.Value);
 378        }
 379
 380        // Return limit
 0381        if (limit.HasValue)
 382        {
 0383            items = items.Take(limit.Value);
 384        }
 385
 0386        return items;
 387    }
 388}

Methods/Properties

.ctor(MediaBrowser.Controller.Library.IUserManager,MediaBrowser.Controller.Library.ILibraryManager,MediaBrowser.Controller.Dto.IDtoService,MediaBrowser.Controller.TV.ITVSeriesManager)
GetNextUp(System.Nullable`1<System.Guid>,System.Nullable`1<System.Int32>,System.Nullable`1<System.Int32>,MediaBrowser.Model.Querying.ItemFields[],System.Nullable`1<System.Guid>,System.Nullable`1<System.Guid>,System.Nullable`1<System.Boolean>,System.Nullable`1<System.Int32>,MediaBrowser.Model.Entities.ImageType[],System.Nullable`1<System.Boolean>,System.Nullable`1<System.DateTime>,System.Boolean,System.Boolean,System.Boolean)
GetUpcomingEpisodes(System.Nullable`1<System.Guid>,System.Nullable`1<System.Int32>,System.Nullable`1<System.Int32>,MediaBrowser.Model.Querying.ItemFields[],System.Nullable`1<System.Guid>,System.Nullable`1<System.Boolean>,System.Nullable`1<System.Int32>,MediaBrowser.Model.Entities.ImageType[],System.Nullable`1<System.Boolean>)
GetEpisodes(System.Guid,System.Nullable`1<System.Guid>,MediaBrowser.Model.Querying.ItemFields[],System.Nullable`1<System.Int32>,System.Nullable`1<System.Guid>,System.Nullable`1<System.Boolean>,System.Nullable`1<System.Guid>,System.Nullable`1<System.Guid>,System.Nullable`1<System.Int32>,System.Nullable`1<System.Int32>,System.Nullable`1<System.Boolean>,System.Nullable`1<System.Int32>,MediaBrowser.Model.Entities.ImageType[],System.Nullable`1<System.Boolean>,System.Nullable`1<Jellyfin.Data.Enums.ItemSortBy>)
GetSeasons(System.Guid,System.Nullable`1<System.Guid>,MediaBrowser.Model.Querying.ItemFields[],System.Nullable`1<System.Boolean>,System.Nullable`1<System.Boolean>,System.Nullable`1<System.Guid>,System.Nullable`1<System.Boolean>,System.Nullable`1<System.Int32>,MediaBrowser.Model.Entities.ImageType[],System.Nullable`1<System.Boolean>)
ApplyPaging(System.Collections.Generic.IEnumerable`1<MediaBrowser.Controller.Entities.BaseItem>,System.Nullable`1<System.Int32>,System.Nullable`1<System.Int32>)