2020-06-25 12:51:18 +02:00
|
|
|
|
using System;
|
2020-08-06 16:17:45 +02:00
|
|
|
|
using System.ComponentModel.DataAnnotations;
|
2020-08-21 22:01:19 +02:00
|
|
|
|
using System.Threading.Tasks;
|
2020-06-25 12:51:18 +02:00
|
|
|
|
using Jellyfin.Api.Constants;
|
|
|
|
|
using Jellyfin.Api.Extensions;
|
|
|
|
|
using Jellyfin.Api.Helpers;
|
|
|
|
|
using MediaBrowser.Controller.Collections;
|
|
|
|
|
using MediaBrowser.Controller.Dto;
|
|
|
|
|
using MediaBrowser.Controller.Net;
|
|
|
|
|
using MediaBrowser.Model.Collections;
|
|
|
|
|
using Microsoft.AspNetCore.Authorization;
|
|
|
|
|
using Microsoft.AspNetCore.Http;
|
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
|
|
|
|
|
|
namespace Jellyfin.Api.Controllers
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// The collection controller.
|
|
|
|
|
/// </summary>
|
2020-08-04 16:27:54 +02:00
|
|
|
|
[Route("Collections")]
|
2020-08-04 16:30:03 +02:00
|
|
|
|
[Authorize(Policy = Policies.DefaultAuthorization)]
|
2020-06-25 12:51:18 +02:00
|
|
|
|
public class CollectionController : BaseJellyfinApiController
|
|
|
|
|
{
|
|
|
|
|
private readonly ICollectionManager _collectionManager;
|
|
|
|
|
private readonly IDtoService _dtoService;
|
|
|
|
|
private readonly IAuthorizationContext _authContext;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Initializes a new instance of the <see cref="CollectionController"/> class.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="collectionManager">Instance of <see cref="ICollectionManager"/> interface.</param>
|
|
|
|
|
/// <param name="dtoService">Instance of <see cref="IDtoService"/> interface.</param>
|
|
|
|
|
/// <param name="authContext">Instance of <see cref="IAuthorizationContext"/> interface.</param>
|
|
|
|
|
public CollectionController(
|
|
|
|
|
ICollectionManager collectionManager,
|
|
|
|
|
IDtoService dtoService,
|
|
|
|
|
IAuthorizationContext authContext)
|
|
|
|
|
{
|
|
|
|
|
_collectionManager = collectionManager;
|
|
|
|
|
_dtoService = dtoService;
|
|
|
|
|
_authContext = authContext;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <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>
|
2020-07-07 17:10:51 +02:00
|
|
|
|
/// <param name="isLocked">Whether or not to lock the new collection.</param>
|
2020-06-25 17:15:08 +02:00
|
|
|
|
/// <response code="200">Collection created.</response>
|
2020-06-25 12:51:18 +02:00
|
|
|
|
/// <returns>A <see cref="CollectionCreationOptions"/> with information about the new collection.</returns>
|
|
|
|
|
[HttpPost]
|
2020-06-25 17:15:08 +02:00
|
|
|
|
[ProducesResponseType(StatusCodes.Status200OK)]
|
2020-08-21 22:01:19 +02:00
|
|
|
|
public async Task<ActionResult<CollectionCreationResult>> CreateCollection(
|
2020-06-27 18:50:44 +02:00
|
|
|
|
[FromQuery] string? name,
|
|
|
|
|
[FromQuery] string? ids,
|
2020-07-07 17:10:51 +02:00
|
|
|
|
[FromQuery] Guid? parentId,
|
|
|
|
|
[FromQuery] bool isLocked = false)
|
2020-06-25 12:51:18 +02:00
|
|
|
|
{
|
|
|
|
|
var userId = _authContext.GetAuthorizationInfo(Request).UserId;
|
|
|
|
|
|
2020-08-21 22:01:19 +02:00
|
|
|
|
var item = await _collectionManager.CreateCollectionAsync(new CollectionCreationOptions
|
2020-06-25 12:51:18 +02:00
|
|
|
|
{
|
|
|
|
|
IsLocked = isLocked,
|
|
|
|
|
Name = name,
|
|
|
|
|
ParentId = parentId,
|
|
|
|
|
ItemIdList = RequestHelpers.Split(ids, ',', true),
|
|
|
|
|
UserIds = new[] { userId }
|
2020-08-21 22:01:19 +02:00
|
|
|
|
}).ConfigureAwait(false);
|
2020-06-25 12:51:18 +02:00
|
|
|
|
|
|
|
|
|
var dtoOptions = new DtoOptions().AddClientFields(Request);
|
|
|
|
|
|
|
|
|
|
var dto = _dtoService.GetBaseItemDto(item, dtoOptions);
|
|
|
|
|
|
|
|
|
|
return new CollectionCreationResult
|
|
|
|
|
{
|
|
|
|
|
Id = dto.Id
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Adds items to a collection.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="collectionId">The collection id.</param>
|
|
|
|
|
/// <param name="itemIds">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)]
|
2020-09-09 22:28:30 +02:00
|
|
|
|
public async Task<ActionResult> AddToCollection([FromRoute, Required] Guid collectionId, [FromQuery, Required] string itemIds)
|
2020-06-25 12:51:18 +02:00
|
|
|
|
{
|
2020-08-21 22:01:19 +02:00
|
|
|
|
await _collectionManager.AddToCollectionAsync(collectionId, RequestHelpers.GetGuids(itemIds)).ConfigureAwait(true);
|
2020-06-25 12:51:18 +02:00
|
|
|
|
return NoContent();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Removes items from a collection.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="collectionId">The collection id.</param>
|
|
|
|
|
/// <param name="itemIds">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)]
|
2020-09-09 22:28:30 +02:00
|
|
|
|
public async Task<ActionResult> RemoveFromCollection([FromRoute, Required] Guid collectionId, [FromQuery, Required] string itemIds)
|
2020-06-25 12:51:18 +02:00
|
|
|
|
{
|
2020-08-21 22:01:19 +02:00
|
|
|
|
await _collectionManager.RemoveFromCollectionAsync(collectionId, RequestHelpers.GetGuids(itemIds)).ConfigureAwait(false);
|
2020-06-25 12:51:18 +02:00
|
|
|
|
return NoContent();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|