Beispiel #1
0
        //[ValidateAntiForgeryToken]
        public IActionResult Customize([Bind("CartItemId,Quantity")] CartItem cartItem, IFormCollection collection)
        {
            if (!ModelState.IsValid)
            {
                TempData["message"] = "The quantity must be greater than 1.";
                return(RedirectToAction(nameof(Customize), cartItem.CartItemId));
            }

            _ingredientService.RemoveIngredients(cartItem.CartItemId);
            foreach (var item in collection.Keys.Where(m => m.StartsWith("ingredient-")))
            {
                var listIngredient    = _context.Ingredients.FirstOrDefault(d => d.IngredientId == Int32.Parse(item.Remove(0, 11)));
                CartItemIngredient di = new CartItemIngredient()
                {
                    CartItemId = cartItem.CartItemId, Ingredient = listIngredient
                };
                _context.CartItemIngredients.Add(di);
                _context.SaveChanges();
            }
            var quantity = cartItem.Quantity;

            cartItem      = _context.CartItems.Where(x => x.CartItemId == cartItem.CartItemId).FirstOrDefault();
            cartItem.Dish = _context.Dishes.FirstOrDefault(x => x.DishId == cartItem.DishId);
            cartItem.Dish.DishIngredients = _context.DishIngredients.Where(x => x.DishId == cartItem.DishId).ToList();
            cartItem.CartItemIngredients  = _context.CartItemIngredients.Where(x => x.CartItem.CartItemId == cartItem.CartItemId).ToList();
            cartItem.Quantity             = quantity;
            foreach (var item in cartItem.Dish.DishIngredients)
            {
                item.Ingredient = _context.Ingredients.Where(x => x.IngredientId == item.IngredientId).FirstOrDefault();
            }
            _context.Update(cartItem);
            _context.SaveChanges();

            return(RedirectToAction("Index", "Home"));
        }
Beispiel #2
0
        public async Task <Cart> AddToExistingCart(HttpContext context, Dish dish)
        {
            var cartId = context.Session.GetInt32("Cart");
            var cart   = _context.Carts.Include(x => x.CartItem).ThenInclude(x => x.CartItemIngredient).FirstOrDefault(x => x.CartId == cartId);

            CartItem cartItem = new CartItem();
            List <CartItemIngredient> cartItemIngredients = new List <CartItemIngredient>();

            foreach (var ing in dish.DishIngredients)
            {
                var cartItemIngredient = new CartItemIngredient()
                {
                    Ingredient              = ing.Ingredient,
                    IngredientId            = ing.IngredientId,
                    CartItem                = cartItem,
                    CartItemId              = cartItem.CartItemId,
                    Checked                 = true,
                    CartItemIngredientPrice = ing.Ingredient.IngredientPrice
                };
                cartItemIngredients.Add(cartItemIngredient);
            }

            cartItem.Name               = dish.Name;
            cartItem.Price              = dish.Price;
            cartItem.Quantity           = 1;
            cartItem.CartItemIngredient = cartItemIngredients;
            cartItem.CartId             = cart.CartId;
            cartItem.DishId             = dish.DishId;
            cart.CartItem.Add(cartItem);
            await _context.CartItems.AddAsync(cartItem);

            await _context.SaveChangesAsync();

            return(cart);
        }
        public void AddToCart(Dish dish, int quantity)
        {
            var cart = GetCart();
            var item = new CartItem
            {
                Dish   = dish,
                DishId = dish.DishId,
                CartId = cart.CartId,
                CartItemIngredients = new List <CartItemIngredient>(),
                Price        = dish.Price,
                Quantity     = quantity,
                CartItemName = dish.DishName
            };

            foreach (var dishIngredient in dish.DishIngredients)
            {
                var cartItemIngredient = new CartItemIngredient
                {
                    IngredientName       = dishIngredient.Ingredient.IngredientName,
                    Price                = 0,
                    CartItem             = item,
                    IsOriginalIngredient = true
                };
                item.CartItemIngredients.Add(cartItemIngredient);
                _context.CartItemIngredients.Add(cartItemIngredient);
            }
            _context.CartItems.Add(item);
            _context.SaveChanges();
            cart.CartItems.Add(item);
            SaveCart(cart);
        }
        public async Task <IActionResult> EditCartItem(int id, [Bind("CartItemId, DishId")] CartItem cartItem, IFormCollection form)
        {
            if (id != cartItem.CartItemId)
            {
                return(NotFound());
            }

            if (ModelState.IsValid)
            {
                try
                {
                    var itemToEdit = await _context.CartItems
                                     .Include(ci => ci.CartItemIngredients)
                                     .ThenInclude(cii => cii.Ingredient)
                                     .Include(d => d.Dish)
                                     .ThenInclude(di => di.DishIngredients)
                                     .ThenInclude(dii => dii.Ingredient)
                                     .SingleOrDefaultAsync(m => m.CartItemId == id);

                    foreach (var cartItemIngredient in itemToEdit.CartItemIngredients)
                    {
                        _context.Remove(cartItemIngredient);
                    }
                    await _context.SaveChangesAsync();

                    itemToEdit.CartItemIngredients = new List <CartItemIngredient>();

                    foreach (var ingredient in _ingredientService.GetIngredients())
                    {
                        var cartItemIngredient = new CartItemIngredient
                        {
                            Ingredient = ingredient,
                            Enabled    = form.Keys.Any(x => x == $"checkboxes-{ingredient.IngredientId}"),
                            //Price = ingredient.Price
                        };
                        itemToEdit.CartItemIngredients.Add(cartItemIngredient);
                    }

                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!CartExists(cartItem.CartId))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                return(RedirectToAction(nameof(CartIndex)));
            }
            return(View(cartItem));
        }
