ArtistsService.cs 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. using MediaBrowser.Controller.Dto;
  2. using MediaBrowser.Controller.Entities;
  3. using MediaBrowser.Controller.Entities.Audio;
  4. using MediaBrowser.Controller.Library;
  5. using MediaBrowser.Controller.Persistence;
  6. using MediaBrowser.Model.Dto;
  7. using MediaBrowser.Model.Querying;
  8. using ServiceStack.ServiceHost;
  9. using System;
  10. using System.Collections.Generic;
  11. using System.Linq;
  12. using System.Threading.Tasks;
  13. namespace MediaBrowser.Api.UserLibrary
  14. {
  15. /// <summary>
  16. /// Class GetArtists
  17. /// </summary>
  18. [Route("/Artists", "GET")]
  19. [Api(Description = "Gets all artists from a given item, folder, or the entire library")]
  20. public class GetArtists : GetItemsByName
  21. {
  22. /// <summary>
  23. /// Filter by artists that are on tour, or not
  24. /// </summary>
  25. /// <value><c>null</c> if [is on tour] contains no value, <c>true</c> if [is on tour]; otherwise, <c>false</c>.</value>
  26. public bool? IsOnTour { get; set; }
  27. }
  28. /// <summary>
  29. /// Class GetArtistsItemCounts
  30. /// </summary>
  31. [Route("/Artists/{Name}/Counts", "GET")]
  32. [Api(Description = "Gets item counts of library items that an artist appears in")]
  33. public class GetArtistsItemCounts : IReturn<ItemByNameCounts>
  34. {
  35. /// <summary>
  36. /// Gets or sets the user id.
  37. /// </summary>
  38. /// <value>The user id.</value>
  39. [ApiMember(Name = "UserId", Description = "User Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "GET")]
  40. public Guid UserId { get; set; }
  41. /// <summary>
  42. /// Gets or sets the name.
  43. /// </summary>
  44. /// <value>The name.</value>
  45. [ApiMember(Name = "Name", Description = "The artist name", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "GET")]
  46. public string Name { get; set; }
  47. }
  48. [Route("/Artists/{Name}", "GET")]
  49. [Api(Description = "Gets an artist, by name")]
  50. public class GetArtist : IReturn<BaseItemDto>
  51. {
  52. /// <summary>
  53. /// Gets or sets the name.
  54. /// </summary>
  55. /// <value>The name.</value>
  56. [ApiMember(Name = "Name", Description = "The artist name", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "GET")]
  57. public string Name { get; set; }
  58. /// <summary>
  59. /// Gets or sets the user id.
  60. /// </summary>
  61. /// <value>The user id.</value>
  62. [ApiMember(Name = "UserId", Description = "Optional. Filter by user id, and attach user data", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "GET")]
  63. public Guid? UserId { get; set; }
  64. }
  65. /// <summary>
  66. /// Class ArtistsService
  67. /// </summary>
  68. public class ArtistsService : BaseItemsByNameService<Artist>
  69. {
  70. /// <summary>
  71. /// Initializes a new instance of the <see cref="ArtistsService"/> class.
  72. /// </summary>
  73. /// <param name="userManager">The user manager.</param>
  74. /// <param name="libraryManager">The library manager.</param>
  75. /// <param name="userDataRepository">The user data repository.</param>
  76. public ArtistsService(IUserManager userManager, ILibraryManager libraryManager, IUserDataRepository userDataRepository)
  77. : base(userManager, libraryManager, userDataRepository)
  78. {
  79. }
  80. /// <summary>
  81. /// Gets the specified request.
  82. /// </summary>
  83. /// <param name="request">The request.</param>
  84. /// <returns>System.Object.</returns>
  85. public object Get(GetArtist request)
  86. {
  87. var result = GetItem(request).Result;
  88. return ToOptimizedResult(result);
  89. }
  90. /// <summary>
  91. /// Gets the item.
  92. /// </summary>
  93. /// <param name="request">The request.</param>
  94. /// <returns>Task{BaseItemDto}.</returns>
  95. private async Task<BaseItemDto> GetItem(GetArtist request)
  96. {
  97. var item = await GetArtist(request.Name, LibraryManager).ConfigureAwait(false);
  98. // Get everything
  99. var fields = Enum.GetNames(typeof(ItemFields)).Select(i => (ItemFields)Enum.Parse(typeof(ItemFields), i, true));
  100. var builder = new DtoBuilder(Logger, LibraryManager, UserDataRepository);
  101. if (request.UserId.HasValue)
  102. {
  103. var user = UserManager.GetUserById(request.UserId.Value);
  104. return await builder.GetBaseItemDto(item, fields.ToList(), user).ConfigureAwait(false);
  105. }
  106. return await builder.GetBaseItemDto(item, fields.ToList()).ConfigureAwait(false);
  107. }
  108. /// <summary>
  109. /// Gets the specified request.
  110. /// </summary>
  111. /// <param name="request">The request.</param>
  112. /// <returns>System.Object.</returns>
  113. public object Get(GetArtistsItemCounts request)
  114. {
  115. var name = DeSlugArtistName(request.Name, LibraryManager);
  116. var items = GetItems(request.UserId).OfType<Audio>().Where(i => i.HasArtist(name)).ToList();
  117. var counts = new ItemByNameCounts
  118. {
  119. TotalCount = items.Count,
  120. SongCount = items.Count(),
  121. AlbumCount = items.Select(i => i.Parent).OfType<MusicAlbum>().Distinct().Count()
  122. };
  123. return ToOptimizedResult(counts);
  124. }
  125. /// <summary>
  126. /// Gets the specified request.
  127. /// </summary>
  128. /// <param name="request">The request.</param>
  129. /// <returns>System.Object.</returns>
  130. public object Get(GetArtists request)
  131. {
  132. var result = GetResult(request).Result;
  133. return ToOptimizedResult(result);
  134. }
  135. /// <summary>
  136. /// Filters the items.
  137. /// </summary>
  138. /// <param name="request">The request.</param>
  139. /// <param name="items">The items.</param>
  140. /// <returns>IEnumerable{BaseItem}.</returns>
  141. protected override IEnumerable<BaseItem> FilterItems(GetItemsByName request, IEnumerable<BaseItem> items)
  142. {
  143. items = base.FilterItems(request, items);
  144. var getArtists = (GetArtists) request;
  145. if (getArtists.IsOnTour.HasValue)
  146. {
  147. items = items.OfType<Artist>().Where(i => i.IsOnTour == getArtists.IsOnTour.Value);
  148. }
  149. return items;
  150. }
  151. /// <summary>
  152. /// Gets all items.
  153. /// </summary>
  154. /// <param name="request">The request.</param>
  155. /// <param name="items">The items.</param>
  156. /// <returns>IEnumerable{Tuple{System.StringFunc{System.Int32}}}.</returns>
  157. protected override IEnumerable<IbnStub<Artist>> GetAllItems(GetItemsByName request, IEnumerable<BaseItem> items)
  158. {
  159. var itemsList = items.OfType<Audio>().ToList();
  160. return itemsList
  161. .SelectMany(i =>
  162. {
  163. var list = new List<string>();
  164. if (!string.IsNullOrEmpty(i.AlbumArtist))
  165. {
  166. list.Add(i.AlbumArtist);
  167. }
  168. if (!string.IsNullOrEmpty(i.Artist))
  169. {
  170. list.Add(i.Artist);
  171. }
  172. return list;
  173. })
  174. .Distinct(StringComparer.OrdinalIgnoreCase)
  175. .Select(name => new IbnStub<Artist>(name, () => itemsList.Where(i => i.HasArtist(name)), GetEntity));
  176. }
  177. /// <summary>
  178. /// Gets the entity.
  179. /// </summary>
  180. /// <param name="name">The name.</param>
  181. /// <returns>Task{Artist}.</returns>
  182. protected Task<Artist> GetEntity(string name)
  183. {
  184. return LibraryManager.GetArtist(name);
  185. }
  186. }
  187. }