CollectionService.cs 4.2 KB

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