From 00cf7693d55cdf94803e7b509074b42fb08d2239 Mon Sep 17 00:00:00 2001 From: Mbucari Date: Tue, 28 Mar 2023 10:02:22 -0600 Subject: [PATCH] Add code comments --- Source/DtoImporterService/LibraryBookImporter.cs | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/Source/DtoImporterService/LibraryBookImporter.cs b/Source/DtoImporterService/LibraryBookImporter.cs index 2942a774..e08a29a1 100644 --- a/Source/DtoImporterService/LibraryBookImporter.cs +++ b/Source/DtoImporterService/LibraryBookImporter.cs @@ -41,13 +41,20 @@ namespace DtoImporterService // // CURRENT SOLUTION: don't re-insert + + //When Books are upserted during the BookImporter run, they are linked to their LibraryBook in the DbContext + //instance. If a LibraryBook has a null book here, that means it's Book was not imported during by BookImporter. + //There should never be duplicates, but this is defensive. var existingEntries = DbContext.LibraryBooks.AsEnumerable().Where(l => l.Book is not null).ToDictionarySafe(l => l.Book.AudibleProductId); - var hash = ToDictionarySafe(importItems, dto => dto.DtoItem.ProductId, tieBreak); + + //If importItems are contains duplicates by asin, keep the Item that's "available" + var uniqueImportItems = ToDictionarySafe(importItems, dto => dto.DtoItem.ProductId, tieBreak); + int qtyNew = 0; - foreach (var item in hash.Values) + foreach (var item in uniqueImportItems.Values) { - if (existingEntries.TryGetValue(item.DtoItem.ProductId, out LibraryBook existing)) + if (qtyNew == 0 && existingEntries.TryGetValue(item.DtoItem.ProductId, out LibraryBook existing)) { if (existing.Account != item.AccountId) {