2
0

CollectionController.cs 4.8 KB

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