< Summary - Jellyfin

Information
Class: Jellyfin.Api.Controllers.CollectionController
Assembly: Jellyfin.Api
File(s): /srv/git/jellyfin/Jellyfin.Api/Controllers/CollectionController.cs
Line coverage
0%
Covered lines: 0
Uncovered lines: 6
Coverable lines: 6
Total lines: 111
Line coverage: 0%
Branch coverage
N/A
Covered branches: 0
Total branches: 0
Branch coverage: N/A
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%

File(s)

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

#LineLine coverage
 1using System;
 2using System.ComponentModel.DataAnnotations;
 3using System.Threading.Tasks;
 4using Jellyfin.Api.Constants;
 5using Jellyfin.Api.Extensions;
 6using Jellyfin.Api.ModelBinders;
 7using MediaBrowser.Common.Api;
 8using MediaBrowser.Controller.Collections;
 9using MediaBrowser.Controller.Dto;
 10using MediaBrowser.Model.Collections;
 11using Microsoft.AspNetCore.Authorization;
 12using Microsoft.AspNetCore.Http;
 13using Microsoft.AspNetCore.Mvc;
 14
 15namespace Jellyfin.Api.Controllers;
 16
 17/// <summary>
 18/// The collection controller.
 19/// </summary>
 20[Route("Collections")]
 21[Authorize(Policy = Policies.CollectionManagement)]
 22public class CollectionController : BaseJellyfinApiController
 23{
 24    private readonly ICollectionManager _collectionManager;
 25    private readonly IDtoService _dtoService;
 26
 27    /// <summary>
 28    /// Initializes a new instance of the <see cref="CollectionController"/> class.
 29    /// </summary>
 30    /// <param name="collectionManager">Instance of <see cref="ICollectionManager"/> interface.</param>
 31    /// <param name="dtoService">Instance of <see cref="IDtoService"/> interface.</param>
 032    public CollectionController(
 033        ICollectionManager collectionManager,
 034        IDtoService dtoService)
 35    {
 036        _collectionManager = collectionManager;
 037        _dtoService = dtoService;
 038    }
 39
 40    /// <summary>
 41    /// Creates a new collection.
 42    /// </summary>
 43    /// <param name="name">The name of the collection.</param>
 44    /// <param name="ids">Item Ids to add to the collection.</param>
 45    /// <param name="parentId">Optional. Create the collection within a specific folder.</param>
 46    /// <param name="isLocked">Whether or not to lock the new collection.</param>
 47    /// <response code="200">Collection created.</response>
 48    /// <returns>A <see cref="CollectionCreationOptions"/> with information about the new collection.</returns>
 49    [HttpPost]
 50    [ProducesResponseType(StatusCodes.Status200OK)]
 51    public async Task<ActionResult<CollectionCreationResult>> CreateCollection(
 52        [FromQuery] string? name,
 53        [FromQuery, ModelBinder(typeof(CommaDelimitedArrayModelBinder))] string[] ids,
 54        [FromQuery] Guid? parentId,
 55        [FromQuery] bool isLocked = false)
 56    {
 57        var userId = User.GetUserId();
 58
 59        var item = await _collectionManager.CreateCollectionAsync(new CollectionCreationOptions
 60        {
 61            IsLocked = isLocked,
 62            Name = name,
 63            ParentId = parentId,
 64            ItemIdList = ids,
 65            UserIds = new[] { userId }
 66        }).ConfigureAwait(false);
 67
 68        var dtoOptions = new DtoOptions().AddClientFields(User);
 69
 70        var dto = _dtoService.GetBaseItemDto(item, dtoOptions);
 71
 72        return new CollectionCreationResult
 73        {
 74            Id = dto.Id
 75        };
 76    }
 77
 78    /// <summary>
 79    /// Adds items to a collection.
 80    /// </summary>
 81    /// <param name="collectionId">The collection id.</param>
 82    /// <param name="ids">Item ids, comma delimited.</param>
 83    /// <response code="204">Items added to collection.</response>
 84    /// <returns>A <see cref="NoContentResult"/> indicating success.</returns>
 85    [HttpPost("{collectionId}/Items")]
 86    [ProducesResponseType(StatusCodes.Status204NoContent)]
 87    public async Task<ActionResult> AddToCollection(
 88        [FromRoute, Required] Guid collectionId,
 89        [FromQuery, Required, ModelBinder(typeof(CommaDelimitedArrayModelBinder))] Guid[] ids)
 90    {
 91        await _collectionManager.AddToCollectionAsync(collectionId, ids).ConfigureAwait(true);
 92        return NoContent();
 93    }
 94
 95    /// <summary>
 96    /// Removes items from a collection.
 97    /// </summary>
 98    /// <param name="collectionId">The collection id.</param>
 99    /// <param name="ids">Item ids, comma delimited.</param>
 100    /// <response code="204">Items removed from collection.</response>
 101    /// <returns>A <see cref="NoContentResult"/> indicating success.</returns>
 102    [HttpDelete("{collectionId}/Items")]
 103    [ProducesResponseType(StatusCodes.Status204NoContent)]
 104    public async Task<ActionResult> RemoveFromCollection(
 105        [FromRoute, Required] Guid collectionId,
 106        [FromQuery, Required, ModelBinder(typeof(CommaDelimitedArrayModelBinder))] Guid[] ids)
 107    {
 108        await _collectionManager.RemoveFromCollectionAsync(collectionId, ids).ConfigureAwait(false);
 109        return NoContent();
 110    }
 111}