diff --git a/AppScaffolding/AppScaffolding.csproj b/AppScaffolding/AppScaffolding.csproj index 6a1e02c2..7efe8ce8 100644 --- a/AppScaffolding/AppScaffolding.csproj +++ b/AppScaffolding/AppScaffolding.csproj @@ -3,7 +3,7 @@ net6.0 - 6.5.5.1 + 6.5.6.1 diff --git a/DtoImporterService/BookImporter.cs b/DtoImporterService/BookImporter.cs index 494ef35c..cb2e99f4 100644 --- a/DtoImporterService/BookImporter.cs +++ b/DtoImporterService/BookImporter.cs @@ -120,17 +120,36 @@ namespace DtoImporterService var category = DbContext.Categories.Local.SingleOrDefault(c => c.AudibleCategoryId == lastCategory); - var book = DbContext.Books.Add(new Book( - new AudibleProductId(item.ProductId), - item.TitleWithSubtitle, - item.Description, - item.LengthInMinutes, - contentType, - authors, - narrators, - category, - importItem.LocaleName) - ).Entity; + Book book; + try + { + book = DbContext.Books.Add(new Book( + new AudibleProductId(item.ProductId), + item.TitleWithSubtitle, + item.Description, + item.LengthInMinutes, + contentType, + authors, + narrators, + category, + importItem.LocaleName) + ).Entity; + } + catch (Exception ex) + { + Serilog.Log.Logger.Error(ex, "Error adding book. {@DebugInfo}", new { + item.ProductId, + item.TitleWithSubtitle, + item.Description, + item.LengthInMinutes, + contentType, + QtyAuthors = authors?.Count, + QtyNarrators = narrators?.Count, + Category = category?.Name, + importItem.LocaleName + }); + throw; + } var publisherName = item.Publisher; if (!string.IsNullOrWhiteSpace(publisherName)) diff --git a/DtoImporterService/CategoryImporter.cs b/DtoImporterService/CategoryImporter.cs index 32bd3a8b..09b8f95b 100644 --- a/DtoImporterService/CategoryImporter.cs +++ b/DtoImporterService/CategoryImporter.cs @@ -73,7 +73,15 @@ namespace DtoImporterService var category = DbContext.Categories.Local.FirstOrDefault(c => c.AudibleCategoryId == id); if (category is null) { - category = DbContext.Categories.Add(new Category(new AudibleCategoryId(id), name)).Entity; + try + { + category = DbContext.Categories.Add(new Category(new AudibleCategoryId(id), name)).Entity; + } + catch (Exception ex) + { + Serilog.Log.Logger.Error(ex, "Error adding category. {@DebugInfo}", new { id, name }); + throw; + } qtyNew++; } diff --git a/DtoImporterService/ContributorImporter.cs b/DtoImporterService/ContributorImporter.cs index da90d7f8..5dce7362 100644 --- a/DtoImporterService/ContributorImporter.cs +++ b/DtoImporterService/ContributorImporter.cs @@ -83,7 +83,16 @@ namespace DtoImporterService foreach (var name in newPeople) { var p = groupby.Single(g => g.Name == name).People.First(); - DbContext.Contributors.Add(new Contributor(p.Name, p.Asin)); + + try + { + DbContext.Contributors.Add(new Contributor(p.Name, p.Asin)); + } + catch (Exception ex) + { + Serilog.Log.Logger.Error(ex, "Error adding person. {@DebugInfo}", new { p?.Name, p?.Asin }); + throw; + } } return newPeople.Count; @@ -99,7 +108,17 @@ namespace DtoImporterService .ToList(); foreach (var pub in newPublishers) - DbContext.Contributors.Add(new Contributor(pub)); + { + try + { + DbContext.Contributors.Add(new Contributor(pub)); + } + catch (Exception ex) + { + Serilog.Log.Logger.Error(ex, "Error adding publisher. {@DebugInfo}", new { pub }); + throw; + } + } return newPublishers.Count; } diff --git a/DtoImporterService/LibraryBookImporter.cs b/DtoImporterService/LibraryBookImporter.cs index aa9c4198..d90eda7b 100644 --- a/DtoImporterService/LibraryBookImporter.cs +++ b/DtoImporterService/LibraryBookImporter.cs @@ -35,18 +35,38 @@ namespace DtoImporterService // CURRENT SOLUTION: don't re-insert var currentLibraryProductIds = DbContext.LibraryBooks.Select(l => l.Book.AudibleProductId).ToList(); - var newItems = importItems.Where(dto => !currentLibraryProductIds.Contains(dto.DtoItem.ProductId)).ToList(); + var newItems = importItems + .Where(dto => !currentLibraryProductIds + .Contains(dto.DtoItem.ProductId)) + .ToList(); - foreach (var newItem in newItems) + // if 2 accounts try to import the same book in the same transaction: error since we're only tracking and pulling by asin. + // just use the first + var groupby = newItems.GroupBy( + i => i.DtoItem.ProductId, + i => i, + (key, g) => new { ProductId = key, ImportItems = g.ToList() } + ) + .ToList(); + foreach (var gb in groupby) { + var newItem = gb.ImportItems.First(); + var libraryBook = new LibraryBook( DbContext.Books.Local.Single(b => b.AudibleProductId == newItem.DtoItem.ProductId), newItem.DtoItem.DateAdded, newItem.AccountId); - DbContext.LibraryBooks.Add(libraryBook); + try + { + DbContext.LibraryBooks.Add(libraryBook); + } + catch (Exception ex) + { + Serilog.Log.Logger.Error(ex, "Error adding library book. {@DebugInfo}", new { libraryBook.Book, libraryBook.Account }); + } } - var qtyNew = newItems.Count; + var qtyNew = groupby.Count; return qtyNew; } } diff --git a/DtoImporterService/SeriesImporter.cs b/DtoImporterService/SeriesImporter.cs index de42e02d..6c4a5414 100644 --- a/DtoImporterService/SeriesImporter.cs +++ b/DtoImporterService/SeriesImporter.cs @@ -51,7 +51,15 @@ namespace DtoImporterService var series = DbContext.Series.Local.FirstOrDefault(c => c.AudibleSeriesId == s.SeriesId); if (series is null) { - series = DbContext.Series.Add(new DataLayer.Series(new AudibleSeriesId(s.SeriesId))).Entity; + try + { + series = DbContext.Series.Add(new DataLayer.Series(new AudibleSeriesId(s.SeriesId))).Entity; + } + catch (Exception ex) + { + Serilog.Log.Logger.Error(ex, "Error adding series. {@DebugInfo}", new { s?.SeriesId }); + throw; + } qtyNew++; } series.UpdateName(s.SeriesName);