CollectionController.cs 4.1 KB

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