예제 #1
0
        public async Task <bool> SaveProducts(IEnumerable <ExternalProductDto> products)
        {
            _logger.LogInformation("Attempting to save products to database");
            return(await _retryPolicy.ExecuteAsync(async() =>
            {
                try
                {
                    foreach (var product in products)
                    {
                        if (!_context.Products.Any(p => p.Ean == product.Ean && p.Source == product.Source))
                        {
                            // Product doesnt exist, add it
                            var item = new ProductDto
                            {
                                Id = Guid.NewGuid(),
                                InStock = product.InStock,
                                CategoryId = product.CategoryId,
                                CategoryName = product.CategoryName,
                                Description = product.Description,
                                Ean = product.Ean,
                                ExpectedRestock = product.ExpectedRestock,
                                ExternalId = product.Id,
                                Name = product.Name,
                                Price = product.Price,
                                Source = product.Source
                            };
                            await _context.Products.AddAsync(item);
                        }
                        else
                        {
                            // Product exists, lets update it
                            var existingProduct = await _context.Products.Where(s => s.Ean == product.Ean && s.Source == product.Source).FirstOrDefaultAsync();
                            existingProduct.InStock = product.InStock;
                            existingProduct.CategoryId = product.CategoryId;
                            existingProduct.CategoryName = product.CategoryName;
                            existingProduct.Description = product.Description;
                            existingProduct.Ean = product.Ean;
                            existingProduct.ExpectedRestock = product.ExpectedRestock;
                            existingProduct.ExternalId = product.Id;
                            existingProduct.Name = product.Name;
                            existingProduct.Price = product.Price;
                            existingProduct.Source = product.Source;

                            _context.Update(existingProduct);
                        }
                    }
                    await _context.SaveChangesAsync();
                    _logger.LogInformation("Successfully saved products to database");
                    return true;
                }
                catch (Exception e)
                {
                    _logger.LogError("Failed to save products to database: " + products + e + e.StackTrace);
                }
                return false;
            }));
        }
예제 #2
0
 public async Task <Guid> CreateOrder(PurchaseOrderDto purchaseOrder)
 {
     _logger.LogInformation("Creating an order in local database");
     return(await _retryPolicy.ExecuteAsync(async() =>
     {
         try
         {
             var dbEntity = await _context.PurchaseOrders.AddAsync(purchaseOrder);
             await _context.SaveChangesAsync();
             _logger.LogInformation("Successfully created an order in local database");
             return dbEntity.Entity.ID;
         }
         catch (Exception e)
         {
             _logger.LogError("Exception when trying to create a new purchase order:" + purchaseOrder + e + e.StackTrace);
         }
         return Guid.Empty;
     }));
 }