Beispiel #5
0
        public IActionResult Edit(int id, IFormCollection formCollection, [Bind("CartItemId, DishId")] CartItem cartItem)
        {
            if (id != cartItem.CartItemId)
            {
                return(NotFound());
            }

            if (ModelState.IsValid)
            {
                try
                {
                    var itemToEdit = _context.CartItems
                                     .Include(ci => ci.CartItemIngredients)
                                     .ThenInclude(cii => cii.Ingredient)
                                     .Include(d => d.Dish)
                                     .ThenInclude(di => di.DishIngredients)
                                     .ThenInclude(dii => dii.Ingredient)
                                     .SingleOrDefault(m => m.CartItemId == id);

                    foreach (var cartItemIngredient in itemToEdit.CartItemIngredients)
                    {
                        _context.Remove(cartItemIngredient);
                    }
                    _context.SaveChangesAsync();
                    itemToEdit.CartItemIngredients = new List <CartItemIngredient>();

                    foreach (var ingredient in _ingredientService.GetIngredients())
                    {
                        var cartItemIngredient = new CartItemIngredient
                        {
                            Ingredient = ingredient,
                            Enabled    = formCollection.Keys.Any(x => x == $"IngredientBox-{ingredient.IngredientId}"),
                            CartItemIngredientPrice = ingredient.Price
                        };
                        itemToEdit.CartItemIngredients.Add(cartItemIngredient);
                    }

                    _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!CartExists(cartItem.CartId))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
            }
            return(View("Index"));
        }
