Beispiel #1
0
        public async Task <IActionResult> CooksSaved()
        {
            CookSavedRecipes saveRecipe = new CookSavedRecipes()
            {
                AllRecipes = new List <Recipe>()
            };
            var userId    = this.User.FindFirstValue(ClaimTypes.NameIdentifier);
            var foundCook = await _repo.Cook.FindByCondition(r => r.IdentityUserId == userId).SingleOrDefaultAsync();

            if (foundCook == null)
            {
                return(NotFound());
            }

            var cookSavedRecipes = await _repo.CookSavedRecipes.FindByCondition(s => s.CookId == foundCook.CookId).ToListAsync();

            foreach (CookSavedRecipes savedRecipe in cookSavedRecipes)
            {
                var recipe = await _repo.Recipe.FindByCondition(r => r.RecipeId == savedRecipe.RecipeId).SingleOrDefaultAsync();



                saveRecipe.AllRecipes.Add(recipe);
            }
            return(View(saveRecipe));
        }
Beispiel #2
0
        private void SaveToRepo(Recipe recipe, CookSavedRecipes savedRecipe)
        {
            var userId       = this.User.FindFirstValue(ClaimTypes.NameIdentifier);
            var loggedInCook = _repo.Cook.FindByCondition(e => e.IdentityUserId == userId).SingleOrDefault();

            recipe.CookID = loggedInCook.CookId;

            _repo.CookSavedRecipes.Create(savedRecipe);
            _repo.Save();
        }
Beispiel #3
0
        public async Task <IActionResult> Details(int id)

        {
            var recipe = _repo.Recipe.FindByCondition(r => r.RecipeId == id).FirstOrDefault();

            CookSavedRecipes saveRecipe = new CookSavedRecipes();
            var userId = this.User.FindFirstValue(ClaimTypes.NameIdentifier);

            var foundCook = await _repo.Cook.FindByCondition(r => r.IdentityUserId == userId).SingleOrDefaultAsync();

            var foundRecipe = await _repo.Recipe.FindByCondition(r => r.RecipeId == recipe.RecipeId).FirstOrDefaultAsync();


            if (foundCook == null)
            {
                return(NotFound());
            }
            saveRecipe.CookId   = foundCook.CookId;
            saveRecipe.RecipeId = foundRecipe.RecipeId;

            //If they own it redirect them
            var cookOwnsThisRecipe = _repo.CookSavedRecipes.FindByCondition(s => s.CookId == foundRecipe.CookID).FirstOrDefault();

            if (cookOwnsThisRecipe != null)
            {
                return(RedirectToAction(nameof(Index)));
            }

            try
            {
                var alreadySaved1 = _repo.CookSavedRecipes.FindByCondition(s => s.RecipeId == foundRecipe.RecipeId).ToList();
                foreach (CookSavedRecipes recip in alreadySaved1)
                {
                    if (foundCook.CookId == recip.CookId)
                    {
                        return(RedirectToAction(nameof(CooksSaved)));
                    }
                }
            }
            catch
            {
                SaveToRepo(recipe, saveRecipe);
            }

            SaveToRepo(recipe, saveRecipe);

            return(RedirectToAction(nameof(CooksSaved)));
        }