示例#1
0
        public async Task <Response <string> > SetFavoriteRecipe(Guid userId, int recipeId)
        {
            var favorite = await _context.FavoriteRecipe.FirstOrDefaultAsync(fav => fav.UserId == userId && fav.RecipeId == recipeId);

            if (favorite == null)
            {
                var newFavorite = new FavoriteRecipe()
                {
                    RecipeId = recipeId,
                    UserId   = userId
                };
                await _context.AddAsync(newFavorite);
            }
            else
            {
                _context.Remove(favorite);
            }
            await _context.SaveChangesAsync();

            return(new Response <string>()
            {
                Data = "favorite is updated",
                Success = true
            });
        }
示例#2
0
        public async Task <ActionResult <string> > Create(RecipeIdModel idModel)
        {
            var claimsIdentity = (ClaimsIdentity)User.Identity;

            if (claimsIdentity.FindFirst(ClaimTypes.NameIdentifier) == null)
            {
                throw new System.Exception("User not logged in");
            }

            var currentUserId = claimsIdentity.FindFirst(ClaimTypes.NameIdentifier).Value;

            FavoriteRecipe favoriteRecipe = _unitOfWork.FavoriteRecipe.GetFavoriteRecipeRelatedToUser(currentUserId, idModel.Id);

            if (favoriteRecipe != null)
            {
                _unitOfWork.FavoriteRecipe.Remove(favoriteRecipe.Id);
                _unitOfWork.Save();

                return("Favorite recipe removed!");
            }
            else
            {
                favoriteRecipe = new FavoriteRecipe();

                favoriteRecipe.ApplicationUserId = currentUserId;
                favoriteRecipe.RecipeId          = idModel.Id;

                _unitOfWork.FavoriteRecipe.Add(favoriteRecipe);
                _unitOfWork.Save();

                return("Favorite recipe added!");
            }
        }
        public ActionResult DeleteConfirmed(int id)
        {
            FavoriteRecipe favoriteRecipe = db.FavoriteRecipes.Find(id);

            db.FavoriteRecipes.Remove(favoriteRecipe);
            db.SaveChanges();
            return(RedirectToAction("Index"));
        }
示例#4
0
    public MyResponse DeleteFavoriteRecipe(FavoriteRecipe favoriteRecipe)
    {
        MyResponse validateResult = ValidateDeleteFavoriteRecipe(favoriteRecipe);

        if (validateResult.IsSuccess())
        {
            _recipesRepository.DeleteFavoriteRecipe(favoriteRecipe);
        }
        return(validateResult);
    }
 public ActionResult Edit([Bind(Include = "Id,RecipeId")] FavoriteRecipe favoriteRecipe)
 {
     if (ModelState.IsValid)
     {
         db.Entry(favoriteRecipe).State = EntityState.Modified;
         db.SaveChanges();
         return(RedirectToAction("Index"));
     }
     ViewBag.RecipeId = new SelectList(db.Recipes, "Id", "RecipeTitle", favoriteRecipe.RecipeId);
     return(View(favoriteRecipe));
 }
示例#6
0
    private MyResponse ValidateDeleteFavoriteRecipe(FavoriteRecipe favoriteRecipe)
    {
        MyResponse response = new MyResponse(true);

        if (!_recipesRepository.IsFavoriteRecipeAdded(favoriteRecipe))
        {
            response.Success = false;
            response.Message = "Can't delete - recipe is not added to favorites";
        }
        return(response);
    }
        public ActionResult Create([Bind(Include = "Id,RecipeId")] FavoriteRecipe favoriteRecipe)
        {
            if (ModelState.IsValid)
            {
                db.FavoriteRecipes.Add(favoriteRecipe);
                db.SaveChanges();
                return(RedirectToAction("Index"));
            }

            ViewBag.RecipeId = new SelectList(db.Recipes, "Id", "RecipeTitle", favoriteRecipe.RecipeId);
            return(View(favoriteRecipe));
        }
        // GET: FavoriteRecipes/Details/5
        public ActionResult Details(int?id)
        {
            if (id == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }
            FavoriteRecipe favoriteRecipe = db.FavoriteRecipes.Find(id);

            if (favoriteRecipe == null)
            {
                return(HttpNotFound());
            }
            return(View(favoriteRecipe));
        }
示例#9
0
        public void AddToFavorites(Recipe recipe, ApplicationUser user)
        {
            if (IsFavorited(recipe, user))
            {
                return;
            }

            var favorited = new FavoriteRecipe();

            favorited.RecipeId = recipe.Id;
            favorited.UserId   = user.Id;
            //favorited.CreatedAt = DateTime.Now;
            db.Add(favorited);
            db.SaveChanges();
        }
        // GET: FavoriteRecipes/Edit/5
        public ActionResult Edit(int?id)
        {
            if (id == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }
            FavoriteRecipe favoriteRecipe = db.FavoriteRecipes.Find(id);

            if (favoriteRecipe == null)
            {
                return(HttpNotFound());
            }
            ViewBag.RecipeId = new SelectList(db.Recipes, "Id", "RecipeTitle", favoriteRecipe.RecipeId);
            return(View(favoriteRecipe));
        }
        public bool CreateFavoriteRecipe(FavoriteRecipeCreate model)
        {
            var entity =
                new FavoriteRecipe()
            {
                UserID       = _userId,
                FavoriteList = model.FavoriteList,
                Recipes      = model.Recipes
            };

            using (var ctx = new ApplicationDbContext())
            {
                ctx.FavoriteRecipes.Add(entity);
                return(ctx.SaveChanges() == 1);
            }
        }
