public bool AddProductMaterial(int productId, int materialId) { Recipe recipe = new Recipe(); recipe.ProductId = productId; recipe.ProductMaterialId = materialId; recipe.RecipeQuantity = 1; db.Recipes.Add(recipe); db.SaveChanges(); return true; }
public bool AddProduct(string productName, string productUnit, double productWeight, string productDes, string productNote, int productPrice, int dropCate, string productCode, string productImage, int[] materialId, int[] materialQuantity) { DbContextTransaction contextTransaction = db.Database.BeginTransaction(); Product product = new Product(); product.ProductName = productName; product.Unit = productUnit; product.ProductWeight = productWeight; if (productDes != null) { product.Descriptions = productDes; } if (productNote != null) { product.Note = productNote; } product.ProductStandardPrice = productPrice; product.CategoryId = dropCate; product.ProductCode = productCode; if (productImage != null) { product.ProductImage = productImage; } product.IsActive = true; db.Products.Add(product); db.SaveChanges(); int productId = GetProductId(); for (int i = 0; i < materialId.Length; i++) { Recipe recipe = new Recipe(); recipe.ProductId = productId; recipe.ProductMaterialId = materialId[i]; recipe.RecipeQuantity = materialQuantity[i]; db.Recipes.Add(recipe); db.SaveChanges(); } try { contextTransaction.Commit(); } catch (Exception) { contextTransaction.Rollback(); } finally { contextTransaction.Dispose(); } return true; }
public bool EditProduct(int productId, string productName, string productUnit, double productWeight, string productDes, string productNote, int productPrice, int dropCate, string productCode, string productImage, int[] materialId, int[] materialQuantity) { DbContextTransaction contextTransaction = db.Database.BeginTransaction(); //bool checkEdit = CheckEditInformation(productId, productPrice, materialId, materialQuantity); //if (checkEdit) //{ // Product productOld = db.Products.SingleOrDefault(n => n.ProductId == productId); // productOld.IsActive = false; // Product product = new Product(); // product.ProductName = productName; // product.Unit = productUnit; // product.ProductWeight = productWeight; // if (productDes != null) // { // product.Descriptions = productDes; // } // if (productNote != null) // { // product.Note = productNote; // } // product.ProductStandardPrice = productPrice; // product.CategoryId = dropCate; // product.ProductCode = productCode; // if (productImage != null) // { // product.ProductImage = productImage; // } // product.IsActive = true; // db.Products.Add(product); // db.SaveChanges(); // int newProductId = GetProductId(); // for (int i = 0; i < materialId.Length; i++) // { // Recipe recipe = new Recipe(); // recipe.ProductId = newProductId; // recipe.ProductMaterialId = materialId[i]; // recipe.RecipeQuantity = materialQuantity[i]; // db.Recipes.Add(recipe); // db.SaveChanges(); // } // db.SaveChanges(); //} //else //{ Product product = db.Products.SingleOrDefault(n => n.ProductId == productId); product.ProductName = productName; product.Unit = productUnit; product.ProductWeight = productWeight; if (productDes != null) { product.Descriptions = productDes; } if (productNote != null) { product.Note = productNote; } product.ProductStandardPrice = productPrice; product.CategoryId = dropCate; product.ProductCode = productCode; if (productImage != null) { product.ProductImage = productImage; } product.IsActive = true; db.SaveChanges(); List<Recipe> oldRecipe = db.Recipes.Where(n => n.ProductId == productId).ToList(); int numberOfOldRecipe = oldRecipe.Count; for (int i = 0; i < numberOfOldRecipe; i++) { db.Recipes.Remove(oldRecipe[i]); } db.SaveChanges(); for (int i = 0; i < materialId.Length; i++) { Recipe recipe = new Recipe(); recipe.ProductId = productId; recipe.ProductMaterialId = materialId[i]; recipe.RecipeQuantity = materialQuantity[i]; db.Recipes.Add(recipe); } db.SaveChanges(); try { contextTransaction.Commit(); } catch (Exception) { contextTransaction.Rollback(); } finally { contextTransaction.Dispose(); } return true; }