aboutsummaryrefslogtreecommitdiff
path: root/Jellyfin.Api/Controllers/CollectionController.cs
blob: 29db0b17824379a0b42e012bfecb500aaa368376 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
using System;
using Jellyfin.Api.Constants;
using Jellyfin.Api.Extensions;
using Jellyfin.Api.Helpers;
using MediaBrowser.Controller.Collections;
using MediaBrowser.Controller.Dto;
using MediaBrowser.Controller.Net;
using MediaBrowser.Model.Collections;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace Jellyfin.Api.Controllers
{
    /// <summary>
    /// The collection controller.
    /// </summary>
    [Authorize(Policy = Policies.DefaultAuthorization)]
    [Route("/Collections")]
    public class CollectionController : BaseJellyfinApiController
    {
        private readonly ICollectionManager _collectionManager;
        private readonly IDtoService _dtoService;
        private readonly IAuthorizationContext _authContext;

        /// <summary>
        /// Initializes a new instance of the <see cref="CollectionController"/> class.
        /// </summary>
        /// <param name="collectionManager">Instance of <see cref="ICollectionManager"/> interface.</param>
        /// <param name="dtoService">Instance of <see cref="IDtoService"/> interface.</param>
        /// <param name="authContext">Instance of <see cref="IAuthorizationContext"/> interface.</param>
        public CollectionController(
            ICollectionManager collectionManager,
            IDtoService dtoService,
            IAuthorizationContext authContext)
        {
            _collectionManager = collectionManager;
            _dtoService = dtoService;
            _authContext = authContext;
        }

        /// <summary>
        /// Creates a new collection.
        /// </summary>
        /// <param name="name">The name of the collection.</param>
        /// <param name="ids">Item Ids to add to the collection.</param>
        /// <param name="isLocked">Whether or not to lock the new collection.</param>
        /// <param name="parentId">Optional. Create the collection within a specific folder.</param>
        /// <response code="200">Collection created.</response>
        /// <returns>A <see cref="CollectionCreationOptions"/> with information about the new collection.</returns>
        [HttpPost]
        [ProducesResponseType(StatusCodes.Status200OK)]
        public ActionResult<CollectionCreationResult> CreateCollection(
            [FromQuery] string name,
            [FromQuery] string ids,
            [FromQuery] bool isLocked,
            [FromQuery] Guid? parentId)
        {
            var userId = _authContext.GetAuthorizationInfo(Request).UserId;

            var item = _collectionManager.CreateCollection(new CollectionCreationOptions
            {
                IsLocked = isLocked,
                Name = name,
                ParentId = parentId,
                ItemIdList = RequestHelpers.Split(ids, ',', true),
                UserIds = new[] { userId }
            });

            var dtoOptions = new DtoOptions().AddClientFields(Request);

            var dto = _dtoService.GetBaseItemDto(item, dtoOptions);

            return new CollectionCreationResult
            {
                Id = dto.Id
            };
        }

        /// <summary>
        /// Adds items to a collection.
        /// </summary>
        /// <param name="collectionId">The collection id.</param>
        /// <param name="itemIds">Item ids, comma delimited.</param>
        /// <response code="204">Items added to collection.</response>
        /// <returns>A <see cref="NoContentResult"/> indicating success.</returns>
        [HttpPost("{collectionId}/Items")]
        [ProducesResponseType(StatusCodes.Status204NoContent)]
        public ActionResult AddToCollection([FromRoute] Guid collectionId, [FromQuery] string itemIds)
        {
            _collectionManager.AddToCollection(collectionId, RequestHelpers.Split(itemIds, ',', true));
            return NoContent();
        }

        /// <summary>
        /// Removes items from a collection.
        /// </summary>
        /// <param name="collectionId">The collection id.</param>
        /// <param name="itemIds">Item ids, comma delimited.</param>
        /// <response code="204">Items removed from collection.</response>
        /// <returns>A <see cref="NoContentResult"/> indicating success.</returns>
        [HttpDelete("{collectionId}/Items")]
        [ProducesResponseType(StatusCodes.Status204NoContent)]
        public ActionResult RemoveFromCollection([FromRoute] Guid collectionId, [FromQuery] string itemIds)
        {
            _collectionManager.RemoveFromCollection(collectionId, RequestHelpers.Split(itemIds, ',', true));
            return NoContent();
        }
    }
}