Пример #1
0
        public void Delete(Product element)
        {
            using (StorageDbContext context = new StorageDbContext())
            {
                context.Configuration.AutoDetectChangesEnabled = false;
                using (var transaction = context.Database.BeginTransaction())
                {
                    try
                    {
                        ConteinerRepository conteiner = new ConteinerRepository();
                        conteiner.DeleteAll(element);

                        IngredientsForProductRepository recept = new IngredientsForProductRepository();
                        recept.Delete(element);

                        context.Configuration.ValidateOnSaveEnabled = false;
                        context.Products.Attach(element);
                        context.Entry(element).State = EntityState.Deleted;

                        context.ChangeTracker.DetectChanges();
                        context.SaveChanges();
                        transaction.Commit();
                    }
                    catch (Exception)
                    {
                        transaction.Rollback();
                        throw new InvalidOperationException("Помилка видалення.");
                    }
                    finally
                    {
                        context.Configuration.ValidateOnSaveEnabled = true;
                    }
                }
            }
        }
        public void Delete(Ingredient ingredient)
        {
            using (StorageDbContext context = new StorageDbContext())
            {
                context.Configuration.AutoDetectChangesEnabled = false;
                using (var transaction = context.Database.BeginTransaction())
                {
                    try
                    {
                        PackageRepository package           = new PackageRepository();
                        ProductRepository productRepository = new ProductRepository();
                        IngredientsForProductRepository ingredientsForProductRepository = new IngredientsForProductRepository();

                        package.Delete(ingredient);

                        var allReceiptsWithEntryIngredients = ingredientsForProductRepository.GetDataSource().Where(n => n.IngredientId == ingredient.Id).ToList();
                        if (allReceiptsWithEntryIngredients != null)
                        {
                            foreach (var oneIngredientForProductInReceipt in allReceiptsWithEntryIngredients)
                            {
                                Product productWithEntryIngredientInReceipt = productRepository.GetDataSource().First(n => n.Id == oneIngredientForProductInReceipt.ProductId);

                                productRepository.Delete(productWithEntryIngredientInReceipt);
                            }
                        }

                        context.Configuration.ValidateOnSaveEnabled = false;
                        context.Ingredients.Attach(ingredient);
                        context.Entry(ingredient).State = EntityState.Deleted;
                        context.ChangeTracker.DetectChanges();
                        context.SaveChanges();
                        transaction.Commit();
                    }
                    catch (Exception)
                    {
                        transaction.Rollback();
                        throw new InvalidOperationException("Помилка видалення.");
                    }
                    finally
                    {
                        context.Configuration.ValidateOnSaveEnabled = true;
                    }
                }
            }
        }
Пример #3
0
        public void Edit(Product newProduct, Dictionary <Ingredient, double> newReceipt)
        {
            using (StorageDbContext context = new StorageDbContext())
            {
                context.Configuration.AutoDetectChangesEnabled = false;
                using (var transaction = context.Database.BeginTransaction())
                {
                    try
                    {
                        IngredientsForProductRepository ingredientsForProductRepository = new IngredientsForProductRepository();

                        var currentProduct = context.Products.FirstOrDefault(element => element.Id == newProduct.Id);

                        if (currentProduct == null)
                        {
                            throw new ArgumentNullException("Цей продукт не існує");
                        }

                        currentProduct.Name = newProduct.Name;

                        ingredientsForProductRepository.Delete(currentProduct);

                        foreach (var element in newReceipt)
                        {
                            context.IngredientsForProducts.Add(new IngredientsForProduct(currentProduct, element.Key, element.Value));
                        }

                        context.ChangeTracker.DetectChanges();
                        context.SaveChanges();
                        transaction.Commit();
                    }
                    catch (Exception e)
                    {
                        transaction.Rollback();
                        throw new ArgumentException(e.Message);
                    }
                }
            }
        }