Additional safety check

This commit is contained in:
Michael Bucari-Tovo 2022-06-11 15:10:18 -06:00
parent 5e145846bd
commit 26f0ff62df
2 changed files with 9 additions and 11 deletions

View File

@ -43,20 +43,16 @@ namespace DataLayer
.Include(le => le.Book).ThenInclude(b => b.ContributorsLink).ThenInclude(c => c.Contributor) .Include(le => le.Book).ThenInclude(b => b.ContributorsLink).ThenInclude(c => c.Contributor)
.Include(le => le.Book).ThenInclude(b => b.Category).ThenInclude(c => c.ParentCategory); .Include(le => le.Book).ThenInclude(b => b.Category).ThenInclude(c => c.ParentCategory);
public static IEnumerable<LibraryBook> FindOrphanedEpisodes(this IEnumerable<LibraryBook> libraryBooks) public static IEnumerable<LibraryBook> ParentedEpisodes(this IEnumerable<LibraryBook> libraryBooks)
{ => libraryBooks.Where(lb => lb.Book.IsEpisodeParent()).SelectMany(s => libraryBooks.FindChildren(s));
var parentedEpisodes =
libraryBooks
.Where(lb => lb.Book.IsEpisodeParent())
.SelectMany(s => libraryBooks.FindChildren(s));
return public static IEnumerable<LibraryBook> FindOrphanedEpisodes(this IEnumerable<LibraryBook> libraryBooks)
libraryBooks => libraryBooks
.Where(lb => lb.Book.IsEpisodeChild()) .Where(lb => lb.Book.IsEpisodeChild())
.ExceptBy( .ExceptBy(
parentedEpisodes libraryBooks
.ParentedEpisodes()
.Select(ge => ge.Book.AudibleProductId), ge => ge.Book.AudibleProductId); .Select(ge => ge.Book.AudibleProductId), ge => ge.Book.AudibleProductId);
}
#nullable enable #nullable enable
public static LibraryBook? FindSeriesParent(this IEnumerable<LibraryBook> libraryBooks, LibraryBook seriesEpisode) public static LibraryBook? FindSeriesParent(this IEnumerable<LibraryBook> libraryBooks, LibraryBook seriesEpisode)

View File

@ -156,6 +156,7 @@ namespace LibationWinForms.GridView
var allEntries = bindingList.AllItems().BookEntries(); var allEntries = bindingList.AllItems().BookEntries();
var seriesEntries = bindingList.AllItems().SeriesEntries().ToList(); var seriesEntries = bindingList.AllItems().SeriesEntries().ToList();
var parentedEpisodes = dbBooks.ParentedEpisodes();
foreach (var libraryBook in dbBooks.OrderBy(e => e.DateAdded)) foreach (var libraryBook in dbBooks.OrderBy(e => e.DateAdded))
{ {
@ -163,7 +164,8 @@ namespace LibationWinForms.GridView
if (libraryBook.Book.IsProduct()) if (libraryBook.Book.IsProduct())
AddOrUpdateBook(libraryBook, existingEntry); AddOrUpdateBook(libraryBook, existingEntry);
else if(libraryBook.Book.IsEpisodeChild()) else if(parentedEpisodes.Any(lb => lb == libraryBook))
//Only try to add or update is this LibraryBook is a know child of a parent
AddOrUpdateEpisode(libraryBook, existingEntry, seriesEntries, dbBooks); AddOrUpdateEpisode(libraryBook, existingEntry, seriesEntries, dbBooks);
} }