CollectionController.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. using System;
  2. using Jellyfin.Api.Constants;
  3. using Jellyfin.Api.Extensions;
  4. using Jellyfin.Api.Helpers;
  5. using MediaBrowser.Controller.Collections;
  6. using MediaBrowser.Controller.Dto;
  7. using MediaBrowser.Controller.Net;
  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. {
  14. /// <summary>
  15. /// The collection controller.
  16. /// </summary>
  17. [Authorize(Policy = Policies.DefaultAuthorization)]
  18. [Route("/Collections")]
  19. public class CollectionController : BaseJellyfinApiController
  20. {
  21. private readonly ICollectionManager _collectionManager;
  22. private readonly IDtoService _dtoService;
  23. private readonly IAuthorizationContext _authContext;
  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. /// <param name="authContext">Instance of <see cref="IAuthorizationContext"/> interface.</param>
  30. public CollectionController(
  31. ICollectionManager collectionManager,
  32. IDtoService dtoService,
  33. IAuthorizationContext authContext)
  34. {
  35. _collectionManager = collectionManager;
  36. _dtoService = dtoService;
  37. _authContext = authContext;
  38. }
  39. /// <summary>
  40. /// Creates a new collection.
  41. /// </summary>
  42. /// <param name="name">The name of the collection.</param>
  43. /// <param name="ids">Item Ids to add to the collection.</param>
  44. /// <param name="isLocked">Whether or not to lock the new collection.</param>
  45. /// <param name="parentId">Optional. Create the collection within a specific folder.</param>
  46. /// <returns>A <see cref="CollectionCreationOptions"/> with information about the new collection.</returns>
  47. [HttpPost]
  48. public ActionResult<CollectionCreationResult> CreateCollection(
  49. [FromQuery] string name,
  50. [FromQuery] string ids,
  51. [FromQuery] bool isLocked,
  52. [FromQuery] Guid? parentId)
  53. {
  54. var userId = _authContext.GetAuthorizationInfo(Request).UserId;
  55. var item = _collectionManager.CreateCollection(new CollectionCreationOptions
  56. {
  57. IsLocked = isLocked,
  58. Name = name,
  59. ParentId = parentId,
  60. ItemIdList = RequestHelpers.Split(ids, ',', true),
  61. UserIds = new[] { userId }
  62. });
  63. var dtoOptions = new DtoOptions().AddClientFields(Request);
  64. var dto = _dtoService.GetBaseItemDto(item, dtoOptions);
  65. return new CollectionCreationResult
  66. {
  67. Id = dto.Id
  68. };
  69. }
  70. /// <summary>
  71. /// Adds items to a collection.
  72. /// </summary>
  73. /// <param name="collectionId">The collection id.</param>
  74. /// <param name="itemIds">Item ids, comma delimited.</param>
  75. /// <response code="204">Items added to collection.</response>
  76. /// <returns>A <see cref="NoContentResult"/> indicating success.</returns>
  77. [HttpPost("{collectionId}/Items")]
  78. [ProducesResponseType(StatusCodes.Status204NoContent)]
  79. public ActionResult AddToCollection([FromRoute] Guid collectionId, [FromQuery] string itemIds)
  80. {
  81. _collectionManager.AddToCollection(collectionId, RequestHelpers.Split(itemIds, ',', 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="itemIds">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 ActionResult RemoveFromCollection([FromRoute] Guid collectionId, [FromQuery] string itemIds)
  94. {
  95. _collectionManager.RemoveFromCollection(collectionId, RequestHelpers.Split(itemIds, ',', true));
  96. return NoContent();
  97. }
  98. }
  99. }