public void AddImagesToProduct(VM.Product product) { foreach (var img in product.Images) { var productImage = new DM.ProductImage() { ProductID = product.ProductID, Image = img }; dbContext.ProductImages.Add(productImage); } dbContext.SaveChanges(); }
public bool Update(VM.Product product) { var pro = (from c in dbContext.Products where c.ProductID == product.ProductID select c).FirstOrDefault(); if (pro != null) { pro.ProductName = product.ProductName; pro.Color = product.Color; pro.UnitPrice = product.UnitPrice; pro.ProductDescription = product.ProductDescription; pro.DiscountPercentage = product.DiscountPercentage; dbContext.SaveChanges(); return(true); } return(false); }
public void Add(VM.Product product) { var prod = new DM.Product() { CategoryID = product.CategoryID, SubCategoryID = product.SubcategoryID, ProductDescription = product.ProductDescription, Color = product.Color, UnitPrice = product.UnitPrice, ProductName = product.ProductName, DiscountPercentage = product.DiscountPercentage }; var insertedProduct = dbContext.Products.Add(prod); foreach (var s in product.Stocks) { var stock = new DM.Stock() { SizeID = s.SizeID, ProductID = insertedProduct.ProductID, Quantity = s.Quantity }; dbContext.Stocks.Add(stock); //dbContext.SaveChanges(); } foreach (var img in product.Images) { var productImage = new DM.ProductImage() { ProductID = insertedProduct.ProductID, Image = img }; dbContext.ProductImages.Add(productImage); //dbContext.SaveChanges(); } dbContext.SaveChanges(); }
public bool UpdateStock(VM.Product product) { int row = 0; var dbStocks = dbContext.Stocks.Where(x => x.ProductID == product.ProductID).ToList(); var sizeIDs = dbStocks.Select(x => x.SizeID).ToArray(); var stockIDs = dbStocks.Select(x => x.StockID).ToArray(); var stkIDs = product.Stocks.Select(x => x.StockID).ToArray(); foreach (var s in product.Stocks) { if (s.StockID > 0) { var stk = dbContext.Stocks.Find(s.StockID); stk.Quantity = s.Quantity; stk.SizeID = s.SizeID; if (dbContext.SaveChanges() > 0) { row++; } } else if (s.StockID == 0 && !sizeIDs.Contains(s.SizeID)) { var stock = new DM.Stock() { SizeID = s.SizeID, ProductID = product.ProductID, Quantity = s.Quantity }; dbContext.Stocks.Add(stock); if (dbContext.SaveChanges() > 0) { row++; } } else if (s.StockID == 0 && sizeIDs.Contains(s.SizeID)) { var stk = dbContext.Stocks.Where(x => x.ProductID == product.ProductID && x.SizeID == s.SizeID).FirstOrDefault(); stk.Quantity = s.Quantity; if (dbContext.SaveChanges() > 0) { row++; } } } foreach (var st in stockIDs) { if (!stkIDs.Contains(st)) { var d = dbContext.Stocks.Find(st); dbContext.Stocks.Remove(d); //dbContext.Entry(d).State = EntityState.Deleted; if (dbContext.SaveChanges() > 0) { row++; } } } if (row > 0) { return(true); } else { return(false); } }