public ActionResult AddFavorite(string movie) { if (movie != null) { //Create favorite database object based on model FavoriteMovy movy = new FavoriteMovy(); movy.ImdbId = movie.Trim(); movy.UserId = User.Identity.GetUserId(); //Make sure user is logged in. if (movy.UserId == null) { return(RedirectToAction(nameof(AccountController.Login), "Account")); } //Find movie in favorites database List <FavoriteMovy> list = db.FavoriteMovies.Where(x => x.ImdbId == movy.ImdbId).ToList(); //Check count if it is zero than the movie isn't in the favorites database. if (list.Count == 0) { db.FavoriteMovies.Add(movy); db.SaveChanges(); } //Show favorites list return(RedirectToAction("FavoriteList")); } else { return(RedirectToAction("Index")); } }
public ActionResult RemoveFavorite(string id) { //Make sure parameter was supplied. If parameter is an int it will error out if not parameter is provided in url. if (id != null) { //If we can parse the string to an id if (int.TryParse(id, out int movieId)) { //Find movie in the favorites database and store in Favorite Movy object FavoriteMovy movy = db.FavoriteMovies.Find(movieId); //Check if we found a movie in the favorites database before trying to remove if (movy != null) { //Remove the movie from the favorites database db.FavoriteMovies.Remove(movy); db.SaveChanges(); } //Show favorites list return(RedirectToAction("FavoriteList")); } else { //No id send back to index return(RedirectToAction("Index")); } } else { //id is null send back to index return(RedirectToAction("Index")); } }