예제 #1
0
        //Allows a customer to delete a dishe from the cart. If there is multiple times the same dish, quantity will be decremented.
        public IActionResult DeleteItem(int idDish)
        {
            DishesManager dManager = new DishesManager(Configuration);
            int           price    = dManager.GetDishePrice(idDish);

            HttpContext.Session.SetInt32("TotalAmount", (int)HttpContext.Session.GetInt32("TotalAmount") - price);

            List <Item> cart         = SessionHelper.GetObjectFromJson <List <Item> >(HttpContext.Session, "cart");
            var         itemToRemove = cart.Single(d => d.Dishe.idDishes == idDish);

            //In case of the element the customer wants to delete is the last one, we empty the session.
            if (cart.Count == 1)
            {
                SessionHelper.SetObjectAsJson(HttpContext.Session, "cart", null);
            }
            //If the item has more than 1 quantity, we just decrement it but do not delete the entire
            else if (itemToRemove.Quantity > 1)
            {
                cart.Remove(itemToRemove);
                itemToRemove.Quantity--;
                cart.Add(itemToRemove);
                SessionHelper.SetObjectAsJson(HttpContext.Session, "cart", cart);
            }
            else
            {
                cart.Remove(itemToRemove);
                SessionHelper.SetObjectAsJson(HttpContext.Session, "cart", cart);
            }
            return(RedirectToAction("Index", "Cart"));
        }
예제 #2
0
        //Allows customer to add a dish to his cart
        public ActionResult AddToCart(int id)
        {
            //We retrieve the dishe's price
            DishesManager dManager = new DishesManager(Configuration);
            Dishes        dishe    = new Dishes();
            int           price    = dManager.GetDishePrice(id);

            //We add the price to the total amount, we initiate the session value if it is the first dish of the cart.
            if (HttpContext.Session.GetInt32("TotalAmount") != null)
            {
                HttpContext.Session.SetInt32("TotalAmount", (price + (int)HttpContext.Session.GetInt32("TotalAmount")));
            }
            else
            {
                HttpContext.Session.SetInt32("TotalAmount", price);
            }
            //So if it is the first dish to be added we create a list of item(Dishe+quantity) in the session
            if (SessionHelper.GetObjectFromJson <List <Item> >(HttpContext.Session, "cart") == null)
            {
                List <Item> cart = new List <Item>();
                cart.Add(new Item {
                    Dishe = dManager.GetDishe(id), Quantity = 1
                });
                SessionHelper.SetObjectAsJson(HttpContext.Session, "cart", cart);
            }
            else
            {   /*Else if the list is already created we get it and check if the dish is already in the list.
                 *  If yes we increment the quantity, else we add the dish as a new item.
                 */
                List <Item> cart  = SessionHelper.GetObjectFromJson <List <Item> >(HttpContext.Session, "cart");
                int         index = DoesExist(id);
                if (index != -1)
                {
                    cart[index].Quantity++;
                }
                else
                {
                    cart.Add(new Item {
                        Dishe = dManager.GetDishe(id), Quantity = 1
                    });
                }
                SessionHelper.SetObjectAsJson(HttpContext.Session, "cart", cart);
            }
            //Redirect to the cart
            return(RedirectToAction("Index", "Cart"));
        }