2020-11-21 13:26:03 +00:00
|
|
|
using System;
|
2020-08-06 14:17:45 +00:00
|
|
|
using System.ComponentModel.DataAnnotations;
|
2020-08-21 20:01:19 +00:00
|
|
|
using System.Threading.Tasks;
|
2023-02-13 14:42:04 +00:00
|
|
|
using Jellyfin.Api.Constants;
|
2020-06-25 10:51:18 +00:00
|
|
|
using Jellyfin.Api.Extensions;
|
2020-11-17 03:29:46 +00:00
|
|
|
using Jellyfin.Api.ModelBinders;
|
2020-06-25 10:51:18 +00:00
|
|
|
using MediaBrowser.Controller.Collections;
|
|
|
|
using MediaBrowser.Controller.Dto;
|
|
|
|
using MediaBrowser.Model.Collections;
|
|
|
|
using Microsoft.AspNetCore.Authorization;
|
|
|
|
using Microsoft.AspNetCore.Http;
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
|
2023-01-31 11:18:10 +00:00
|
|
|
namespace Jellyfin.Api.Controllers;
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// The collection controller.
|
|
|
|
/// </summary>
|
|
|
|
[Route("Collections")]
|
2023-02-13 14:42:04 +00:00
|
|
|
[Authorize(Policy = Policies.CollectionManagement)]
|
2023-01-31 11:18:10 +00:00
|
|
|
public class CollectionController : BaseJellyfinApiController
|
2020-06-25 10:51:18 +00:00
|
|
|
{
|
2023-01-31 11:18:10 +00:00
|
|
|
private readonly ICollectionManager _collectionManager;
|
|
|
|
private readonly IDtoService _dtoService;
|
|
|
|
|
2020-06-25 10:51:18 +00:00
|
|
|
/// <summary>
|
2023-01-31 11:18:10 +00:00
|
|
|
/// Initializes a new instance of the <see cref="CollectionController"/> class.
|
2020-06-25 10:51:18 +00:00
|
|
|
/// </summary>
|
2023-01-31 11:18:10 +00:00
|
|
|
/// <param name="collectionManager">Instance of <see cref="ICollectionManager"/> interface.</param>
|
|
|
|
/// <param name="dtoService">Instance of <see cref="IDtoService"/> interface.</param>
|
|
|
|
public CollectionController(
|
|
|
|
ICollectionManager collectionManager,
|
|
|
|
IDtoService dtoService)
|
2020-06-25 10:51:18 +00:00
|
|
|
{
|
2023-01-31 11:18:10 +00:00
|
|
|
_collectionManager = collectionManager;
|
|
|
|
_dtoService = dtoService;
|
|
|
|
}
|
2020-06-25 10:51:18 +00:00
|
|
|
|
2023-01-31 11:18:10 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Creates a new collection.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="name">The name of the collection.</param>
|
|
|
|
/// <param name="ids">Item Ids to add to the collection.</param>
|
|
|
|
/// <param name="parentId">Optional. Create the collection within a specific folder.</param>
|
|
|
|
/// <param name="isLocked">Whether or not to lock the new collection.</param>
|
|
|
|
/// <response code="200">Collection created.</response>
|
|
|
|
/// <returns>A <see cref="CollectionCreationOptions"/> with information about the new collection.</returns>
|
|
|
|
[HttpPost]
|
|
|
|
[ProducesResponseType(StatusCodes.Status200OK)]
|
|
|
|
public async Task<ActionResult<CollectionCreationResult>> CreateCollection(
|
|
|
|
[FromQuery] string? name,
|
|
|
|
[FromQuery, ModelBinder(typeof(CommaDelimitedArrayModelBinder))] string[] ids,
|
|
|
|
[FromQuery] Guid? parentId,
|
|
|
|
[FromQuery] bool isLocked = false)
|
|
|
|
{
|
|
|
|
var userId = User.GetUserId();
|
2020-06-25 10:51:18 +00:00
|
|
|
|
2023-01-31 11:18:10 +00:00
|
|
|
var item = await _collectionManager.CreateCollectionAsync(new CollectionCreationOptions
|
2020-06-25 10:51:18 +00:00
|
|
|
{
|
2023-01-31 11:18:10 +00:00
|
|
|
IsLocked = isLocked,
|
|
|
|
Name = name,
|
|
|
|
ParentId = parentId,
|
|
|
|
ItemIdList = ids,
|
|
|
|
UserIds = new[] { userId }
|
|
|
|
}).ConfigureAwait(false);
|
2020-06-25 10:51:18 +00:00
|
|
|
|
2023-01-31 11:18:10 +00:00
|
|
|
var dtoOptions = new DtoOptions().AddClientFields(User);
|
2020-06-25 10:51:18 +00:00
|
|
|
|
2023-01-31 11:18:10 +00:00
|
|
|
var dto = _dtoService.GetBaseItemDto(item, dtoOptions);
|
2020-06-25 10:51:18 +00:00
|
|
|
|
2023-01-31 11:18:10 +00:00
|
|
|
return new CollectionCreationResult
|
2020-06-25 10:51:18 +00:00
|
|
|
{
|
2023-01-31 11:18:10 +00:00
|
|
|
Id = dto.Id
|
|
|
|
};
|
|
|
|
}
|
2020-06-25 10:51:18 +00:00
|
|
|
|
2023-01-31 11:18:10 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Adds items to a collection.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="collectionId">The collection id.</param>
|
|
|
|
/// <param name="ids">Item ids, comma delimited.</param>
|
|
|
|
/// <response code="204">Items added to collection.</response>
|
|
|
|
/// <returns>A <see cref="NoContentResult"/> indicating success.</returns>
|
|
|
|
[HttpPost("{collectionId}/Items")]
|
|
|
|
[ProducesResponseType(StatusCodes.Status204NoContent)]
|
|
|
|
public async Task<ActionResult> AddToCollection(
|
|
|
|
[FromRoute, Required] Guid collectionId,
|
|
|
|
[FromQuery, Required, ModelBinder(typeof(CommaDelimitedArrayModelBinder))] Guid[] ids)
|
|
|
|
{
|
|
|
|
await _collectionManager.AddToCollectionAsync(collectionId, ids).ConfigureAwait(true);
|
|
|
|
return NoContent();
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Removes items from a collection.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="collectionId">The collection id.</param>
|
|
|
|
/// <param name="ids">Item ids, comma delimited.</param>
|
|
|
|
/// <response code="204">Items removed from collection.</response>
|
|
|
|
/// <returns>A <see cref="NoContentResult"/> indicating success.</returns>
|
|
|
|
[HttpDelete("{collectionId}/Items")]
|
|
|
|
[ProducesResponseType(StatusCodes.Status204NoContent)]
|
|
|
|
public async Task<ActionResult> RemoveFromCollection(
|
|
|
|
[FromRoute, Required] Guid collectionId,
|
|
|
|
[FromQuery, Required, ModelBinder(typeof(CommaDelimitedArrayModelBinder))] Guid[] ids)
|
|
|
|
{
|
|
|
|
await _collectionManager.RemoveFromCollectionAsync(collectionId, ids).ConfigureAwait(false);
|
|
|
|
return NoContent();
|
2020-06-25 10:51:18 +00:00
|
|
|
}
|
|
|
|
}
|