Beispiel #6
0
        public async Task <EditCartViewModel> EditCartItemIngredients(EditCartViewModel model)
        {
            foreach (var cii in _context.CartItemIngredients.Where(x => x.CartItemId == model.CartItemId))
            {
                _context.Remove(cii);
            }
            await _context.SaveChangesAsync();

            List <Ingredient> checkedIngredients = _context.Ingredients.Where(i => model.CartItemIngredients.Where(y => y.Checked)
                                                                              .Any(x => x.IngredientId == i.IngredientId)).ToList();

            //foreach (var ing in model.CartItemIngredients)
            //{
            //    foreach (var cIng in checkedIngredients.Where(x => x.IngredientId == ing.IngredientId))
            //    {
            //        cIng.IngredientPrice = ing.CartItemIngredientPrice;
            //    }
            //}

            var cartItem = await _context.CartItems.Include(x => x.CartItemIngredient).FirstOrDefaultAsync(x => x.CartItemId == model.CartItemId);

            cartItem.Price = model.CartItem.DishOriginalPrice;
            var price = 0;

            foreach (var ing in checkedIngredients)
            {
                var cartItemIngredient = new CartItemIngredient()
                {
                    Ingredient   = ing,
                    IngredientId = ing.IngredientId,
                    CartItem     = model.CartItem,
                    CartItemId   = model.CartItemId,
                };
                if (await OriginalDishHasIngredients(cartItem.DishId, ing.IngredientId))
                {
                    cartItemIngredient.CartItemIngredientPrice = 0;
                }
                else
                {
                    cartItemIngredient.CartItemIngredientPrice = ing.IngredientPrice;
                    price += ing.IngredientPrice;
                }

                _context.CartItemIngredients.Add(cartItemIngredient);
            }
            cartItem.Price += price;
            await _context.SaveChangesAsync();

            return(model);
        }
Beispiel #7
0
        public void TotalPriceFor_CartItemIngredients_ForSpecificCartItem()
        {
            var dish = new Dish {
                Name = "Capricciosa", DishId = 1, Price = 80, DishIngredients = new List <DishIngredient>()
            };
            var ingredient1 = new Ingredient {
                Name = "Bacon", IngredientId = 1, Price = 10
            };
            var ingredient2 = new Ingredient {
                Name = "Cheese", IngredientId = 2, Price = 5
            };
            var ingredient3 = new Ingredient {
                Name = "Extra", IngredientId = 3, Price = 15
            };
            var dishIngredients1 = new DishIngredient {
                DishId = 1, IngredientId = 1, Dish = dish, Ingredient = ingredient1
            };
            var dishIngredients2 = new DishIngredient {
                DishId = 1, IngredientId = 2, Dish = dish, Ingredient = ingredient2
            };

            dish.DishIngredients.Add(dishIngredients1);
            dish.DishIngredients.Add(dishIngredients2);
            var cartItem = new CartItem {
                CartItemId = 1, Dish = dish, DishId = 1, CartItemIngredients = new List <CartItemIngredient>()
            };
            var cartItemIngredient1 = new CartItemIngredient {
                CartItemId = 1, CartItem = cartItem, IngredientId = 1, Ingredient = ingredient1
            };
            var cartItemIngredient2 = new CartItemIngredient {
                CartItemId = 2, CartItem = cartItem, IngredientId = 2, Ingredient = ingredient2
            };
            var cartItemIngredient3 = new CartItemIngredient {
                CartItemId = 3, CartItem = cartItem, IngredientId = 3, Ingredient = ingredient3
            };

            cartItem.CartItemIngredients.Add(cartItemIngredient1);
            cartItem.CartItemIngredients.Add(cartItemIngredient2);
            cartItem.CartItemIngredients.Add(cartItemIngredient3);


            var calculationService = new CartCalculationService();
            var totalPrice         = calculationService.TotalPriceForCartItemIngredients(cartItem);

            Assert.Equal(15, totalPrice);
        }
        public List <CartItem> AddDishToNewCart(int dishId, HttpContext httpContext)
        {
            int cartId;
            var dish = _context.Dishes.Include(x => x.DishIngredients)
                       .ThenInclude(i => i.Ingredient)
                       .SingleOrDefault(d => d.DishId == dishId);

            CartItem cartItem = new CartItem();
            List <CartItemIngredient> cartItemIngredient = new List <CartItemIngredient>();

            foreach (var item in dish.DishIngredients)
            {
                var newCartItemIngredient = new CartItemIngredient
                {
                    CartItem                = cartItem,
                    Ingredient              = item.Ingredient,
                    IngredientId            = item.IngredientId,
                    CartItemIngredientPrice = item.Ingredient.Price
                };

                cartItemIngredient.Add(newCartItemIngredient);
            }

            List <CartItem> listOfCartItems = new List <CartItem>
            {
                cartItem
            };

            Cart newCart = new Cart();

            cartItem.Dish               = dish;
            cartItem.CartId             = newCart.CartId;
            cartItem.CartItemIngredient = cartItemIngredient;
            cartItem.Quantity           = 1;

            newCart.CartItem = listOfCartItems;

            _context.Carts.Add(newCart);
            _context.SaveChanges();

            cartId = newCart.CartId;
            httpContext.Session.SetInt32("Cart", cartId);

            return(newCart.CartItem);
        }
        private IEnumerable <CartItemIngredient> GetIngredients()
        {
            for (int i = 0; i < IngredientsGrid.RowCount; i++)
            {
                var row        = IngredientsGrid.Rows[i];
                var isSelected = Convert.ToBoolean(row.Cells["chk"].Value);
                if (!isSelected)
                {
                    continue;
                }
                var ingredient = new CartItemIngredient
                {
                    IngId = (int?)row.Cells["IngId"].Value,
                    Name  = row.Cells["IName"].Value.ToString(),
                    Price = (double)row.Cells["Price"].Value
                };

                yield return(ingredient);
            }
        }
        public List <CartItem> AddDishToExistingCart(int dishId, HttpContext httpContext)
        {
            int cartId;
            var dish = _context.Dishes.Include(x => x.DishIngredients)
                       .ThenInclude(i => i.Ingredient)
                       .SingleOrDefault(d => d.DishId == dishId);

            CartItem cartItem = new CartItem();
            List <CartItemIngredient> cartItemIngredient = new List <CartItemIngredient>();

            cartId = (int)httpContext.Session.GetInt32("Cart");

            var currentCart = _context.Carts.Include(i => i.CartItem)
                              .ThenInclude(d => d.Dish)
                              .SingleOrDefault(x => x.CartId == cartId);


            foreach (var item in dish.DishIngredients)
            {
                var newCartItemIngredient = new CartItemIngredient
                {
                    CartItem                = cartItem,
                    Ingredient              = item.Ingredient,
                    IngredientId            = item.IngredientId,
                    CartItemIngredientPrice = item.Ingredient.Price
                };
                cartItemIngredient.Add(newCartItemIngredient);
            }
            var newDish = new CartItem
            {
                CartId             = currentCart.CartId,
                Dish               = dish,
                Quantity           = 1,
                CartItemIngredient = cartItemIngredient
            };

            _context.CartItems.Add(newDish);
            _context.SaveChangesAsync();

            return(currentCart.CartItem);
        }
