Exemplo n.º 1
0
        public ActionResult Edit(int? id)
        {
            ReviewModel reviewModel = null;

            if (id != null)
            {
                var reviewer = _reviewRepo.Query().Filter(r => r.Id == id)
                    .Include(r => r.Reviewer)
                    .Include(r => r.Movie)
                    .FirstOrDefault();

                if (reviewer != null)
                {
                    reviewModel = ModelFactoryInstance.Create(reviewer);
                }
            }

            if (reviewModel == null)
            {
                reviewModel = new ReviewModel();
                reviewModel.ReviewedDate = DateTime.Now;
            }

            return View(reviewModel);
        }
Exemplo n.º 2
0
        public ActionResult Edit(int id, ReviewModel reviewModel)
        {
            if (ModelState.IsValid)
            {
                MovieReview review;

                if (id == default(int))
                {
                    review = new MovieReview();
                    if (!ModelFactoryInstance.TryParse(review, reviewModel, _unitOfWork, ModelState))
                    {
                        return View(reviewModel);
                    }
                    review.State = ObjectState.Added;
                    _reviewRepo.Insert(review);
                }
                else
                {
                    review = _reviewRepo.FindById(id);
                    if (!ModelFactoryInstance.TryParse(review, reviewModel, _unitOfWork, ModelState))
                    {
                        return View(reviewModel);
                    }
                    review.State = ObjectState.Modified;
                    _reviewRepo.Update(review);
                }
                _unitOfWork.Save();
                return RedirectToAction("Index");
            }

            return View(reviewModel);
        }
Exemplo n.º 3
0
 public bool TryParse(MovieReview review, ReviewModel reviewModel, IUnitOfWork _unitOfWork, ModelStateDictionary modelState)
 {
     bool isValid = true;
     var movie = _unitOfWork.Repository<Movie>().Query()
         .Filter(m => m.Title.ToLower() == reviewModel.Movie.ToLower())
         .FirstOrDefault();
     if (movie == null)
     {
         modelState.AddModelError("Movie", "Not a valid Movie");
         isValid = false;
     }
     var reviewer = _unitOfWork.Repository<Reviewer>().Query()
         .Filter(r => r.Name.ToLower() == reviewModel.Reviewer.ToLower())
         .FirstOrDefault();
     if (reviewer == null)
     {
         modelState.AddModelError("Reviewer", "Not a valid Reviewer");
         isValid = false;
     }
     review.Movie = movie;
     review.Reviewer = reviewer;
     review.ReviewedDate = reviewModel.ReviewedDate;
     review.TagLine = reviewModel.TagLine;
     review.Rating = reviewModel.Rating;
     review.Review = reviewModel.Review;
     return isValid;
 }