Example #1
0
        public JsonResult AddToCart(CartEntryViewModel entry)
        {
            MongoDBDataProvider provider = new MongoDBDataProvider();

            if (Session["Cart"] == null)
            {
                Session["Cart"] = new List <OrderedDish>();
            }

            OrderedDish orderedDish = new OrderedDish();
            Category    tmpCat      = provider.CategoryGet(entry.category);

            orderedDish.price    = 0;
            orderedDish.dish     = tmpCat.dishes.Where(x => x.name == entry.dish).SingleOrDefault();
            orderedDish.quantity = entry.quantity;
            if (entry.supplements == null)
            {
                orderedDish.supplements = null;
            }
            else
            {
                orderedDish.supplements = new List <Supplement>();
                foreach (string supName in entry.supplements)
                {
                    foreach (Supplement catSup in tmpCat.supplements)
                    {
                        if (supName == catSup.name)
                        {
                            Supplement tmpSup = new Supplement();
                            tmpSup.name  = catSup.name;
                            tmpSup.price = catSup.price;
                            orderedDish.supplements.Add(tmpSup);
                            orderedDish.price += tmpSup.price;
                        }
                    }
                }
            }

            orderedDish.price += orderedDish.dish.price * orderedDish.quantity;
            List <OrderedDish> cart = (List <OrderedDish>)Session["Cart"];

            cart.Add(orderedDish);
            return(Json(cart));
        }