Move PlaylistService to Jellyfin.Api

pull/3275/head
crobibero 4 years ago
parent e153214e76
commit d97e306cda

@ -0,0 +1,198 @@
#nullable enable
#pragma warning disable CA1801
using System;
using System.Linq;
using System.Threading.Tasks;
using Jellyfin.Api.Helpers;
using Jellyfin.Api.Models.PlaylistDtos;
using MediaBrowser.Controller.Dto;
using MediaBrowser.Controller.Library;
using MediaBrowser.Controller.Playlists;
using MediaBrowser.Model.Dto;
using MediaBrowser.Model.Playlists;
using MediaBrowser.Model.Querying;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.ModelBinding;
namespace Jellyfin.Api.Controllers
{
/// <summary>
/// Playlists controller.
/// </summary>
[Authorize]
public class PlaylistsController : BaseJellyfinApiController
{
private readonly IPlaylistManager _playlistManager;
private readonly IDtoService _dtoService;
private readonly IUserManager _userManager;
private readonly ILibraryManager _libraryManager;
/// <summary>
/// Initializes a new instance of the <see cref="PlaylistsController"/> class.
/// </summary>
/// <param name="dtoService">Instance of the <see cref="IDtoService"/> interface.</param>
/// <param name="playlistManager">Instance of the <see cref="IPlaylistManager"/> interface.</param>
/// <param name="userManager">Instance of the <see cref="IUserManager"/> interface.</param>
/// <param name="libraryManager">Instance of the <see cref="ILibraryManager"/> interface.</param>
public PlaylistsController(
IDtoService dtoService,
IPlaylistManager playlistManager,
IUserManager userManager,
ILibraryManager libraryManager)
{
_dtoService = dtoService;
_playlistManager = playlistManager;
_userManager = userManager;
_libraryManager = libraryManager;
}
/// <summary>
/// Creates a new playlist.
/// </summary>
/// <param name="createPlaylistRequest">The create playlist payload.</param>
/// <returns>
/// A <see cref="Task" /> that represents the asynchronous operation to create a playlist.
/// The task result contains an <see cref="OkResult"/> indicating success.
/// </returns>
[HttpPost]
[ProducesResponseType(StatusCodes.Status200OK)]
public async Task<ActionResult<PlaylistCreationResult>> CreatePlaylist(
[FromBody, BindRequired] CreatePlaylistDto createPlaylistRequest)
{
Guid[] idGuidArray = RequestHelpers.GetGuids(createPlaylistRequest.Ids);
var result = await _playlistManager.CreatePlaylist(new PlaylistCreationRequest
{
Name = createPlaylistRequest.Name,
ItemIdList = idGuidArray,
UserId = createPlaylistRequest.UserId,
MediaType = createPlaylistRequest.MediaType
}).ConfigureAwait(false);
return result;
}
/// <summary>
/// Adds items to a playlist.
/// </summary>
/// <param name="playlistId">The playlist id.</param>
/// <param name="ids">Item id, comma delimited.</param>
/// <param name="userId">The userId.</param>
/// <response code="200">Items added to playlist.</response>
/// <returns>An <see cref="OkResult"/> on success.</returns>
[HttpPost("{playlistId}/Items")]
[ProducesResponseType(StatusCodes.Status200OK)]
public ActionResult AddToPlaylist(
[FromRoute] string playlistId,
[FromQuery] string ids,
[FromQuery] Guid userId)
{
_playlistManager.AddToPlaylist(playlistId, RequestHelpers.GetGuids(ids), userId);
return Ok();
}
/// <summary>
/// Moves a playlist item.
/// </summary>
/// <param name="playlistId">The playlist id.</param>
/// <param name="itemId">The item id.</param>
/// <param name="newIndex">The new index.</param>
/// <response code="200">Item moved to new index.</response>
/// <returns>An <see cref="OkResult"/> on success.</returns>
[HttpPost("{playlistId}/Items/{itemId}/Move/{newIndex}")]
[ProducesResponseType(StatusCodes.Status200OK)]
public ActionResult MoveItem(
[FromRoute] string playlistId,
[FromRoute] string itemId,
[FromRoute] int newIndex)
{
_playlistManager.MoveItem(playlistId, itemId, newIndex);
return Ok();
}
/// <summary>
/// Removes items from a playlist.
/// </summary>
/// <param name="playlistId">The playlist id.</param>
/// <param name="entryIds">The item ids, comma delimited.</param>
/// <response code="200">Items removed.</response>
/// <returns>An <see cref="OkResult"/> on success.</returns>
[HttpDelete("{playlistId}/Items")]
[ProducesResponseType(StatusCodes.Status200OK)]
public ActionResult RemoveFromPlaylist([FromRoute] string playlistId, [FromQuery] string entryIds)
{
_playlistManager.RemoveFromPlaylist(playlistId, entryIds.Split(','));
return Ok();
}
/// <summary>
/// Gets the original items of a playlist.
/// </summary>
/// <param name="playlistId">The playlist id.</param>
/// <param name="userId">User id.</param>
/// <param name="startIndex">Optional. The record index to start at. All items with a lower index will be dropped from the results.</param>
/// <param name="limit">Optional. The maximum number of records to return.</param>
/// <param name="fields">Optional. Specify additional fields of information to return in the output. This allows multiple, comma delimited. Options: Budget, Chapters, DateCreated, Genres, HomePageUrl, IndexOptions, MediaStreams, Overview, ParentId, Path, People, ProviderIds, PrimaryImageAspectRatio, Revenue, SortName, Studios, Taglines.</param>
/// <param name="enableImages">Optional. Include image information in output.</param>
/// <param name="enableUserData">Optional. Include user data.</param>
/// <param name="imageTypeLimit">Optional. The max number of images to return, per image type.</param>
/// <param name="enableImageTypes">Optional. The image types to include in the output.</param>
/// <response code="200">Original playlist returned.</response>
/// <response code="404">Playlist not found.</response>
/// <returns>The original playlist items.</returns>
[HttpGet("{playlistId}/Items")]
public ActionResult<QueryResult<BaseItemDto>> GetPlaylistItems(
[FromRoute] Guid playlistId,
[FromRoute] Guid userId,
[FromRoute] int? startIndex,
[FromRoute] int? limit,
[FromRoute] string fields,
[FromRoute] bool? enableImages,
[FromRoute] bool? enableUserData,
[FromRoute] bool? imageTypeLimit,
[FromRoute] string enableImageTypes)
{
var playlist = (Playlist)_libraryManager.GetItemById(playlistId);
if (playlist == null)
{
return NotFound();
}
var user = !userId.Equals(Guid.Empty) ? _userManager.GetUserById(userId) : null;
var items = playlist.GetManageableItems().ToArray();
var count = items.Length;
if (startIndex.HasValue)
{
items = items.Skip(startIndex.Value).ToArray();
}
if (limit.HasValue)
{
items = items.Take(limit.Value).ToArray();
}
// TODO var dtoOptions = GetDtoOptions(_authContext, request);
var dtoOptions = new DtoOptions();
var dtos = _dtoService.GetBaseItemDtos(items.Select(i => i.Item2).ToList(), dtoOptions, user);
for (int index = 0; index < dtos.Count; index++)
{
dtos[index].PlaylistItemId = items[index].Item1.Id;
}
var result = new QueryResult<BaseItemDto>
{
Items = dtos,
TotalRecordCount = count
};
return result;
}
}
}

