public ActionResult PostReview(int id, LocationReviewViewModel model) { try { DbLocation location = GetLocationIfExists(id); ViewBag.Location = location; if (location == null) { return(RedirectToAction("Index", "Reviews")); } AuthenticatedUser user = (AuthenticatedUser)User; BookingsRepository bookingsRepository = _unitOfWork.BookingsRepository; IList <DbBooking> booking = bookingsRepository.GetAllByUserIdAndLocationId(user.Id, id); if (booking == null || !booking.Any()) { return(RedirectToAction("Index", "Reviews")); } if (!ModelState.IsValid) { return(View(model)); } LocationReviewsRepository locationReviewsRepository = _unitOfWork.LocationReviewsRepository; DbLocationReview review = new DbLocationReview(); review.DateTime = DateTime.Now; review.LocationId = id; review.Review = model.Review; review.UserId = user.Id; review.Title = model.Title; // We insert our review before storing the facility ratings so we have a review id locationReviewsRepository.Insert(review); review.FacilityRatings = model.FacilityRatings.Select(x => { return(new DbLocationFacilityRating { FacilityId = x.Id, Rating = MathEx.Clamp(x.Rating, 0, 1), ReviewId = review.Id }); }).ToList(); locationReviewsRepository.Update(review); TempData["AlertType"] = "success"; TempData["AlertMessage"] = "De review is succesvol toegevoegd aan de locatie."; return(RedirectToAction("Index", "Reviews")); } catch { TempData["AlertType"] = "danger"; TempData["AlertMessage"] = "Er is iets fout gelopen tijdens het verwerken van de review!"; return(View(model)); } }
private bool IsUserAllowedToWriteReviewForLocation(int locationId) { BookingsRepository bookingsRepository = _unitOfWork.BookingsRepository; IList <DbBooking> booking = bookingsRepository.GetAllByUserIdAndLocationId(((AuthenticatedUser)User).Id, locationId); if (booking == null || !booking.Any()) { return(false); } LocationReviewsRepository locationReviewsRepository = _unitOfWork.LocationReviewsRepository; DbLocationReview review = locationReviewsRepository.GetByUserIdAndLocationId(((AuthenticatedUser)User).Id, locationId); if (review != null) { return(false); } return(true); }