Rare duplicates are making their way into the db. Defensive FirstOrDefault added to address bug #184

This commit is contained in:
Robert McRackan 2022-01-11 07:34:42 -05:00
parent 5103240a76
commit 6b649cf4ca
5 changed files with 13 additions and 7 deletions

View File

@ -3,7 +3,7 @@
<PropertyGroup> <PropertyGroup>
<TargetFramework>net6.0</TargetFramework> <TargetFramework>net6.0</TargetFramework>
<Version>6.6.3.1</Version> <Version>6.6.4.1</Version>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>

View File

@ -209,7 +209,8 @@ namespace AudibleUtilities
new Series new Series
{ {
Asin = parent.Asin, 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 Title = parent.TitleWithSubtitle
} }
}; };

View File

@ -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 // 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 var authors = item
.Authors .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(); .ToList();
var narrators 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 // nested logic is required so order of names is retained. else, contributors may appear in the order they were inserted into the db
: item : item
.Narrators .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(); .ToList();
// categories are laid out for a breadcrumb. category is 1st, subcategory is 2nd // categories are laid out for a breadcrumb. category is 1st, subcategory is 2nd
@ -154,7 +156,8 @@ namespace DtoImporterService
var publisherName = item.Publisher; var publisherName = item.Publisher;
if (!string.IsNullOrWhiteSpace(publisherName)) 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); book.ReplacePublisher(publisher);
} }

View File

@ -82,7 +82,8 @@ namespace DtoImporterService
); );
foreach (var name in newPeople) 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 try
{ {

View File

@ -53,7 +53,8 @@ namespace DtoImporterService
var newItem = gb.ImportItems.First(); var newItem = gb.ImportItems.First();
var libraryBook = new LibraryBook( 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.DtoItem.DateAdded,
newItem.AccountId); newItem.AccountId);
try try