Beispiel #11
0
        public async Task <Cart> NewCart(Dish dish)
        {
            var userCart            = new Cart();
            var cartItem            = new CartItem();
            var cartItemIngredients = new List <CartItemIngredient>();

            userCart.CartItem = new List <CartItem>();

            foreach (var ing in dish.DishIngredients)
            {
                var cartItemIngredient = new CartItemIngredient()
                {
                    Ingredient              = ing.Ingredient,
                    IngredientId            = ing.IngredientId,
                    CartItem                = cartItem,
                    Checked                 = true,
                    CartItemId              = cartItem.CartItemId,
                    CartItemIngredientPrice = ing.Ingredient.IngredientPrice
                };
                cartItemIngredients.Add(cartItemIngredient);
            }

            cartItem.Name               = dish.Name;
            cartItem.Price              = dish.Price;
            cartItem.Quantity           = 1;
            cartItem.CartItemIngredient = cartItemIngredients;
            cartItem.CartId             = userCart.CartId;
            cartItem.DishOriginalPrice  = dish.Price;
            cartItem.DishId             = dish.DishId;
            userCart.CartItem.Add(cartItem);
            await _context.CartItems.AddAsync(cartItem);

            await _context.Carts.AddAsync(userCart);

            await _context.SaveChangesAsync();

            var cartId = userCart.CartId;

            return(userCart);
        }
        public void EditDishIngredientsInCartPost(DishIngredientVM dishIngredientVM, HttpContext httpContext)
        {
            var cartId = httpContext.Session.GetInt32("Cart");

            var cartItem = _context.CartItems
                           .Include(x => x.Dish)
                           .Include(i => i.CartItemIngredient)
                           .ThenInclude(s => s.Ingredient)
                           .SingleOrDefault(x => x.CartId == cartId && x.Dish.DishId == dishIngredientVM.DishId && x.CartItemId == dishIngredientVM.CartItemId);

            var existingIngredients = cartItem.CartItemIngredient.Select(x => x.IngredientId).ToList();

            var SelectedIds = dishIngredientVM.SelectedIngredients.Where(x => x.Enabled == true).Select(s => s.Id).ToList();

            var unCheckedIds = dishIngredientVM.SelectedIngredients.Where(x => x.Enabled == false).Select(s => s.Id).ToList();


            var ingredientsToAdd = _context.Ingredients.Where(x => SelectedIds.Contains(x.IngredientId)).ToList();

            var ingredientsToRemove = _context.Ingredients.Where(x => unCheckedIds.Contains(x.IngredientId)).ToList();


            cartItem.CartItemIngredient.ForEach(i => _context.Remove(i));

            _context.SaveChangesAsync();
            foreach (var item in ingredientsToAdd)
            {
                var newIngredient = new CartItemIngredient
                {
                    CartItem                = cartItem,
                    Ingredient              = item,
                    CartItemId              = cartItem.CartItemId,
                    IngredientId            = item.IngredientId,
                    CartItemIngredientPrice = item.Price
                };
                cartItem.CartItemIngredient.Add(newIngredient);
            }
            _context.SaveChangesAsync();
        }
        // GET: Carts/ItemToCart/5
        //[HttpGet]
        public async Task <IActionResult> ItemToCart(int?id)
        {
            if (id == null)
            {
                return(NotFound());
            }

            var dish = await _context.Dishes
                       .Include(d => d.DishIngredients)
                       .ThenInclude(di => di.Ingredient)
                       .FirstOrDefaultAsync(m => m.DishId == id);

            var cartID = HttpContext.Session.GetInt32("CartSession");

            Cart cart;

            if (HttpContext.Session.GetInt32("CartSession") == null)
            {
                cart = new Cart
                {
                    CartItems = new List <CartItem>()
                };

                _context.Cart.Add(cart);
                await _context.SaveChangesAsync();

                HttpContext.Session.SetInt32("CartSession", cart.CartId);
                cartID = cart.CartId;
            }

            cart = _context.Cart
                   .Include(c => c.CartItems)
                   .ThenInclude(ci => ci.Dish)
                   .ThenInclude(d => d.DishIngredients)
                   .SingleOrDefault(c => c.CartId == cartID);

            CartItem cartItem = new CartItem
            {
                CartId = cart.CartId,
                Dish   = dish,
                DishId = dish.DishId,
                Cart   = cart,
                CartItemIngredients = new List <CartItemIngredient>(),
                Quantity            = 1
            };

            foreach (var item in dish.DishIngredients.Where(di => di.checkboxAnswer))
            {
                var cartItemIngredient = new CartItemIngredient
                {
                    Enabled    = item.checkboxAnswer,
                    Ingredient = item.Ingredient,
                    CartItem   = cartItem
                };
                cartItem.CartItemIngredients.Add(cartItemIngredient);
            }

            cart.CartItems.Add(cartItem);

            _context.SaveChanges();

            return(RedirectToAction("Index", "Dishes"));
        }
