Exemple #1
0
        public async Task <IActionResult> Delete(FavoritedRecipeViewModel model)
        {
            if (!User.Identity.IsAuthenticated)
            {
                RedirectToPage("LoginPage");
            }

            var user = await _userManager.GetUserAsync(User);

            var favoritedRecipe = user.FavoritedRecipes.FirstOrDefault(fr => fr.RecipeId == model.RecipeId);

            if (favoritedRecipe != null)
            {
                user.FavoritedRecipes.Remove(favoritedRecipe);

                await _userManager.UpdateAsync(user);
            }

            return(RedirectToAction("Show", "Recipes", new { id = model.RecipeId }));
        }
        public async Task <IActionResult> Delete(FavoritedRecipeViewModel model)
        {
            var recipe = await _recipeService.Get(model.RecipeId);

            if (!User.Identity.IsAuthenticated)
            {
                RedirectToPage("LoginPage");
            }

            var nameIdentifier        = User.FindFirstValue(ClaimTypes.NameIdentifier);
            var applicationUserRecipe = recipe.ApplicationUsersRecipes.FirstOrDefault(aur => aur.RecipeId == model.RecipeId && aur.ApplicationUserId == nameIdentifier);

            if (applicationUserRecipe != null)
            {
                recipe.ApplicationUsersRecipes.Remove(applicationUserRecipe);

                await _recipeService.Update(recipe);
            }

            return(RedirectToAction("Show", "Recipes", new { id = model.RecipeId }));
        }
Exemple #3
0
        public async Task <IActionResult> Create(FavoritedRecipeViewModel model)
        {
            if (!User.Identity.IsAuthenticated)
            {
                RedirectToPage("LoginPage");
            }

            var user = await _userManager.GetUserAsync(User);

            if (user.FavoritedRecipes.All(fr => fr.RecipeId != model.RecipeId))
            {
                var favoritedRecipe = new FavoritedRecipe
                {
                    RecipeId = model.RecipeId
                };

                user.FavoritedRecipes.Add(favoritedRecipe);

                await _userManager.UpdateAsync(user);
            }

            return(RedirectToAction("Show", "Recipes", new { id = model.RecipeId }));
        }
        public async Task <IActionResult> Create(FavoritedRecipeViewModel model)
        {
            var recipe = await _recipeService.Get(model.RecipeId);

            if (!User.Identity.IsAuthenticated)
            {
                RedirectToPage("LoginPage");
            }

            var user = await _userManager.GetUserAsync(User);

            if (user.ApplicationUsersRecipes.All(aur => aur.RecipeId != model.RecipeId))
            {
                recipe.ApplicationUsersRecipes.Add(new ApplicationUserRecipe
                {
                    ApplicationUser = user
                });

                await _recipeService.Update(recipe);
            }

            return(RedirectToAction("Show", "Recipes", new { id = model.RecipeId }));
        }