diff --git a/MediaBrowser.Api/UserLibrary/ArtistsService.cs b/MediaBrowser.Api/UserLibrary/ArtistsService.cs index f027fe9df..cde5eade5 100644 --- a/MediaBrowser.Api/UserLibrary/ArtistsService.cs +++ b/MediaBrowser.Api/UserLibrary/ArtistsService.cs @@ -123,7 +123,7 @@ namespace MediaBrowser.Api.UserLibrary /// The request. /// The items. /// IEnumerable{Tuple{System.StringFunc{System.Int32}}}. - protected override IEnumerable GetAllItems(GetItemsByName request, IEnumerable items) + protected override IEnumerable GetAllItems(GetItemsByName request, IEnumerable items) { if (request is GetAlbumArtists) { diff --git a/MediaBrowser.Api/UserLibrary/BaseItemsByNameService.cs b/MediaBrowser.Api/UserLibrary/BaseItemsByNameService.cs index 539b792f4..a28202943 100644 --- a/MediaBrowser.Api/UserLibrary/BaseItemsByNameService.cs +++ b/MediaBrowser.Api/UserLibrary/BaseItemsByNameService.cs @@ -48,6 +48,42 @@ namespace MediaBrowser.Api.UserLibrary DtoService = dtoService; } + protected BaseItem GetParentItem(GetItemsByName request) + { + BaseItem parentItem; + + if (!string.IsNullOrWhiteSpace(request.UserId)) + { + var user = UserManager.GetUserById(request.UserId); + parentItem = string.IsNullOrEmpty(request.ParentId) ? user.RootFolder : LibraryManager.GetItemById(request.ParentId); + } + else + { + parentItem = string.IsNullOrEmpty(request.ParentId) ? LibraryManager.RootFolder : LibraryManager.GetItemById(request.ParentId); + } + + return parentItem; + } + + protected string GetParentItemViewType(GetItemsByName request) + { + var parent = GetParentItem(request); + + var collectionFolder = parent as ICollectionFolder; + if (collectionFolder != null) + { + return collectionFolder.CollectionType; + } + + var view = parent as UserView; + if (view != null) + { + return view.ViewType; + } + + return null; + } + /// /// Gets the specified request. /// @@ -114,13 +150,13 @@ namespace MediaBrowser.Api.UserLibrary var filteredItems = FilterItems(request, extractedItems, user); - filteredItems = FilterByLibraryItems(request, filteredItems, user, libraryItems); + filteredItems = FilterByLibraryItems(request, filteredItems.Cast(), user, libraryItems).Cast(); - filteredItems = LibraryManager.Sort(filteredItems, user, request.GetOrderBy(), request.SortOrder ?? SortOrder.Ascending).Cast(); + filteredItems = LibraryManager.Sort(filteredItems, user, request.GetOrderBy(), request.SortOrder ?? SortOrder.Ascending); var ibnItemsArray = filteredItems.ToList(); - IEnumerable ibnItems = ibnItemsArray; + IEnumerable ibnItems = ibnItemsArray; var result = new ItemsResult { @@ -141,14 +177,14 @@ namespace MediaBrowser.Api.UserLibrary } - IEnumerable>> tuples; + IEnumerable>> tuples; if (dtoOptions.Fields.Contains(ItemFields.ItemCounts)) { - tuples = ibnItems.Select(i => new Tuple>(i, i.GetTaggedItems(libraryItems).ToList())); + tuples = ibnItems.Select(i => new Tuple>(i, ((IItemByName)i).GetTaggedItems(libraryItems).ToList())); } else { - tuples = ibnItems.Select(i => new Tuple>(i, new List())); + tuples = ibnItems.Select(i => new Tuple>(i, new List())); } var dtos = tuples.Select(i => DtoService.GetItemByNameDto(i.Item1, dtoOptions, i.Item2, user)); @@ -180,7 +216,7 @@ namespace MediaBrowser.Api.UserLibrary return options.Fields.Contains(ItemFields.ItemCounts); } - private IEnumerable FilterByLibraryItems(GetItemsByName request, IEnumerable items, User user, IEnumerable libraryItems) + private IEnumerable FilterByLibraryItems(GetItemsByName request, IEnumerable items, User user, IEnumerable libraryItems) { var filters = request.GetFilters().ToList(); @@ -211,7 +247,7 @@ namespace MediaBrowser.Api.UserLibrary /// The items. /// The user. /// IEnumerable{`0}. - private IEnumerable FilterItems(GetItemsByName request, IEnumerable items, User user) + private IEnumerable FilterItems(GetItemsByName request, IEnumerable items, User user) { if (!string.IsNullOrEmpty(request.NameStartsWithOrGreater)) { @@ -375,7 +411,7 @@ namespace MediaBrowser.Api.UserLibrary /// The request. /// The items. /// IEnumerable{Task{`0}}. - protected abstract IEnumerable GetAllItems(GetItemsByName request, IEnumerable items); + protected abstract IEnumerable GetAllItems(GetItemsByName request, IEnumerable items); } /// diff --git a/MediaBrowser.Api/UserLibrary/GameGenresService.cs b/MediaBrowser.Api/UserLibrary/GameGenresService.cs index f9d0f0d0f..a1ad14a4d 100644 --- a/MediaBrowser.Api/UserLibrary/GameGenresService.cs +++ b/MediaBrowser.Api/UserLibrary/GameGenresService.cs @@ -99,14 +99,24 @@ namespace MediaBrowser.Api.UserLibrary /// The request. /// The items. /// IEnumerable{Tuple{System.StringFunc{System.Int32}}}. - protected override IEnumerable GetAllItems(GetItemsByName request, IEnumerable items) + protected override IEnumerable GetAllItems(GetItemsByName request, IEnumerable items) { - var itemsList = items.Where(i => i.Genres != null).ToList(); - - return itemsList + return items .SelectMany(i => i.Genres) .DistinctNames() - .Select(name => LibraryManager.GetGameGenre(name)); + .Select(name => + { + try + { + return LibraryManager.GetGameGenre(name); + } + catch (Exception ex) + { + Logger.ErrorException("Error getting genre {0}", ex, name); + return null; + } + }) + .Where(i => i != null); } } } diff --git a/MediaBrowser.Api/UserLibrary/GenresService.cs b/MediaBrowser.Api/UserLibrary/GenresService.cs index 070246886..9e56907da 100644 --- a/MediaBrowser.Api/UserLibrary/GenresService.cs +++ b/MediaBrowser.Api/UserLibrary/GenresService.cs @@ -5,6 +5,7 @@ using MediaBrowser.Controller.Library; using MediaBrowser.Controller.Net; using MediaBrowser.Controller.Persistence; using MediaBrowser.Model.Dto; +using MediaBrowser.Model.Entities; using MediaBrowser.Model.Querying; using ServiceStack; using System; @@ -104,8 +105,38 @@ namespace MediaBrowser.Api.UserLibrary /// The request. /// The items. /// IEnumerable{Tuple{System.StringFunc{System.Int32}}}. - protected override IEnumerable GetAllItems(GetItemsByName request, IEnumerable items) + protected override IEnumerable GetAllItems(GetItemsByName request, IEnumerable items) { + var viewType = GetParentItemViewType(request); + + if (string.Equals(viewType, CollectionType.Music) || string.Equals(viewType, CollectionType.MusicVideos)) + { + return items + .SelectMany(i => i.Genres) + .DistinctNames() + .Select(name => LibraryManager.GetMusicGenre(name)); + } + + if (string.Equals(viewType, CollectionType.Games)) + { + return items + .SelectMany(i => i.Genres) + .DistinctNames() + .Select(name => + { + try + { + return LibraryManager.GetGameGenre(name); + } + catch (Exception ex) + { + Logger.ErrorException("Error getting genre {0}", ex, name); + return null; + } + }) + .Where(i => i != null); + } + return items .SelectMany(i => i.Genres) .DistinctNames() diff --git a/MediaBrowser.Api/UserLibrary/MusicGenresService.cs b/MediaBrowser.Api/UserLibrary/MusicGenresService.cs index e63d6c0f4..182691584 100644 --- a/MediaBrowser.Api/UserLibrary/MusicGenresService.cs +++ b/MediaBrowser.Api/UserLibrary/MusicGenresService.cs @@ -99,11 +99,9 @@ namespace MediaBrowser.Api.UserLibrary /// The request. /// The items. /// IEnumerable{Tuple{System.StringFunc{System.Int32}}}. - protected override IEnumerable GetAllItems(GetItemsByName request, IEnumerable items) + protected override IEnumerable GetAllItems(GetItemsByName request, IEnumerable items) { - var itemsList = items.ToList(); - - return itemsList + return items .SelectMany(i => i.Genres) .DistinctNames() .Select(name => LibraryManager.GetMusicGenre(name)); diff --git a/MediaBrowser.Api/UserLibrary/PersonsService.cs b/MediaBrowser.Api/UserLibrary/PersonsService.cs index fc1431f2e..e6a60fcc6 100644 --- a/MediaBrowser.Api/UserLibrary/PersonsService.cs +++ b/MediaBrowser.Api/UserLibrary/PersonsService.cs @@ -108,7 +108,7 @@ namespace MediaBrowser.Api.UserLibrary /// The request. /// The items. /// IEnumerable{Tuple{System.StringFunc{System.Int32}}}. - protected override IEnumerable GetAllItems(GetItemsByName request, IEnumerable items) + protected override IEnumerable GetAllItems(GetItemsByName request, IEnumerable items) { var inputPersonTypes = ((GetPersons)request).PersonTypes; var personTypes = string.IsNullOrEmpty(inputPersonTypes) ? new string[] { } : inputPersonTypes.Split(','); diff --git a/MediaBrowser.Api/UserLibrary/StudiosService.cs b/MediaBrowser.Api/UserLibrary/StudiosService.cs index d6a7db14f..7cf8d752a 100644 --- a/MediaBrowser.Api/UserLibrary/StudiosService.cs +++ b/MediaBrowser.Api/UserLibrary/StudiosService.cs @@ -103,7 +103,7 @@ namespace MediaBrowser.Api.UserLibrary /// The request. /// The items. /// IEnumerable{Tuple{System.StringFunc{System.Int32}}}. - protected override IEnumerable GetAllItems(GetItemsByName request, IEnumerable items) + protected override IEnumerable GetAllItems(GetItemsByName request, IEnumerable items) { var itemsList = items.Where(i => i.Studios != null).ToList(); diff --git a/MediaBrowser.Api/UserLibrary/YearsService.cs b/MediaBrowser.Api/UserLibrary/YearsService.cs index 859b9f959..4b3fe6c8e 100644 --- a/MediaBrowser.Api/UserLibrary/YearsService.cs +++ b/MediaBrowser.Api/UserLibrary/YearsService.cs @@ -103,7 +103,7 @@ namespace MediaBrowser.Api.UserLibrary /// The request. /// The items. /// IEnumerable{Tuple{System.StringFunc{System.Int32}}}. - protected override IEnumerable GetAllItems(GetItemsByName request, IEnumerable items) + protected override IEnumerable GetAllItems(GetItemsByName request, IEnumerable items) { var itemsList = items.Where(i => i.ProductionYear != null).ToList(); diff --git a/MediaBrowser.Controller/Dto/IDtoService.cs b/MediaBrowser.Controller/Dto/IDtoService.cs index 5ec8f274b..e4ab29102 100644 --- a/MediaBrowser.Controller/Dto/IDtoService.cs +++ b/MediaBrowser.Controller/Dto/IDtoService.cs @@ -81,13 +81,11 @@ namespace MediaBrowser.Controller.Dto /// /// Gets the item by name dto. /// - /// /// The item. /// The options. /// The tagged items. /// The user. /// BaseItemDto. - BaseItemDto GetItemByNameDto(T item, DtoOptions options, List taggedItems, User user = null) - where T : BaseItem, IItemByName; + BaseItemDto GetItemByNameDto(BaseItem item, DtoOptions options, List taggedItems, User user = null); } } diff --git a/MediaBrowser.Server.Implementations/Dto/DtoService.cs b/MediaBrowser.Server.Implementations/Dto/DtoService.cs index 1dbdf90a7..5c9f72b7d 100644 --- a/MediaBrowser.Server.Implementations/Dto/DtoService.cs +++ b/MediaBrowser.Server.Implementations/Dto/DtoService.cs @@ -397,8 +397,7 @@ namespace MediaBrowser.Server.Implementations.Dto return dto; } - public BaseItemDto GetItemByNameDto(T item, DtoOptions options, List taggedItems, User user = null) - where T : BaseItem, IItemByName + public BaseItemDto GetItemByNameDto(BaseItem item, DtoOptions options, List taggedItems, User user = null) { var syncProgress = GetSyncedItemProgress(options);