using System; using System.Collections.Generic; using System.Linq; using AudibleApiDTOs; using DataLayer; using InternalUtilities; namespace DtoImporterService { public class CategoryImporter : ItemsImporterBase { public override IEnumerable Validate(IEnumerable items) => new CategoryValidator().Validate(items); protected override int DoImport(IEnumerable items, LibationContext context) { // get distinct var categoryIds = items.GetCategoriesDistinct().Select(c => c.CategoryId).ToList(); // load db existing => .Local loadLocal_categories(categoryIds, context); // upsert var categoryPairs = items.GetCategoryPairsDistinct().ToList(); var qtyNew = upsertCategories(categoryPairs, context); return qtyNew; } private void loadLocal_categories(List categoryIds, LibationContext context) { var localIds = context.Categories.Local.Select(c => c.AudibleCategoryId); var remainingCategoryIds = categoryIds .Distinct() .Except(localIds) .ToList(); if (remainingCategoryIds.Any()) context.Categories.Where(c => remainingCategoryIds.Contains(c.AudibleCategoryId)).ToList(); } // only use after loading contributors => local private int upsertCategories(List categoryPairs, LibationContext context) { var qtyNew = 0; foreach (var pair in categoryPairs) { for (var i = 0; i < pair.Length; i++) { var id = pair[i].CategoryId; var name = pair[i].CategoryName; Category parentCategory = null; if (i == 1) parentCategory = context.Categories.Local.Single(c => c.AudibleCategoryId == pair[0].CategoryId); var category = context.Categories.Local.SingleOrDefault(c => c.AudibleCategoryId == id); if (category is null) { category = context.Categories.Add(new Category(new AudibleCategoryId(id), name)).Entity; qtyNew++; } category.UpdateParentCategory(parentCategory); } } return qtyNew; } } }