2014-03-07 16:53:23 +01:00
using MediaBrowser.Controller.Collections ;
2014-06-04 05:34:36 +02:00
using MediaBrowser.Controller.Dto ;
2014-07-02 20:34:08 +02:00
using MediaBrowser.Controller.Net ;
2014-08-02 04:34:45 +02:00
using MediaBrowser.Model.Collections ;
2014-03-07 16:53:23 +01:00
using System ;
2014-06-04 05:34:36 +02:00
using System.Collections.Generic ;
2014-03-07 16:53:23 +01:00
using System.Linq ;
using System.Threading.Tasks ;
2016-10-25 21:02:04 +02:00
using MediaBrowser.Model.Services ;
2014-03-07 16:53:23 +01:00
namespace MediaBrowser.Api.Movies
{
2014-03-21 04:31:40 +01:00
[Route("/Collections", "POST", Summary = "Creates a new collection")]
2014-06-04 05:34:36 +02:00
public class CreateCollection : IReturn < CollectionCreationResult >
2014-03-07 16:53:23 +01:00
{
[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")]
2015-05-30 01:51:33 +02:00
public string ParentId { get ; set ; }
2014-06-04 05:34:36 +02: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-07 16:53:23 +01:00
}
2014-03-21 04:31:40 +01:00
[Route("/Collections/{Id}/Items", "POST", Summary = "Adds items to a collection")]
2014-03-07 16:53:23 +01:00
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")]
2015-05-30 01:51:33 +02:00
public string Id { get ; set ; }
2014-03-07 16:53:23 +01:00
}
2014-03-21 04:31:40 +01:00
[Route("/Collections/{Id}/Items", "DELETE", Summary = "Removes items from a collection")]
2014-03-07 16:53:23 +01:00
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")]
2015-05-30 01:51:33 +02:00
public string Id { get ; set ; }
2014-03-07 16:53:23 +01:00
}
2014-07-02 20:34:08 +02:00
[Authenticated]
2014-03-07 16:53:23 +01:00
public class CollectionService : BaseApiService
{
private readonly ICollectionManager _collectionManager ;
2014-06-04 05:34:36 +02:00
private readonly IDtoService _dtoService ;
2016-11-10 15:41:24 +01:00
private readonly IAuthorizationContext _authContext ;
2014-03-07 16:53:23 +01:00
2016-11-10 15:41:24 +01:00
public CollectionService ( ICollectionManager collectionManager , IDtoService dtoService , IAuthorizationContext authContext )
2014-03-07 16:53:23 +01:00
{
_collectionManager = collectionManager ;
2014-06-04 05:34:36 +02:00
_dtoService = dtoService ;
2016-11-10 15:41:24 +01:00
_authContext = authContext ;
2014-03-07 16:53:23 +01:00
}
2014-08-17 07:38:13 +02:00
public async Task < object > Post ( CreateCollection request )
2014-03-07 16:53:23 +01:00
{
2016-11-10 15:41:24 +01:00
var userId = _authContext . GetAuthorizationInfo ( Request ) . UserId ;
2015-04-26 06:39:40 +02:00
2015-05-30 01:51:33 +02:00
var parentId = string . IsNullOrWhiteSpace ( request . ParentId ) ? ( Guid ? ) null : new Guid ( request . ParentId ) ;
2014-08-17 07:38:13 +02:00
var item = await _collectionManager . CreateCollection ( new CollectionCreationOptions
2014-03-07 16:53:23 +01:00
{
IsLocked = request . IsLocked ,
Name = request . Name ,
2015-05-30 01:51:33 +02:00
ParentId = parentId ,
2015-04-26 06:39:40 +02:00
ItemIdList = ( request . Ids ? ? string . Empty ) . Split ( ',' ) . Where ( i = > ! string . IsNullOrWhiteSpace ( i ) ) . Select ( i = > new Guid ( i ) ) . ToList ( ) ,
UserIds = new List < Guid > { new Guid ( userId ) }
2014-03-07 16:53:23 +01:00
2014-08-17 07:38:13 +02:00
} ) . ConfigureAwait ( false ) ;
2014-06-04 05:34:36 +02:00
2016-11-10 15:41:24 +01:00
var dtoOptions = GetDtoOptions ( _authContext , request ) ;
2014-12-27 06:08:39 +01:00
var dto = _dtoService . GetBaseItemDto ( item , dtoOptions ) ;
2014-06-04 05:34:36 +02:00
return ToOptimizedResult ( new CollectionCreationResult
{
Id = dto . Id
} ) ;
2014-03-07 16:53:23 +01:00
}
public void Post ( AddToCollection request )
{
2015-05-30 01:51:33 +02:00
var task = _collectionManager . AddToCollection ( new Guid ( request . Id ) , request . Ids . Split ( ',' ) . Select ( i = > new Guid ( i ) ) ) ;
2014-03-07 16:53:23 +01:00
Task . WaitAll ( task ) ;
}
public void Delete ( RemoveFromCollection request )
{
2015-05-30 01:51:33 +02:00
var task = _collectionManager . RemoveFromCollection ( new Guid ( request . Id ) , request . Ids . Split ( ',' ) . Select ( i = > new Guid ( i ) ) ) ;
2014-03-07 16:53:23 +01:00
Task . WaitAll ( task ) ;
}
}
}