diff --git a/Source/DataLayer/QueryObjects/BookQueries.cs b/Source/DataLayer/QueryObjects/BookQueries.cs index e54bb42f..efcbdc36 100644 --- a/Source/DataLayer/QueryObjects/BookQueries.cs +++ b/Source/DataLayer/QueryObjects/BookQueries.cs @@ -35,5 +35,15 @@ namespace DataLayer .Include(b => b.SeriesLink).ThenInclude(sb => sb.Series) .Include(b => b.ContributorsLink).ThenInclude(c => c.Contributor) .Include(b => b.Category).ThenInclude(c => c.ParentCategory); + + public static bool IsProduct(this Book book) + => book.ContentType is not ContentType.Episode and not ContentType.Parent; + + public static bool IsEpisodeChild(this Book book) + => book.ContentType is ContentType.Episode; + + public static bool IsEpisodeParent(this Book book) + => book.ContentType is ContentType.Parent; + } } diff --git a/Source/DataLayer/QueryObjects/LibraryBookQueries.cs b/Source/DataLayer/QueryObjects/LibraryBookQueries.cs index 969aa702..810488cc 100644 --- a/Source/DataLayer/QueryObjects/LibraryBookQueries.cs +++ b/Source/DataLayer/QueryObjects/LibraryBookQueries.cs @@ -41,5 +41,23 @@ namespace DataLayer .Include(le => le.Book).ThenInclude(b => b.SeriesLink).ThenInclude(sb => sb.Series) .Include(le => le.Book).ThenInclude(b => b.ContributorsLink).ThenInclude(c => c.Contributor) .Include(le => le.Book).ThenInclude(b => b.Category).ThenInclude(c => c.ParentCategory); + +#nullable enable + public static LibraryBook? FindSeriesParent(this IEnumerable libraryBooks, LibraryBook seriesEpisode) + { + if (seriesEpisode.Book.SeriesLink is null) return null; + + //Parent books will always have exactly 1 SeriesBook due to how + //they are imported in ApiExtended.getChildEpisodesAsync() + return libraryBooks.FirstOrDefault( + lb => + lb.Book.IsEpisodeParent() && + seriesEpisode.Book.SeriesLink.Any( + s => s.Series.AudibleSeriesId == lb.Book.SeriesLink.Single().Series.AudibleSeriesId)); + } +#nullable disable + + public static IEnumerable FindChildren(this IEnumerable bookList, LibraryBook parent) + => bookList.Where(lb => lb.Book.SeriesLink?.Any(s => s.Series.AudibleSeriesId == parent.Book.AudibleProductId) == true).ToList(); } } diff --git a/Source/LibationWinForms/GridView/ProductsGrid.cs b/Source/LibationWinForms/GridView/ProductsGrid.cs index ff058f92..c13a2d12 100644 --- a/Source/LibationWinForms/GridView/ProductsGrid.cs +++ b/Source/LibationWinForms/GridView/ProductsGrid.cs @@ -91,13 +91,13 @@ namespace LibationWinForms.GridView internal void BindToGrid(List dbBooks) { - var geList = dbBooks.Where(lb => lb.IsProduct()).Select(b => new LibraryBookEntry(b)).Cast().ToList(); + var geList = dbBooks.Where(lb => lb.Book.IsProduct()).Select(b => new LibraryBookEntry(b)).Cast().ToList(); - var episodes = dbBooks.Where(lb => lb.IsEpisodeChild()); + var episodes = dbBooks.Where(lb => lb.Book.IsEpisodeChild()); - foreach (var parent in dbBooks.Where(lb => lb.IsEpisodeParent())) + foreach (var parent in dbBooks.Where(lb => lb.Book.IsEpisodeParent())) { - var seriesEpisodes = episodes.FindChildren(parent).ToList(); + var seriesEpisodes = episodes.FindChildren(parent); if (!seriesEpisodes.Any()) continue; @@ -132,9 +132,9 @@ namespace LibationWinForms.GridView { var existingEntry = allEntries.FindByAsin(libraryBook.Book.AudibleProductId); - if (libraryBook.IsProduct()) + if (libraryBook.Book.IsProduct()) AddOrUpdateBook(libraryBook, existingEntry); - else if(libraryBook.IsEpisodeChild()) + else if(libraryBook.Book.IsEpisodeChild()) AddOrUpdateEpisode(libraryBook, existingEntry, seriesEntries, dbBooks); } diff --git a/Source/LibationWinForms/GridView/QueryExtensions.cs b/Source/LibationWinForms/GridView/QueryExtensions.cs index c6257014..d084aec7 100644 --- a/Source/LibationWinForms/GridView/QueryExtensions.cs +++ b/Source/LibationWinForms/GridView/QueryExtensions.cs @@ -8,9 +8,6 @@ namespace LibationWinForms.GridView #nullable enable internal static class QueryExtensions { - public static IEnumerable FindChildren(this IEnumerable bookList, LibraryBook parent) - => bookList.Where(lb => lb.Book.SeriesLink?.Any(s => s.Series.AudibleSeriesId == parent.Book.AudibleProductId) == true); - public static IEnumerable BookEntries(this IEnumerable gridEntries) => gridEntries.OfType(); @@ -23,15 +20,6 @@ namespace LibationWinForms.GridView public static IEnumerable EmptySeries(this IEnumerable gridEntries) => gridEntries.SeriesEntries().Where(i => i.Children.Count == 0); - public static bool IsProduct(this LibraryBook lb) - => lb.Book.ContentType is not ContentType.Episode and not ContentType.Parent; - - public static bool IsEpisodeChild(this LibraryBook lb) - => lb.Book.ContentType is ContentType.Episode; - - public static bool IsEpisodeParent(this LibraryBook lb) - => lb.Book.ContentType is ContentType.Parent; - public static SeriesEntry? FindSeriesParent(this IEnumerable gridEntries, LibraryBook seriesEpisode) { if (seriesEpisode.Book.SeriesLink is null) return null; @@ -43,19 +31,6 @@ namespace LibationWinForms.GridView seriesEpisode.Book.SeriesLink.Any( s => s.Series.AudibleSeriesId == lb.LibraryBook.Book.SeriesLink.Single().Series.AudibleSeriesId)); } - - public static LibraryBook? FindSeriesParent(this IEnumerable libraryBooks, LibraryBook seriesEpisode) - { - if (seriesEpisode.Book.SeriesLink is null) return null; - - //Parent books will always have exactly 1 SeriesBook due to how - //they are imported in ApiExtended.getChildEpisodesAsync() - return libraryBooks.FirstOrDefault( - lb => - lb.IsEpisodeParent() && - seriesEpisode.Book.SeriesLink.Any( - s => s.Series.AudibleSeriesId == lb.Book.SeriesLink.Single().Series.AudibleSeriesId)); - } } #nullable disable }