@ -0,0 +1,30 @@
#nullable enable
using System;
using System.Linq;
namespace Jellyfin.Api.Helpers
{
/// <summary>
/// Request Helpers.
/// </summary>
public static class RequestHelpers
{
/// <summary>
/// Get Guid array from string.
/// </summary>
/// <param name="value">String value.</param>
/// <returns>Guid array.</returns>
public static Guid[] GetGuids(string? value)
{
if (value == null)
{
return Array.Empty<Guid>();
}
return value.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries)
.Select(i => new Guid(i))
.ToArray();
}
}
}

@ -0,0 +1,31 @@
#nullable enable
using System;
namespace Jellyfin.Api.Models.PlaylistDtos
{
/// <summary>
/// Create new playlist dto.
/// </summary>
public class CreatePlaylistDto
{
/// <summary>
/// Gets or sets the name of the new playlist.
/// </summary>
public string? Name { get; set; }
/// <summary>
/// Gets or sets item ids to add to the playlist.
/// </summary>
public string? Ids { get; set; }
/// <summary>
/// Gets or sets the user id.
/// </summary>
public Guid UserId { get; set; }
/// <summary>
/// Gets or sets the media type.
/// </summary>
public string? MediaType { get; set; }
}
}

@ -14,66 +14,6 @@ using Microsoft.Extensions.Logging;
namespace MediaBrowser.Api
{
[Route("/Playlists", "POST", Summary = "Creates a new playlist")]
public class CreatePlaylist : IReturn<PlaylistCreationResult>
{
[ApiMember(Name = "Name", Description = "The name of the new playlist.", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "POST")]
public string Name { get; set; }
[ApiMember(Name = "Ids", Description = "Item Ids to add to the playlist", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "POST", AllowMultiple = true)]
public string Ids { get; set; }
[ApiMember(Name = "UserId", Description = "User Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "POST")]
public Guid UserId { get; set; }
[ApiMember(Name = "MediaType", Description = "The playlist media type", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "POST")]
public string MediaType { get; set; }
}
[Route("/Playlists/{Id}/Items", "POST", Summary = "Adds items to a playlist")]
public class AddToPlaylist : IReturnVoid
{
[ApiMember(Name = "Ids", Description = "Item id, comma delimited", IsRequired = true, DataType = "string", ParameterType = "query", Verb = "POST")]
public string Ids { get; set; }
[ApiMember(Name = "Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "POST")]
public string Id { get; set; }
/// <summary>
/// Gets or sets the user id.
/// </summary>
/// <value>The user id.</value>
[ApiMember(Name = "UserId", Description = "User Id", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "POST")]
public Guid UserId { get; set; }
}
[Route("/Playlists/{Id}/Items/{ItemId}/Move/{NewIndex}", "POST", Summary = "Moves a playlist item")]
public class MoveItem : IReturnVoid
{
[ApiMember(Name = "ItemId", Description = "ItemId", IsRequired = true, DataType = "string", ParameterType = "query", Verb = "POST")]
public string ItemId { get; set; }
[ApiMember(Name = "Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "POST")]
public string Id { get; set; }
/// <summary>
/// Gets or sets the user id.
/// </summary>
/// <value>The user id.</value>
[ApiMember(Name = "NewIndex", Description = "NewIndex", IsRequired = true, DataType = "string", ParameterType = "query", Verb = "POST")]
public int NewIndex { get; set; }
}
[Route("/Playlists/{Id}/Items", "DELETE", Summary = "Removes items from a playlist")]
public class RemoveFromPlaylist : IReturnVoid
{
[ApiMember(Name = "Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "DELETE")]
public string Id { get; set; }
[ApiMember(Name = "EntryIds", IsRequired = true, DataType = "string", ParameterType = "query", Verb = "DELETE")]
public string EntryIds { get; set; }
}
[Route("/Playlists/{Id}/Items", "GET", Summary = "Gets the original items of a playlist")]
public class GetPlaylistItems : IReturn<QueryResult<BaseItemDto>>, IHasDtoOptions
{
@ -153,20 +93,6 @@ namespace MediaBrowser.Api
_playlistManager.MoveItem(request.Id, request.ItemId, request.NewIndex);
}
public async Task<object> Post(CreatePlaylist request)
{
var result = await _playlistManager.CreatePlaylist(new PlaylistCreationRequest
{
Name = request.Name,
ItemIdList = GetGuids(request.Ids),
UserId = request.UserId,
MediaType = request.MediaType
}).ConfigureAwait(false);
return ToOptimizedResult(result);
}
public void Post(AddToPlaylist request)
{
_playlistManager.AddToPlaylist(request.Id, GetGuids(request.Ids), request.UserId);

Loading…
Cancel
Save