示例#1
0
        // GET: Foods
        public async Task <IActionResult> Index()
        {
            var userId   = this.User.FindFirstValue(ClaimTypes.NameIdentifier);
            var homeCook = _context.HomeCook.Where(c => c.IdentityUserId ==
                                                   userId).SingleOrDefault();
            List <SelectListItem> ingredients = new List <SelectListItem>();
            var _ingredients = _context.Ingredients.Select(a => new SelectListItem()
            {
                Text  = a.Name,
                Value = a.Name
            });

            ingredients = _ingredients.OrderBy(a => a.Text).ToList();
            string selectedIngredient      = ingredients.Select(a => a.Value).FirstOrDefault().ToString();
            List <SelectListItem> cuisines = new List <SelectListItem>();
            var _cuisines = _context.Cuisines.Select(a => new SelectListItem()
            {
                Text  = a.Name,
                Value = a.Name
            });

            cuisines = _cuisines.OrderBy(a => a.Text).ToList();
            string selectedCuisine = cuisines.Select(a => a.Value).FirstOrDefault().ToString();
            var    meals           = await _recipeByIngredientAndCuisineRequest.GetRecipesByIngredientAndCuisine(selectedIngredient, selectedCuisine);

            if (_context.Ingredients.Contains(null))
            {
                IngredientIO iO = new IngredientIO(_context);
                iO.ReadFile();
            }
            FoodViewModel foodViewModel = new FoodViewModel()
            {
                Meals       = meals,
                Ingredients = ingredients,
                Cuisines    = cuisines,
                HomeCook    = homeCook
            };

            return(View(foodViewModel));
        }
示例#2
0
        public async Task <IActionResult> Cart(string id)
        {
            var userId   = this.User.FindFirstValue(ClaimTypes.NameIdentifier);
            var homeCook = _context.HomeCook.Where(c => c.IdentityUserId ==
                                                   userId).SingleOrDefault();
            RecipeInfo recipeInfo = await _recipeInfoRequest.GetRecipeInfo(id);

            string items = "";

            for (int i = 0; i < recipeInfo.extendedIngredients.Length; i++)
            {
                items += recipeInfo.extendedIngredients[i].originalString + " , ";
            }
            ShoppingCart cart = _context.ShoppingCart.Where(a => a.HomeCookId == homeCook.HomeCookId).FirstOrDefault();

            if (cart == null)
            {
                cart = new ShoppingCart()
                {
                    Items    = items,
                    HomeCook = homeCook
                };
                _context.ShoppingCart.Add(cart);
            }
            else
            {
                cart.Items   += items;
                cart.HomeCook = homeCook;
                _context.ShoppingCart.Update(cart);
            }
            _context.SaveChanges();
            List <SelectListItem> ingredients = new List <SelectListItem>();
            var _ingredients = _context.Ingredients.Select(a => new SelectListItem()
            {
                Text  = a.Name,
                Value = a.Name
            });

            ingredients = _ingredients.OrderBy(a => a.Text).ToList();
            List <SelectListItem> cuisines = new List <SelectListItem>();
            var _cuisines = _context.Cuisines.Select(a => new SelectListItem()
            {
                Text  = a.Name,
                Value = a.Name
            });

            cuisines = _cuisines.OrderBy(a => a.Text).ToList();
            string selectedIngredient = ingredients.Select(a => a.Value).FirstOrDefault().ToString();
            string selectedCuisine    = cuisines.Select(a => a.Value).FirstOrDefault().ToString();
            var    meals = await _recipeByIngredientAndCuisineRequest.GetRecipesByIngredientAndCuisine(selectedIngredient, selectedCuisine);

            if (_context.Ingredients.Contains(null))
            {
                IngredientIO iO = new IngredientIO(_context);
                iO.ReadFile();
            }
            FoodViewModel foodViewModel = new FoodViewModel()
            {
                Meals       = meals,
                Ingredients = ingredients,
                Cuisines    = cuisines,
                HomeCook    = homeCook
            };

            return(View("Index", foodViewModel));
        }