From 6b649cf4caf92a082b9479b3d27be1bef115f345 Mon Sep 17 00:00:00 2001 From: Robert McRackan Date: Tue, 11 Jan 2022 07:34:42 -0500 Subject: [PATCH] Rare duplicates are making their way into the db. Defensive FirstOrDefault added to address bug #184 --- AppScaffolding/AppScaffolding.csproj | 2 +- AudibleUtilities/ApiExtended.cs | 3 ++- DtoImporterService/BookImporter.cs | 9 ++++++--- DtoImporterService/ContributorImporter.cs | 3 ++- DtoImporterService/LibraryBookImporter.cs | 3 ++- 5 files changed, 13 insertions(+), 7 deletions(-) diff --git a/AppScaffolding/AppScaffolding.csproj b/AppScaffolding/AppScaffolding.csproj index 00706f18..bcfbd257 100644 --- a/AppScaffolding/AppScaffolding.csproj +++ b/AppScaffolding/AppScaffolding.csproj @@ -3,7 +3,7 @@ net6.0 - 6.6.3.1 + 6.6.4.1 diff --git a/AudibleUtilities/ApiExtended.cs b/AudibleUtilities/ApiExtended.cs index 84e29508..3d311457 100644 --- a/AudibleUtilities/ApiExtended.cs +++ b/AudibleUtilities/ApiExtended.cs @@ -209,7 +209,8 @@ namespace AudibleUtilities new Series { Asin = parent.Asin, - Sequence = parent.Relationships.Single(r => r.Asin == child.Asin).Sort.ToString(), + // This should properly be Single() not FirstOrDefault(), but FirstOrDefault is defensive for malformed data from audible + Sequence = parent.Relationships.FirstOrDefault(r => r.Asin == child.Asin).Sort.ToString(), Title = parent.TitleWithSubtitle } }; diff --git a/DtoImporterService/BookImporter.cs b/DtoImporterService/BookImporter.cs index cb2e99f4..2ee45574 100644 --- a/DtoImporterService/BookImporter.cs +++ b/DtoImporterService/BookImporter.cs @@ -94,7 +94,8 @@ namespace DtoImporterService // nested logic is required so order of names is retained. else, contributors may appear in the order they were inserted into the db var authors = item .Authors - .Select(a => DbContext.Contributors.Local.Single(c => a.Name == c.Name)) + // This should properly be Single() not FirstOrDefault(), but FirstOrDefault is defensive + .Select(a => DbContext.Contributors.Local.FirstOrDefault(c => a.Name == c.Name)) .ToList(); var narrators @@ -104,7 +105,8 @@ namespace DtoImporterService // nested logic is required so order of names is retained. else, contributors may appear in the order they were inserted into the db : item .Narrators - .Select(n => DbContext.Contributors.Local.Single(c => n.Name == c.Name)) + // This should properly be Single() not FirstOrDefault(), but FirstOrDefault is defensive + .Select(n => DbContext.Contributors.Local.FirstOrDefault(c => n.Name == c.Name)) .ToList(); // categories are laid out for a breadcrumb. category is 1st, subcategory is 2nd @@ -154,7 +156,8 @@ namespace DtoImporterService var publisherName = item.Publisher; if (!string.IsNullOrWhiteSpace(publisherName)) { - var publisher = DbContext.Contributors.Local.Single(c => publisherName == c.Name); + // This should properly be Single() not FirstOrDefault(), but FirstOrDefault is defensive + var publisher = DbContext.Contributors.Local.FirstOrDefault(c => publisherName == c.Name); book.ReplacePublisher(publisher); } diff --git a/DtoImporterService/ContributorImporter.cs b/DtoImporterService/ContributorImporter.cs index 5dce7362..e37a160e 100644 --- a/DtoImporterService/ContributorImporter.cs +++ b/DtoImporterService/ContributorImporter.cs @@ -82,7 +82,8 @@ namespace DtoImporterService ); foreach (var name in newPeople) { - var p = groupby.Single(g => g.Name == name).People.First(); + // This should properly be Single() not FirstOrDefault(), but FirstOrDefault is defensive + var p = groupby.FirstOrDefault(g => g.Name == name).People.First(); try { diff --git a/DtoImporterService/LibraryBookImporter.cs b/DtoImporterService/LibraryBookImporter.cs index d90eda7b..5b5e5aa5 100644 --- a/DtoImporterService/LibraryBookImporter.cs +++ b/DtoImporterService/LibraryBookImporter.cs @@ -53,7 +53,8 @@ namespace DtoImporterService var newItem = gb.ImportItems.First(); var libraryBook = new LibraryBook( - DbContext.Books.Local.Single(b => b.AudibleProductId == newItem.DtoItem.ProductId), + // This should properly be Single() not FirstOrDefault(), but FirstOrDefault is defensive + DbContext.Books.Local.FirstOrDefault(b => b.AudibleProductId == newItem.DtoItem.ProductId), newItem.DtoItem.DateAdded, newItem.AccountId); try