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 }); }
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")); }
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)); }
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)); }
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); } }
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); }
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()); } }
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 })); }
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)); }
public bool IsFavoriteRecipeAdded(FavoriteRecipe favoriteRecipe) { return(_context.FavoriteRecipes .Where(f => f.RecipeId == favoriteRecipe.RecipeId && f.UserId.Equals(favoriteRecipe.UserId)) .Any()); }
public void DeleteFavoriteRecipe(FavoriteRecipe favoriteRecipe) { _context.FavoriteRecipes.Remove(favoriteRecipe); _context.SaveChanges(); }
public void PostFavoriteRecipe(FavoriteRecipe favoriteRecipe) { _context.FavoriteRecipes.Add(favoriteRecipe); _context.SaveChanges(); _context.Entry(favoriteRecipe).State = EntityState.Detached; }