CollectionService.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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 userId = AuthorizationContext.GetAuthorizationInfo(Request).UserId;
  55. var item = await _collectionManager.CreateCollection(new CollectionCreationOptions
  56. {
  57. IsLocked = request.IsLocked,
  58. Name = request.Name,
  59. ParentId = request.ParentId,
  60. ItemIdList = (request.Ids ?? string.Empty).Split(',').Where(i => !string.IsNullOrWhiteSpace(i)).Select(i => new Guid(i)).ToList(),
  61. UserIds = new List<Guid> { new Guid(userId) }
  62. }).ConfigureAwait(false);
  63. var dtoOptions = GetDtoOptions(request);
  64. var dto = _dtoService.GetBaseItemDto(item, dtoOptions);
  65. return ToOptimizedResult(new CollectionCreationResult
  66. {
  67. Id = dto.Id
  68. });
  69. }
  70. public void Post(AddToCollection request)
  71. {
  72. var task = _collectionManager.AddToCollection(request.Id, request.Ids.Split(',').Select(i => new Guid(i)));
  73. Task.WaitAll(task);
  74. }
  75. public void Delete(RemoveFromCollection request)
  76. {
  77. var task = _collectionManager.RemoveFromCollection(request.Id, request.Ids.Split(',').Select(i => new Guid(i)));
  78. Task.WaitAll(task);
  79. }
  80. }
  81. }