示例#12
0
    private MyResponse ValidatePostFavoriteRecipe(FavoriteRecipe favoriteRecipe)
    {
        MyResponse response = new MyResponse(false);

        if (GetRecipe(favoriteRecipe.RecipeId) == null)
        {
            response.Message = "Recipe with id = " + favoriteRecipe.RecipeId + " doesn't exist";
        }
        else if (_recipesRepository.IsFavoriteRecipeAdded(favoriteRecipe))
        {
            response.Message = "Recipe is already added to favorites";
        }
        else
        {
            response.Success = true;
        }
        return(response);
    }
示例#13
0
        public async Task <ActionResult> AddFavorite(string label, string url, string image)
        {
            try
            {
                var user = await GetUserAsync();

                var userFavoriteRecipes = await _context.UserFavoriteRecipes
                                          .Where(ufr => ufr.UserId == user.Id)
                                          .Include(fr => fr.FavoriteRecipe)
                                          .ToListAsync();

                var matchingRecipe = userFavoriteRecipes.FirstOrDefault(ufr => ufr.FavoriteRecipe.Label == label);

                if (matchingRecipe == null)
                {
                    var newFavoriteRecipe = new FavoriteRecipe()
                    {
                        Label = label,
                        Image = image,
                        Url   = url
                    };

                    _context.FavoriteRecipes.Add(newFavoriteRecipe);
                    await _context.SaveChangesAsync();

                    var newUserFavRecipe = new UserFavoriteRecipe()
                    {
                        UserId           = user.Id,
                        FavoriteRecipeId = newFavoriteRecipe.Id
                    };

                    _context.UserFavoriteRecipes.Add(newUserFavRecipe);
                    await _context.SaveChangesAsync();
                }


                return(RedirectToAction(nameof(Favorites)));
            }
            catch
            {
                return(View());
            }
        }
示例#14
0
        public IActionResult AddToFavorites(RecipeViewModel recipeViewModel)
        {
            var claimsIdentity = (ClaimsIdentity)User.Identity;
            var currentUserId  = claimsIdentity.FindFirst(ClaimTypes.NameIdentifier).Value;

            FavoriteRecipe favoriteRecipe = _unitOfWork.FavoriteRecipe.GetFavoriteRecipeRelatedToUser(currentUserId, recipeViewModel.FavoriteRecipe.RecipeId);

            if (favoriteRecipe != null)
            {
                _unitOfWork.FavoriteRecipe.Remove(favoriteRecipe.Id);
                _unitOfWork.Save();
                return(RedirectToAction("Details", new { id = recipeViewModel.Comment.RecipeId }));
            }

            recipeViewModel.FavoriteRecipe.ApplicationUserId = currentUserId;
            _unitOfWork.FavoriteRecipe.Add(recipeViewModel.FavoriteRecipe);
            _unitOfWork.Save();

            return(RedirectToAction("Details", new { id = recipeViewModel.Comment.RecipeId }));
        }
示例#15
0
        public async Task <IActionResult> OnPostFavorite(string recipeId)
        {
            FavoriteRecipe favoriteRecipe = new FavoriteRecipe
            {
                User      = await _db.Users.Where(u => u.Id == long.Parse(_userManager.GetUserId(User))).SingleOrDefaultAsync(),
                Recipe    = await _db.Recipes.Where(r => r.Id == long.Parse(recipeId)).SingleOrDefaultAsync(),
                AddedDate = DateTime.Now
            };

            FavoriteRecipe recipe = _db.FavoriteRecipes.Where(fr => fr.Recipe == favoriteRecipe.Recipe).Where(fr => fr.User == favoriteRecipe.User).SingleOrDefault();

            if (recipe != null)
            {
                _db.FavoriteRecipes.Remove(recipe);
            }
            else
            {
                _db.FavoriteRecipes.Add(favoriteRecipe);
            }
            await _db.SaveChangesAsync();

            return(new JsonResult(true));
        }
示例#16
0
 public bool IsFavoriteRecipeAdded(FavoriteRecipe favoriteRecipe)
 {
     return(_context.FavoriteRecipes
            .Where(f => f.RecipeId == favoriteRecipe.RecipeId && f.UserId.Equals(favoriteRecipe.UserId))
            .Any());
 }
示例#17
0
 public void DeleteFavoriteRecipe(FavoriteRecipe favoriteRecipe)
 {
     _context.FavoriteRecipes.Remove(favoriteRecipe);
     _context.SaveChanges();
 }
示例#18
0
 public void PostFavoriteRecipe(FavoriteRecipe favoriteRecipe)
 {
     _context.FavoriteRecipes.Add(favoriteRecipe);
     _context.SaveChanges();
     _context.Entry(favoriteRecipe).State = EntityState.Detached;
 }