Beispiel #14
0
        public List <CartItem> AddToCart(int dishId, HttpContext httpContext)
        {
            if (dishId == null)
            {
                //Gör något här!

                //return NotFound();
            }

            var dish = _context.Dishes
                       .Include(d => d.DishIngredients)
                       .ThenInclude(di => di.Ingredient)
                       .FirstOrDefault(m => m.DishId == dishId);

            var cartID = httpContext.Session.GetInt32("CartSession");

            Cart cart;

            if (httpContext.Session.GetInt32("CartSession") == null)
            {
                cart = new Cart
                {
                    Items = new List <CartItem>()
                };

                _context.Carts.Add(cart);
                _context.SaveChanges();
                httpContext.Session.SetInt32("CartSession", cart.CartId);
                cartID = cart.CartId;
            }

            cart = _context.Carts.Include(c => c.Items)
                   .ThenInclude(ci => ci.Dish)
                   .ThenInclude(d => d.DishIngredients)
                   .SingleOrDefault(c => c.CartId == cartID);

            CartItem cartItem = new CartItem
            {
                Dish = dish,
                Cart = cart,
                CartItemIngredients = new List <CartItemIngredient>(),
                Quantity            = 1
            };

            foreach (var item in dish.DishIngredients)
            {
                var cartItemIngredient = new CartItemIngredient
                {
                    Ingredient = item.Ingredient,
                    CartItem   = cartItem,
                    Enabled    = true,
                    CartItemIngredientPrice = item.Ingredient.Price
                };
                cartItem.CartItemIngredients.Add(cartItemIngredient);
            }
            cart.Items.Add(cartItem);

            _context.SaveChanges();

            return(cart.Items);
        }
