Example #1
0
        //displaying either blank form is user has not yet saved preferences or table of saved preferences
        public async Task <IActionResult> UserPreferences()
        {
            var userId = User.FindFirstValue(ClaimTypes.NameIdentifier);
            var savedUserPreferences = await _preferencesClient.SavedUserPreferences(userId);

            if (savedUserPreferences == false)
            {
                return(View());
            }
            else
            {
                var retrieveUserPreferences = await _preferencesClient.RetrieveUserPreferences(userId);

                return(View("SavePreferences", retrieveUserPreferences));
            }
        }
        //Return home page
        public async Task <IActionResult> Index()
        {
            var userId             = User.FindFirstValue(ClaimTypes.NameIdentifier);
            var hasUserPreferences = await _preferencesClient.SavedUserPreferences(userId);

            var intolerances = new Intolerances();

            if (User.Identity.IsAuthenticated && hasUserPreferences)
            {
                var userPreferences = await _preferencesClient.RetrieveUserPreferences(userId);

                var intolerenceList = userPreferences.Intolerances.Split(',').ToList();
                if (intolerenceList.Contains("egg"))
                {
                    intolerances.egg = true;
                }
                if (intolerenceList.Contains("dairy"))
                {
                    intolerances.dairy = true;
                }
                if (intolerenceList.Contains("gluton"))
                {
                    intolerances.gluton = true;
                }
                if (intolerenceList.Contains("grain"))
                {
                    intolerances.grain = true;
                }
                if (intolerenceList.Contains("peanut"))
                {
                    intolerances.peanut = true;
                }
                if (intolerenceList.Contains("sesame"))
                {
                    intolerances.sesame = true;
                }
                if (intolerenceList.Contains("seafood"))
                {
                    intolerances.seafood = true;
                }
                if (intolerenceList.Contains("shellfish"))
                {
                    intolerances.shellfish = true;
                }
                if (intolerenceList.Contains("soy"))
                {
                    intolerances.sulfite = true;
                }
                if (intolerenceList.Contains("sulfite"))
                {
                    intolerances.sulfite = true;
                }
                if (intolerenceList.Contains("treeNut"))
                {
                    intolerances.treeNut = true;
                }
                if (intolerenceList.Contains("wheat"))
                {
                    intolerances.wheat = true;
                }
                var recipeList = await _recipeClient.SearchForRecipeByQuery(userPreferences.Diet, intolerances, userPreferences.MaxCalorie, userPreferences.MaxCarb, userPreferences.MaxProtein, userPreferences.MinProtein);

                Random random = new Random();
                if (recipeList == null)
                {
                    TempData["Error"] = "You have used up all the api calls. Please swap out the API key";
                    return(RedirectToAction("Error", "Home"));
                }
                else if (recipeList.Count == 0)
                {
                    ViewBag.RecipeName = "No found recipes based on your preferences";
                    return(View());
                }
                else
                {
                    var randomRecipeNumber = random.Next(recipeList.Count);
                    var randomRecipe       = recipeList.ElementAt(randomRecipeNumber);
                    ViewBag.RecipeName = randomRecipe.Title;
                    ViewBag.ImageURL   = randomRecipe.Image;
                    ViewBag.Id         = randomRecipe.Id;
                    ViewBag.Summary    = randomRecipe.Summary;
                    return(View());
                }
            }
            else
            {
                var recipeList = await _recipeClient.SearchForAllRecipes();

                Random random = new Random();
                if (recipeList == null)
                {
                    ViewBag.Error = "You have used up all the api calls. Please swap out the API key";
                    return(RedirectToAction("Error", "Home"));
                }
                else
                {
                    var randomRecipeNumber = random.Next(recipeList.Count);
                    var randomRecipe       = recipeList.ElementAt(randomRecipeNumber);
                    ViewBag.RecipeName = randomRecipe.Title;
                    ViewBag.ImageURL   = randomRecipe.Image;
                    ViewBag.Id         = randomRecipe.Id;
                    ViewBag.Summary    = randomRecipe.Summary;
                    return(View());
                }
            }
        }