2
0

CollectionController.cs 4.3 KB

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