Beispiel #15
0
        public async Task <IActionResult> EditCartItemIngredients(int cartItemId)
        {
            var viewModel = new EditCartViewModel();
            //var cartId = (int)HttpContext.Session.GetInt32("Cart");
            var cartItem = await _context.CartItems.Include(x => x.CartItemIngredient).FirstOrDefaultAsync(x => x.CartItemId == cartItemId);

            var dish = await _context.Dishes.Include(x => x.DishIngredients).FirstOrDefaultAsync(x => x.DishId == cartItem.DishId);

            cartItem.DishOriginalPrice = dish.Price;
            viewModel.CartItem         = cartItem;
            viewModel.AllIngredients   = _context.Ingredients.OrderBy(x => x.Name).ToList();
            var cartItemIngredients = new List <CartItemIngredient>();

            foreach (var aIng in viewModel.AllIngredients)
            {
                var cartItemIngredient = new CartItemIngredient();

                if (cartItem.CartItemIngredient.Any(x => x.IngredientId == aIng.IngredientId))
                {
                    cartItemIngredient.Checked = true;
                    cartItemIngredient.CartItemIngredientPrice = 0;
                    cartItemIngredient.Ingredient   = aIng;
                    cartItemIngredient.CartItem     = cartItem;
                    cartItemIngredient.IngredientId = aIng.IngredientId;
                    cartItemIngredient.CartItemId   = cartItem.CartItemId;
                }
                else
                {
                    cartItemIngredient.CartItemIngredientPrice = aIng.IngredientPrice;
                    cartItemIngredient.Checked      = false;
                    cartItemIngredient.Ingredient   = aIng;
                    cartItemIngredient.CartItem     = cartItem;
                    cartItemIngredient.IngredientId = aIng.IngredientId;
                    cartItemIngredient.CartItemId   = cartItem.CartItemId;
                }
                //foreach (var ing in cartItem.CartItemIngredient)
                //{
                //    if ()
                //    {
                //        cartItemIngredient.Checked = true;
                //        cartItemIngredient.CartItemIngredientPrice = 0;
                //        cartItemIngredient.Ingredient = aIng;
                //        cartItemIngredient.CartItem = cartItem;
                //        cartItemIngredient.IngredientId = aIng.IngredientId;
                //        cartItemIngredient.CartItemId = cartItem.CartItemId;
                //    }
                //    else
                //    {
                //    cartItemIngredient.CartItemIngredientPrice = aIng.IngredientPrice;
                //    cartItemIngredient.Checked = false;
                //    cartItemIngredient.Ingredient = aIng;
                //    cartItemIngredient.CartItem = cartItem;
                //    cartItemIngredient.IngredientId = aIng.IngredientId;
                //    cartItemIngredient.CartItemId = cartItem.CartItemId;
                //    }
                //}
                cartItemIngredients.Add(cartItemIngredient);
            }
            viewModel.CartItemIngredients = cartItemIngredients;
            return(View(viewModel));
        }