private void CreateOrUpdateCart(int employeeId, int productId, int quantity, CartUpdateMode mode)
        {
            using (var context = new eToolsContext())
            {
                // collect entities
                Employee  employee = context.Employees.Find(employeeId);
                StockItem product  = context.StockItems.Find(productId);

                // validate request
                List <string> errors = ValidateCartItem(employee, product, quantity);
                if (errors.Count > 0)
                {
                    string errorMessage = (mode == CartUpdateMode.INCREMENTAL) ?
                                          "Product not added to cart." :
                                          "Product not updated in cart.";
                    throw new BusinessRuleException(errorMessage, errors);
                }
                else
                {
                    // validation passed, update cart
                    CreateOrUpdateCartValidated(employee, product, quantity, mode);
                }
            }
        }
        // Update cart and cartitems. Must be called with validated parameters.
        private void CreateOrUpdateCartValidated(Employee employee, StockItem product, int quantity, CartUpdateMode mode)
        {
            // handle cart update in one transaction
            using (var context = new eToolsContext())
            {
                // find shopping cart
                ShoppingCart cart = context.ShoppingCarts.Where(x => x.EmployeeID == employee.EmployeeID).FirstOrDefault();
                // create new cart if not found or update existing cart
                if (cart == null)
                {
                    // create new cart
                    cart            = new ShoppingCart();
                    cart.EmployeeID = employee.EmployeeID;
                    cart.CreatedOn  = DateTime.Now;
                    context.ShoppingCarts.Add(cart);
                }
                else
                {
                    // update existing cart
                    cart.UpdatedOn = DateTime.Now;
                    context.Entry(cart).Property(x => x.UpdatedOn).IsModified = true;
                    // context.Entry(cart).State = System.Data.Entity.EntityState.Modified;
                }

                // find cart item in cart
                ShoppingCartItem item = cart.ShoppingCartItems.Where(x => x.StockItemID == product.StockItemID).FirstOrDefault();
                // add item to cart if not found or update qty if found
                if (item == null)
                {
                    // add item to cart
                    item = new ShoppingCartItem();
                    item.ShoppingCartID = cart.ShoppingCartID;
                    item.StockItemID    = product.StockItemID;
                    item.Quantity       = quantity;
                    context.ShoppingCartItems.Add(item);
                }
                else
                {
                    // update existing item in cart
                    if (mode == CartUpdateMode.INCREMENTAL)
                    {
                        item.Quantity += quantity;
                        if (item.Quantity > product.QuantityOnHand)
                        {
                            throw new BusinessRuleException(
                                      "Product not added to cart.",
                                      string.Format("Not enough stock: requested {0} available {1}.",
                                                    item.Quantity,
                                                    product.QuantityOnHand));
                        }
                    }
                    else
                    {
                        item.Quantity = quantity;
                        if (item.Quantity > product.QuantityOnHand)
                        {
                            throw new BusinessRuleException(
                                      "Product not added to cart.",
                                      string.Format("Not enough stock: requested {0} available {1}.",
                                                    item.Quantity,
                                                    product.QuantityOnHand));
                        }
                    }
                    context.Entry(item).Property(x => x.Quantity).IsModified = true;
                }

                // commit transaction
                context.SaveChanges();
            }
        }