/// <summary>
        /// Dodaje przepis do ulubionych aktualnie zalogowanego użytkownika
        /// </summary>
        /// <param name="id">Id przepisu</param>
        /// <returns>Kod odpowiedzi</returns>
        public async Task <int> AddRecipeToFavorite(int id)
        {
            string userId = UserHelper.GetCurrentUserId();

            if (userId != null)
            {
                var favoriteRecipeUser = await _db.UserFavouriteRecipe.SingleOrDefaultAsync(x => x.UserId == userId && x.RecipeId == id);

                if (favoriteRecipeUser == null)
                {
                    UserFavouriteRecipe favoriteRecipe = new UserFavouriteRecipe();
                    favoriteRecipe.UserId   = userId;
                    favoriteRecipe.RecipeId = id;

                    _db.UserFavouriteRecipe.Add(favoriteRecipe);
                    await _db.SaveChangesAsync();

                    return(1);
                }

                return(2);
            }

            return(3);
        }
Esempio n. 2
0
        public async Task <bool> FavUnfav(string id, string userId)
        {
            Recipe recipeFd = await recipeRepo.All().Include(x => x.RecipeFavorisers).FirstOrDefaultAsync(r => r.Id == id);

            if (recipeFd is null)
            {
                throw new ArgumentException("Recipe with given id not found in database!");
            }

            UserFavouriteRecipe recipeFavFd = recipeFd.RecipeFavorisers.FirstOrDefault(f => f.UserId == userId);

            if (recipeFavFd is null)
            {
                recipeFd.RecipeFavorisers.Add(new UserFavouriteRecipe {
                    UserId = userId
                });
                await recipeRepo.SaveChangesAsync();

                return(true);
            }
            recipeFavFd.DateOfLastEdit = DateTime.UtcNow;
            recipeFavFd.IsDeleted      = !recipeFavFd.IsDeleted;
            await recipeRepo.SaveChangesAsync();

            return(!recipeFavFd.IsDeleted);
        }
        public ActionResult Delete(
            [Bind("UserId, RecipeId")] UserFavouriteRecipe userFavouriteRecipe,
            int?pageNumber)
        {
            _context.Remove(userFavouriteRecipe);
            _context.SaveChanges();

            return(RedirectToAction("Index", "UserFavouriteRecipes", new { pageNumber }));
        }