CollectionService.cs 4.0 KB

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