CollectionController.cs 4.9 KB

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