diff --git a/MediaBrowser.Api/UserLibrary/BaseItemsByNameService.cs b/MediaBrowser.Api/UserLibrary/BaseItemsByNameService.cs index 4325948420..0a2d6453af 100644 --- a/MediaBrowser.Api/UserLibrary/BaseItemsByNameService.cs +++ b/MediaBrowser.Api/UserLibrary/BaseItemsByNameService.cs @@ -70,9 +70,13 @@ namespace MediaBrowser.Api.UserLibrary items = FilterItems(request, items, user); var extractedItems = GetAllItems(request, items, user); - var ibnItemsArray = SortItems(request, extractedItems).ToArray(); - - IEnumerable>>> ibnItems = ibnItemsArray; + + extractedItems = FilterItems(request, extractedItems, user); + extractedItems = SortItems(request, extractedItems); + + var ibnItemsArray = extractedItems.ToArray(); + + IEnumerable> ibnItems = ibnItemsArray; var result = new ItemsResult { @@ -104,23 +108,74 @@ namespace MediaBrowser.Api.UserLibrary return result; } + /// + /// Filters the items. + /// + /// The request. + /// The items. + /// The user. + /// IEnumerable{IbnStub}. + private IEnumerable> FilterItems(GetItemsByName request, IEnumerable> items, User user) + { + var filters = request.GetFilters().ToList(); + + if (filters.Count == 0) + { + return items; + } + + items = items.AsParallel(); + + if (filters.Contains(ItemFilter.Dislikes)) + { + items = items.Where(i => + { + var userdata = i.GetUserItemData(UserDataRepository, user.Id).Result; + + return userdata != null && userdata.Likes.HasValue && !userdata.Likes.Value; + }); + } + + if (filters.Contains(ItemFilter.Likes)) + { + items = items.Where(i => + { + var userdata = i.GetUserItemData(UserDataRepository, user.Id).Result; + + return userdata != null && userdata.Likes.HasValue && userdata.Likes.Value; + }); + } + + if (filters.Contains(ItemFilter.IsFavorite)) + { + items = items.Where(i => + { + var userdata = i.GetUserItemData(UserDataRepository, user.Id).Result; + + return userdata != null && userdata.Likes.HasValue && userdata.IsFavorite; + }); + } + + return items.AsEnumerable(); + } + /// /// Sorts the items. /// /// The request. /// The items. /// IEnumerable{BaseItem}. - private IEnumerable>>> SortItems(GetItemsByName request, IEnumerable>>> items) + private IEnumerable> SortItems(GetItemsByName request, IEnumerable> items) { if (string.Equals(request.SortBy, "SortName", StringComparison.OrdinalIgnoreCase)) { if (request.SortOrder.HasValue && request.SortOrder.Value == Model.Entities.SortOrder.Descending) { - items = items.OrderByDescending(i => i.Item1); + items = items.OrderByDescending(i => i.Name); } else { - items = items.OrderBy(i => i.Item1); + items = items.OrderBy(i => i.Name); } } @@ -160,14 +215,7 @@ namespace MediaBrowser.Api.UserLibrary /// The items. /// The user. /// IEnumerable{Tuple{System.StringFunc{System.Int32}}}. - protected abstract IEnumerable>>> GetAllItems(GetItemsByName request, IEnumerable items, User user); - - /// - /// Gets the entity. - /// - /// The name. - /// Task{BaseItem}. - protected abstract Task GetEntity(string name); + protected abstract IEnumerable> GetAllItems(GetItemsByName request, IEnumerable items, User user); /// /// Gets the dto. @@ -176,17 +224,17 @@ namespace MediaBrowser.Api.UserLibrary /// The user. /// The fields. /// Task{DtoBaseItem}. - private async Task GetDto(Tuple>> stub, User user, List fields) + private async Task GetDto(IbnStub stub, User user, List fields) { BaseItem item; try { - item = await GetEntity(stub.Item1).ConfigureAwait(false); + item = await stub.GetItem().ConfigureAwait(false); } catch (IOException ex) { - Logger.ErrorException("Error getting IBN item {0}", ex, stub.Item1); + Logger.ErrorException("Error getting IBN item {0}", ex, stub.Name); return null; } @@ -194,7 +242,7 @@ namespace MediaBrowser.Api.UserLibrary if (fields.Contains(ItemFields.ItemCounts)) { - var items = stub.Item2().ToList(); + var items = stub.Items; dto.ChildCount = items.Count; dto.RecentlyAddedItemCount = items.Count(i => i.IsRecentlyAdded(user)); @@ -216,4 +264,48 @@ namespace MediaBrowser.Api.UserLibrary [ApiMember(Name = "SortBy", Description = "Optional. Options: SortName", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "GET", AllowMultiple = true)] public string SortBy { get; set; } } + + public class IbnStub + where T : BaseItem + { + private readonly Func> _childItemsFunction; + private List _childItems; + + private readonly Func> _itemFunction; + private Task _itemTask; + + public string Name; + + public BaseItem Item; + private Task _userData; + + public List Items + { + get { return _childItems ?? (_childItems = _childItemsFunction().ToList()); } + } + + public Task GetItem() + { + return _itemTask ?? (_itemTask = _itemFunction(Name)); + } + + public async Task GetUserItemData(IUserDataRepository repo, Guid userId) + { + var item = await GetItem().ConfigureAwait(false); + + if (_userData == null) + { + _userData = repo.GetUserData(userId, item.GetUserDataKey()); + } + + return await _userData.ConfigureAwait(false); + } + + public IbnStub(string name, Func> childItems, Func> item) + { + Name = name; + _childItemsFunction = childItems; + _itemFunction = item; + } + } } diff --git a/MediaBrowser.Api/UserLibrary/GenresService.cs b/MediaBrowser.Api/UserLibrary/GenresService.cs index e275b6ed03..87d996d504 100644 --- a/MediaBrowser.Api/UserLibrary/GenresService.cs +++ b/MediaBrowser.Api/UserLibrary/GenresService.cs @@ -48,14 +48,14 @@ namespace MediaBrowser.Api.UserLibrary /// The items. /// The user. /// IEnumerable{Tuple{System.StringFunc{System.Int32}}}. - protected override IEnumerable>>> GetAllItems(GetItemsByName request, IEnumerable items, User user) + protected override IEnumerable> GetAllItems(GetItemsByName request, IEnumerable items, User user) { var itemsList = items.Where(i => i.Genres != null).ToList(); return itemsList .SelectMany(i => i.Genres) .Distinct(StringComparer.OrdinalIgnoreCase) - .Select(name => new Tuple>>(name, () => itemsList.Where(i => i.Genres.Contains(name, StringComparer.OrdinalIgnoreCase)))); + .Select(name => new IbnStub(name, () => itemsList.Where(i => i.Genres.Contains(name, StringComparer.OrdinalIgnoreCase)), GetEntity)); } /// @@ -63,7 +63,7 @@ namespace MediaBrowser.Api.UserLibrary /// /// The name. /// Task{Genre}. - protected override Task GetEntity(string name) + protected Task GetEntity(string name) { return LibraryManager.GetGenre(name); } diff --git a/MediaBrowser.Api/UserLibrary/PersonsService.cs b/MediaBrowser.Api/UserLibrary/PersonsService.cs index 974b8c0027..698530eb5b 100644 --- a/MediaBrowser.Api/UserLibrary/PersonsService.cs +++ b/MediaBrowser.Api/UserLibrary/PersonsService.cs @@ -53,7 +53,7 @@ namespace MediaBrowser.Api.UserLibrary /// The items. /// The user. /// IEnumerable{Tuple{System.StringFunc{System.Int32}}}. - protected override IEnumerable>>> GetAllItems(GetItemsByName request, IEnumerable items, User user) + protected override IEnumerable> GetAllItems(GetItemsByName request, IEnumerable items, User user) { var inputPersonTypes = ((GetPersons) request).PersonTypes; var personTypes = string.IsNullOrEmpty(inputPersonTypes) ? new string[] { } : inputPersonTypes.Split(','); @@ -67,7 +67,7 @@ namespace MediaBrowser.Api.UserLibrary .Select(i => i.Name) .Distinct(StringComparer.OrdinalIgnoreCase) - .Select(name => new Tuple>>(name, () => + .Select(name => new IbnStub(name, () => { if (personTypes.Length == 0) { @@ -75,7 +75,7 @@ namespace MediaBrowser.Api.UserLibrary } return itemsList.Where(i => i.People.Any(p => p.Name.Equals(name, StringComparison.OrdinalIgnoreCase) && personTypes.Contains(p.Type ?? string.Empty, StringComparer.OrdinalIgnoreCase))); - }) + }, GetEntity) ); } @@ -102,7 +102,7 @@ namespace MediaBrowser.Api.UserLibrary /// /// The name. /// Task{Genre}. - protected override Task GetEntity(string name) + protected Task GetEntity(string name) { return LibraryManager.GetPerson(name); } diff --git a/MediaBrowser.Api/UserLibrary/StudiosService.cs b/MediaBrowser.Api/UserLibrary/StudiosService.cs index 77f20d8e85..e2c1c4743f 100644 --- a/MediaBrowser.Api/UserLibrary/StudiosService.cs +++ b/MediaBrowser.Api/UserLibrary/StudiosService.cs @@ -48,14 +48,14 @@ namespace MediaBrowser.Api.UserLibrary /// The items. /// The user. /// IEnumerable{Tuple{System.StringFunc{System.Int32}}}. - protected override IEnumerable>>> GetAllItems(GetItemsByName request, IEnumerable items, User user) + protected override IEnumerable> GetAllItems(GetItemsByName request, IEnumerable items, User user) { var itemsList = items.Where(i => i.Studios != null).ToList(); return itemsList .SelectMany(i => i.Studios) .Distinct(StringComparer.OrdinalIgnoreCase) - .Select(name => new Tuple>>(name, () => itemsList.Where(i => i.Studios.Contains(name, StringComparer.OrdinalIgnoreCase)))); + .Select(name => new IbnStub(name, () => itemsList.Where(i => i.Studios.Contains(name, StringComparer.OrdinalIgnoreCase)), GetEntity)); } /// @@ -63,7 +63,7 @@ namespace MediaBrowser.Api.UserLibrary /// /// The name. /// Task{Studio}. - protected override Task GetEntity(string name) + protected Task GetEntity(string name) { return LibraryManager.GetStudio(name); } diff --git a/MediaBrowser.Api/UserLibrary/YearsService.cs b/MediaBrowser.Api/UserLibrary/YearsService.cs index 481645c242..c7cd4ff242 100644 --- a/MediaBrowser.Api/UserLibrary/YearsService.cs +++ b/MediaBrowser.Api/UserLibrary/YearsService.cs @@ -54,14 +54,14 @@ namespace MediaBrowser.Api.UserLibrary /// The items. /// The user. /// IEnumerable{Tuple{System.StringFunc{System.Int32}}}. - protected override IEnumerable>>> GetAllItems(GetItemsByName request, IEnumerable items, User user) + protected override IEnumerable> GetAllItems(GetItemsByName request, IEnumerable items, User user) { var itemsList = items.Where(i => i.ProductionYear != null).ToList(); return itemsList .Select(i => i.ProductionYear.Value) .Distinct() - .Select(year => new Tuple>>(year.ToString(UsCulture), () => itemsList.Where(i => i.ProductionYear.HasValue && i.ProductionYear.Value == year))); + .Select(year => new IbnStub(year.ToString(UsCulture), () => itemsList.Where(i => i.ProductionYear.HasValue && i.ProductionYear.Value == year), GetEntity)); } /// @@ -69,7 +69,7 @@ namespace MediaBrowser.Api.UserLibrary /// /// The name. /// Task{Studio}. - protected override Task GetEntity(string name) + protected Task GetEntity(string name) { return LibraryManager.GetYear(int.Parse(name, UsCulture)); } diff --git a/MediaBrowser.Controller/Entities/BaseItem.cs b/MediaBrowser.Controller/Entities/BaseItem.cs index ccaabd4382..d8a0db69a6 100644 --- a/MediaBrowser.Controller/Entities/BaseItem.cs +++ b/MediaBrowser.Controller/Entities/BaseItem.cs @@ -1,5 +1,4 @@ -using MediaBrowser.Common.Extensions; -using MediaBrowser.Controller.Configuration; +using MediaBrowser.Controller.Configuration; using MediaBrowser.Controller.IO; using MediaBrowser.Controller.Library; using MediaBrowser.Controller.Localization;