CollectionController.cs 4.2 KB

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