public async Task <IActionResult> AddToFavoritesAsync(int id) { Movie movieToAdd = await _movieDAL.GetMovie(id); Favorite newFav = new Favorite(); //grabbing the current list of favorites string userId = User.FindFirst(ClaimTypes.NameIdentifier).Value; List <Favorite> favorites = new List <Favorite>(); favorites = _context.Favorite.Where(x => x.UserId == userId).ToList(); //checking to see if this is a duplicate to prevent duplicate adds foreach (Favorite fav in favorites) { if (fav.Title == movieToAdd.title) { return(RedirectToAction("Favorites")); } } newFav.Title = movieToAdd.title; newFav.ReleaseDate = movieToAdd.release_date; newFav.RunTime = (int)movieToAdd.vote_average; newFav.Overview = movieToAdd.overview; newFav.PosterPath = movieToAdd.poster_path; newFav.UserId = User.FindFirst(ClaimTypes.NameIdentifier).Value; if (ModelState.IsValid) { _context.Favorite.Add(newFav); _context.SaveChanges(); } return(RedirectToAction("Favorites")); }
public IActionResult AddFavoriteMovies(Movie favoriteMovie) // change to movie { FavoriteMovie addedMovie = new FavoriteMovie(); addedMovie.Title = favoriteMovie.Title; addedMovie.Year = favoriteMovie.Year; addedMovie.Genre = favoriteMovie.Genre; addedMovie.Director = favoriteMovie.Director; addedMovie.Actors = favoriteMovie.Actors; addedMovie.Plot = favoriteMovie.Plot; //string id = User.FindFirst(ClaimTypes.NameIdentifier).Value; _context.FavoriteMovie.Add(addedMovie); _context.SaveChanges(); return(RedirectToAction("FavoriteMovieList")); }