コード例 #1
0
        public async Task <IActionResult> Edit(int id, [Bind("RecipeFavoriteId,UserDataId,ResultId")] RecipeFavorite recipeFavorite)
        {
            if (id != recipeFavorite.RecipeFavoriteId)
            {
                return(NotFound());
            }

            if (ModelState.IsValid)
            {
                try
                {
                    _context.Update(recipeFavorite);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!RecipeFavoriteExists(recipeFavorite.RecipeFavoriteId))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                return(RedirectToAction(nameof(Index)));
            }
            ViewData["ResultId"]   = new SelectList(_context.Result, "ResultId", "ResultId", recipeFavorite.ResultId);
            ViewData["UserDataId"] = new SelectList(_context.Users, "UserDataID", "UserDataID", recipeFavorite.UserDataId);
            return(View(recipeFavorite));
        }
コード例 #2
0
        public RecipeFavorite GetFavoriteByUserAndRecipe(int userId, int recipeId)
        {
            string queryString = "EXECUTE dbo.GetFavoriteByUserAndRecipe @userId, @recipeId";

            RecipeFavorite result = null;

            using (var conn = new SqlConnection(connstring))
            {
                result = conn.QueryFirst <RecipeFavorite>(queryString, new { userId, recipeId });
            }
            return(result);
        }
コード例 #3
0
        public int AddToFavorites(RecipeFavorite recipeFavorite)
        {
            int result = 0;

            using (var conn = new SqlConnection(connstring))
            {
                string command = "EXECUTE dbo.AddRecipeFavorite @UserId, @RecipeId";

                result = conn.Execute(command, recipeFavorite);
            }
            return(result);
        }
コード例 #4
0
        public async Task <IActionResult> Create([Bind("RecipeFavoriteId,UserDataId,ResultId")] RecipeFavorite recipeFavorite)
        {
            if (ModelState.IsValid)
            {
                _context.Add(recipeFavorite);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            ViewData["ResultId"]   = new SelectList(_context.Result, "ResultId", "ResultId", recipeFavorite.ResultId);
            ViewData["UserDataId"] = new SelectList(_context.Users, "UserDataID", "UserDataID", recipeFavorite.UserDataId);
            return(View(recipeFavorite));
        }
コード例 #5
0
        public int AddToFavorites(RecipeFavorite recipeFavorite)
        {
            //var result = favoriteData.GetFavoriteByUserAndRecipe(recipeFavorite.UserID, recipeFavorite.RecipeID);
            //int rowsAdded = 0;


            //if (result == null)
            //{
            //    rowsAdded = favoriteData.AddToFavorites(recipeFavorite);
            //}
            //return rowsAdded;

            int result = favoriteData.AddToFavorites(recipeFavorite);

            return(result);
        }
コード例 #6
0
        public async Task <IActionResult> AddFavorite(string title, string ing, string pic, string link)
        {
            if (User.Identity.IsAuthenticated)
            {
                var currentUser = await _context.Users.FirstOrDefaultAsync(m => m.UserName == User.Identity.Name);

                if (currentUser == null)
                {
                    var userData = new UserData(User.Identity.Name);
                    _context.Add(userData);
                    await _context.SaveChangesAsync();

                    currentUser = userData;
                }

                var recipe = new Result
                {
                    Title       = title,
                    Ingredients = ing,
                    Href        = link,
                    Thumbnail   = pic
                };
                _context.Add(recipe);
                await _context.SaveChangesAsync();

                recipe = await _context.Result.FirstOrDefaultAsync(m => m.Href == link);

                var favorite = new RecipeFavorite();
                favorite.UserDataId = currentUser.UserDataID;
                favorite.ResultId   = recipe.ResultId;
                _context.Add(favorite);
                await _context.SaveChangesAsync();

                return(RedirectToAction("Index"));
            }
            else
            {
                return(RedirectToAction("Index", "Home"));
            }
        }