jellyfin/MediaBrowser.Api/Movies/CollectionService.cs

99 lines
4.0 KiB
C#
Raw Normal View History

using MediaBrowser.Controller.Collections;
2014-06-03 20:34:36 -07:00
using MediaBrowser.Controller.Dto;
2014-07-02 11:34:08 -07:00
using MediaBrowser.Controller.Net;
2014-08-01 19:34:45 -07:00
using MediaBrowser.Model.Collections;
2014-12-26 22:08:39 -07:00
using MediaBrowser.Model.Dto;
2014-06-03 20:34:36 -07:00
using MediaBrowser.Model.Querying;
using ServiceStack;
using System;
2014-06-03 20:34:36 -07:00
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace MediaBrowser.Api.Movies
{
2014-03-20 20:31:40 -07:00
[Route("/Collections", "POST", Summary = "Creates a new collection")]
2014-06-03 20:34:36 -07:00
public class CreateCollection : IReturn<CollectionCreationResult>
{
[ApiMember(Name = "IsLocked", Description = "Whether or not to lock the new collection.", IsRequired = false, DataType = "bool", ParameterType = "query", Verb = "POST")]
public bool IsLocked { get; set; }
[ApiMember(Name = "Name", Description = "The name of the new collection.", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "POST")]
public string Name { get; set; }
[ApiMember(Name = "ParentId", Description = "Optional - create the collection within a specific folder", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "POST")]
public Guid? ParentId { get; set; }
2014-06-03 20:34:36 -07:00
[ApiMember(Name = "Ids", Description = "Item Ids to add to the collection", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "POST", AllowMultiple = true)]
public string Ids { get; set; }
}
2014-03-20 20:31:40 -07:00
[Route("/Collections/{Id}/Items", "POST", Summary = "Adds items to a collection")]
public class AddToCollection : 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 Guid Id { get; set; }
}
2014-03-20 20:31:40 -07:00
[Route("/Collections/{Id}/Items", "DELETE", Summary = "Removes items from a collection")]
public class RemoveFromCollection : 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 = "DELETE")]
public Guid Id { get; set; }
}
2014-07-02 11:34:08 -07:00
[Authenticated]
public class CollectionService : BaseApiService
{
private readonly ICollectionManager _collectionManager;
2014-06-03 20:34:36 -07:00
private readonly IDtoService _dtoService;
2014-06-03 20:34:36 -07:00
public CollectionService(ICollectionManager collectionManager, IDtoService dtoService)
{
_collectionManager = collectionManager;
2014-06-03 20:34:36 -07:00
_dtoService = dtoService;
}
2014-08-16 22:38:13 -07:00
public async Task<object> Post(CreateCollection request)
{
2014-08-16 22:38:13 -07:00
var item = await _collectionManager.CreateCollection(new CollectionCreationOptions
{
IsLocked = request.IsLocked,
Name = request.Name,
2014-06-03 20:34:36 -07:00
ParentId = request.ParentId,
ItemIdList = (request.Ids ?? string.Empty).Split(',').Where(i => !string.IsNullOrWhiteSpace(i)).Select(i => new Guid(i)).ToList()
2014-08-16 22:38:13 -07:00
}).ConfigureAwait(false);
2014-06-03 20:34:36 -07:00
2015-01-24 12:03:55 -07:00
var dtoOptions = GetDtoOptions(request);
2014-12-26 22:08:39 -07:00
var dto = _dtoService.GetBaseItemDto(item, dtoOptions);
2014-06-03 20:34:36 -07:00
return ToOptimizedResult(new CollectionCreationResult
{
Id = dto.Id
});
}
public void Post(AddToCollection request)
{
var task = _collectionManager.AddToCollection(request.Id, request.Ids.Split(',').Select(i => new Guid(i)));
Task.WaitAll(task);
}
public void Delete(RemoveFromCollection request)
{
var task = _collectionManager.RemoveFromCollection(request.Id, request.Ids.Split(',').Select(i => new Guid(i)));
Task.WaitAll(task);
}
}
}