CollectionService.cs 4.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. using MediaBrowser.Controller.Collections;
  2. using MediaBrowser.Controller.Dto;
  3. using MediaBrowser.Controller.Net;
  4. using MediaBrowser.Model.Collections;
  5. using MediaBrowser.Model.Dto;
  6. using MediaBrowser.Model.Querying;
  7. using ServiceStack;
  8. using System;
  9. using System.Collections.Generic;
  10. using System.Linq;
  11. using System.Threading.Tasks;
  12. namespace MediaBrowser.Api.Movies
  13. {
  14. [Route("/Collections", "POST", Summary = "Creates a new collection")]
  15. public class CreateCollection : IReturn<CollectionCreationResult>
  16. {
  17. [ApiMember(Name = "IsLocked", Description = "Whether or not to lock the new collection.", IsRequired = false, DataType = "bool", ParameterType = "query", Verb = "POST")]
  18. public bool IsLocked { get; set; }
  19. [ApiMember(Name = "Name", Description = "The name of the new collection.", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "POST")]
  20. public string Name { get; set; }
  21. [ApiMember(Name = "ParentId", Description = "Optional - create the collection within a specific folder", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "POST")]
  22. public Guid? ParentId { get; set; }
  23. [ApiMember(Name = "Ids", Description = "Item Ids to add to the collection", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "POST", AllowMultiple = true)]
  24. public string Ids { get; set; }
  25. }
  26. [Route("/Collections/{Id}/Items", "POST", Summary = "Adds items to a collection")]
  27. public class AddToCollection : IReturnVoid
  28. {
  29. [ApiMember(Name = "Ids", Description = "Item id, comma delimited", IsRequired = true, DataType = "string", ParameterType = "query", Verb = "POST")]
  30. public string Ids { get; set; }
  31. [ApiMember(Name = "Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "POST")]
  32. public Guid Id { get; set; }
  33. }
  34. [Route("/Collections/{Id}/Items", "DELETE", Summary = "Removes items from a collection")]
  35. public class RemoveFromCollection : IReturnVoid
  36. {
  37. [ApiMember(Name = "Ids", Description = "Item id, comma delimited", IsRequired = true, DataType = "string", ParameterType = "query", Verb = "POST")]
  38. public string Ids { get; set; }
  39. [ApiMember(Name = "Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "DELETE")]
  40. public Guid Id { get; set; }
  41. }
  42. [Authenticated]
  43. public class CollectionService : BaseApiService
  44. {
  45. private readonly ICollectionManager _collectionManager;
  46. private readonly IDtoService _dtoService;
  47. public CollectionService(ICollectionManager collectionManager, IDtoService dtoService)
  48. {
  49. _collectionManager = collectionManager;
  50. _dtoService = dtoService;
  51. }
  52. public async Task<object> Post(CreateCollection request)
  53. {
  54. var item = await _collectionManager.CreateCollection(new CollectionCreationOptions
  55. {
  56. IsLocked = request.IsLocked,
  57. Name = request.Name,
  58. ParentId = request.ParentId,
  59. ItemIdList = (request.Ids ?? string.Empty).Split(',').Where(i => !string.IsNullOrWhiteSpace(i)).Select(i => new Guid(i)).ToList()
  60. }).ConfigureAwait(false);
  61. var dtoOptions = GetDtoOptions(request);
  62. var dto = _dtoService.GetBaseItemDto(item, dtoOptions);
  63. return ToOptimizedResult(new CollectionCreationResult
  64. {
  65. Id = dto.Id
  66. });
  67. }
  68. public void Post(AddToCollection request)
  69. {
  70. var task = _collectionManager.AddToCollection(request.Id, request.Ids.Split(',').Select(i => new Guid(i)));
  71. Task.WaitAll(task);
  72. }
  73. public void Delete(RemoveFromCollection request)
  74. {
  75. var task = _collectionManager.RemoveFromCollection(request.Id, request.Ids.Split(',').Select(i => new Guid(i)));
  76. Task.WaitAll(task);
  77. }
  78. }
  79. }