public ActionResult Create(MovieReViewModel movieModel)
 {
     movieModel.MovieReview.ApplicationUserId = User.Identity.GetUserId();
     movieModel.MovieReview.ReviewDate = DateTime.Now;
     if (ModelState.IsValid)
     {
         var alreadyExists = db.movieReviews.Where(
             x => x.MovieTitle == movieModel.MovieReview.MovieTitle
             && x.ApplicationUserId == movieModel.MovieReview.ApplicationUserId).ToList();
         if (alreadyExists.Count == 0 || alreadyExists == null)
         {
             db.movieReviews.Add(movieModel.MovieReview);
             db.SaveChanges();
             return RedirectToAction("Index");
         }
         else
         {
             movieModel.Errors = new Dictionary<string, string>();
             movieModel.Errors.Add("CreateMovieReview", "You've already rated this movie. Try a different title.");
             movieModel.Reviews = new List<SelectListItem>();
             movieModel.Reviews = GetReviews();
             return View(movieModel);
         }
     }
     return View(movieModel);
 }
        public ActionResult Edit(MovieReViewModel movieModel)
        {
            if (ModelState.IsValid)
            {
                var userId = User.Identity.GetUserId();
                var alreadyExists = db.movieReviews.Where(
                    x => x.MovieTitle == movieModel.MovieReview.MovieTitle
                    && x.ApplicationUserId == userId).ToList();
                if (alreadyExists.Count > 0 && alreadyExists.First().ApplicationUserId != userId)
                {
                    return RedirectToAction("Index");
                }
                if ((alreadyExists.Count > 0 && alreadyExists.First().Id == movieModel.MovieReview.Id)
                    || alreadyExists.Count ==0)
                {
                    var updated = db.movieReviews.Find(movieModel.MovieReview.Id);
                    updated.MovieTitle = movieModel.MovieReview.MovieTitle;
                    updated.RatingId = movieModel.MovieReview.RatingId;
                    updated.ReviewDate = DateTime.Now;

                    db.Entry(updated).State = EntityState.Modified;
                    db.SaveChanges();
                }
                else
                {
                    movieModel.Errors = new Dictionary<string, string>();
                    movieModel.Errors.Add("EditMovieReview", "You've already rated this movie. Try a different title.");
                    movieModel.Reviews = GetReviews();
                    return View(movieModel);
                }
                return RedirectToAction("Index");
            }

            return View(movieModel);
        }
 // GET: MovieReviews/Create
 public ActionResult Create()
 {
     var movieModel = new MovieReViewModel();
     movieModel.Reviews = GetReviews();
     return View(movieModel);
 }
        // GET: MovieReviews/Edit/5
        public ActionResult Edit(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            var movieReview = db.movieReviews.Find(id);
            if(movieReview.ApplicationUserId != User.Identity.GetUserId())
            {
                return RedirectToAction("Index");
            }
            var movieModel = new MovieReViewModel();
            movieModel.Reviews = GetReviews();
            movieModel.MovieReview = movieReview;

            if (movieModel.MovieReview == null)
            {
                ViewBag.Message = "We couldn't find what you were looking for. <br />"
                    + " Click <a href=\" / MovieReviews\">here to go back to your movie ratings</a>.";
                return View("Info");
            }
            return View(movieModel);
        }