private static void UpdateProduct(ImmutableHashSet <ParsedProduct> toUpdateParsedProducts, Product productToUpdate) { var parsedProduct = toUpdateParsedProducts.FirstOrDefault(u => ProductDataComparer.IsParsedProductEqualToProduct(u, productToUpdate)) ?? throw new Exception("Cannot find any parsed product"); productToUpdate.Price = parsedProduct.Price; productToUpdate.Mass = parsedProduct.Mass; }
public void CreateOrUpdateProducts(IEnumerable <ParsedProduct> products) { var filteredProducts = products .Where(p => p != null) .ToArray(); var toUpdateProducts = GetProductsToUpdate(filteredProducts); bool isProductToUpdate(ParsedProduct u) => toUpdateProducts.Any(p => ProductDataComparer.IsParsedProductEqualToProduct(u, p)); var(toUpdateParsedProducts, toCreateParsedProducts) = filteredProducts.SplitCollection(isProductToUpdate); UpdateProducts(toUpdateProducts, toUpdateParsedProducts.ToImmutableHashSet()); Context.UpdateRange(toUpdateProducts); var toCreateProductsInfo = _productsConverter.ConvertParsedProducts(toCreateParsedProducts).ToArray(); Context.AddRange(toCreateProductsInfo.Select(t => t.Product)); Context.SaveChanges(); var imagesDict = ProductsInfoToDict(toCreateProductsInfo); UploadImagesToProducts(imagesDict); }