CollectionController.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. using System;
  2. using System.ComponentModel.DataAnnotations;
  3. using Jellyfin.Api.Constants;
  4. using Jellyfin.Api.Extensions;
  5. using Jellyfin.Api.Helpers;
  6. using MediaBrowser.Controller.Collections;
  7. using MediaBrowser.Controller.Dto;
  8. using MediaBrowser.Controller.Net;
  9. using MediaBrowser.Model.Collections;
  10. using Microsoft.AspNetCore.Authorization;
  11. using Microsoft.AspNetCore.Http;
  12. using Microsoft.AspNetCore.Mvc;
  13. namespace Jellyfin.Api.Controllers
  14. {
  15. /// <summary>
  16. /// The collection controller.
  17. /// </summary>
  18. [Route("Collections")]
  19. [Authorize(Policy = Policies.DefaultAuthorization)]
  20. public class CollectionController : BaseJellyfinApiController
  21. {
  22. private readonly ICollectionManager _collectionManager;
  23. private readonly IDtoService _dtoService;
  24. private readonly IAuthorizationContext _authContext;
  25. /// <summary>
  26. /// Initializes a new instance of the <see cref="CollectionController"/> class.
  27. /// </summary>
  28. /// <param name="collectionManager">Instance of <see cref="ICollectionManager"/> interface.</param>
  29. /// <param name="dtoService">Instance of <see cref="IDtoService"/> interface.</param>
  30. /// <param name="authContext">Instance of <see cref="IAuthorizationContext"/> interface.</param>
  31. public CollectionController(
  32. ICollectionManager collectionManager,
  33. IDtoService dtoService,
  34. IAuthorizationContext authContext)
  35. {
  36. _collectionManager = collectionManager;
  37. _dtoService = dtoService;
  38. _authContext = authContext;
  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 ActionResult<CollectionCreationResult> CreateCollection(
  52. [FromQuery] string? name,
  53. [FromQuery] string? ids,
  54. [FromQuery] Guid? parentId,
  55. [FromQuery] bool isLocked = false)
  56. {
  57. var userId = _authContext.GetAuthorizationInfo(Request).UserId;
  58. var item = _collectionManager.CreateCollection(new CollectionCreationOptions
  59. {
  60. IsLocked = isLocked,
  61. Name = name,
  62. ParentId = parentId,
  63. ItemIdList = RequestHelpers.Split(ids, ',', true),
  64. UserIds = new[] { userId }
  65. });
  66. var dtoOptions = new DtoOptions().AddClientFields(Request);
  67. var dto = _dtoService.GetBaseItemDto(item, dtoOptions);
  68. return new CollectionCreationResult
  69. {
  70. Id = dto.Id
  71. };
  72. }
  73. /// <summary>
  74. /// Adds items to a collection.
  75. /// </summary>
  76. /// <param name="collectionId">The collection id.</param>
  77. /// <param name="itemIds">Item ids, comma delimited.</param>
  78. /// <response code="204">Items added to collection.</response>
  79. /// <returns>A <see cref="NoContentResult"/> indicating success.</returns>
  80. [HttpPost("{collectionId}/Items")]
  81. [ProducesResponseType(StatusCodes.Status204NoContent)]
  82. public ActionResult AddToCollection([FromRoute] Guid collectionId, [FromQuery, Required] string? itemIds)
  83. {
  84. _collectionManager.AddToCollection(collectionId, RequestHelpers.Split(itemIds, ',', true));
  85. return NoContent();
  86. }
  87. /// <summary>
  88. /// Removes items from a collection.
  89. /// </summary>
  90. /// <param name="collectionId">The collection id.</param>
  91. /// <param name="itemIds">Item ids, comma delimited.</param>
  92. /// <response code="204">Items removed from collection.</response>
  93. /// <returns>A <see cref="NoContentResult"/> indicating success.</returns>
  94. [HttpDelete("{collectionId}/Items")]
  95. [ProducesResponseType(StatusCodes.Status204NoContent)]
  96. public ActionResult RemoveFromCollection([FromRoute] Guid collectionId, [FromQuery, Required] string? itemIds)
  97. {
  98. _collectionManager.RemoveFromCollection(collectionId, RequestHelpers.Split(itemIds, ',', true));
  99. return NoContent();
  100. }
  101. }
  102. }