示例#1
0
        /// <summary>
        /// Update old products (including related images) in db with new ones
        /// </summary>
        /// <param name="products">products</param>
        /// <returns>void</returns>
        public async Task UpdateProducts(products products)
        {
            try
            {
                List <Product> productRecords = new List <Product>();
                List <Image>   imageRecords   = new List <Image>();

                foreach (var product in products.product)
                {
                    productRecords.Add(new Product
                    {
                        BodyHtml       = product.bodyhtml,
                        CreatedAt      = product.createdat.Value,
                        Handle         = product.handle,
                        ProductId      = long.Parse(product.id.Value.ToString()),
                        ProductType    = product.producttype,
                        PublishedScope = product.publishedscope,
                        Tags           = product.tags,
                        Title          = product.title,
                        Vendor         = product.vendor
                    });

                    imageRecords.Add(new Image
                    {
                        ProductId = long.Parse(product.image.productid.Value.ToString()),
                        ImageId   = long.Parse(product.image.id.Value.ToString()),
                        CreatedAt = product.image.createdat.Value,
                        UpdatedAt = product.image.updatedat.Value,
                        Height    = product.image.height.Value,
                        Width     = product.image.width.Value,
                        Src       = product.image.src
                    });
                }

                // Update products
                _nu3Context.Products.RemoveRange(await _nu3Context.Products.ToListAsync());
                _nu3Context.Products.AddRange(productRecords);

                // Update images
                _nu3Context.Images.RemoveRange(await _nu3Context.Images.ToListAsync());
                _nu3Context.Images.AddRange(imageRecords);

                // Save changes to db
                await _nu3Context.SaveChangesAsync();
            }
            catch (Exception ex)
            {
                throw new Exception($"Failed to update products: {ex.Message}");
            }
        }
示例#2
0
        /// <summary>
        /// Update the inventory records (replace old records with new ones)
        /// </summary>
        /// <param name="inventories">List of inventory records</param>
        /// <returns>void</returns>
        public async Task UpdateInventory(IEnumerable <Inventory> inventories)
        {
            try
            {
                // Delete existing records
                _nu3Context.Inventories.RemoveRange(await _nu3Context.Inventories.ToListAsync());

                // Add new ones
                _nu3Context.Inventories.AddRange(inventories);

                // Update the DB
                await _nu3Context.SaveChangesAsync();
            }
            catch (Exception ex)
            {
                throw new Exception($"Failed to update the inventory: {ex.Message}");
            }
        }