public string UpdateProduct(Product product) { try { if (product != null && product.ProductId > 0) { var currentProduct = _context.products.Where(p => p.ProductId == product.ProductId).SingleOrDefault(); if (currentProduct != null) { currentProduct.ProductId = product.ProductId; currentProduct.ProductName = product.ProductName; currentProduct.Price = product.Price; currentProduct.Quantity = product.Quantity; _context.products.Attach(currentProduct); _context.Entry(currentProduct).State = EntityState.Modified; _context.SaveChanges(); return("Successfully updated!"); } else { throw new KeyNotFoundException(); } } else { throw new KeyNotFoundException(); } } catch (Exception) { throw; } }
public Order submitOrder(Order orderDetails) { try { var pp = _context.orders.Add(orderDetails); //update quantity... _context.SaveChanges(); Product objProruct = _context.products.Find(orderDetails.ProductId); if (objProruct == null) { return(new Order()); } objProruct.Quantity = objProruct.Quantity - 1; _context.Entry(objProruct).State = EntityState.Modified; _context.SaveChanges(); return(pp); } catch (Exception) { throw; } }