public ActionResult AttractionReviews(int count)
        {
            Random random = new Random();
            for (int i = 0; i < count; ++i)
            {
                AttractionReview review = new AttractionReview();
                review.AttractionID = db.Attraction.OrderBy(x => x.ID).Skip(random.Next(db.Attraction.Count() - 1)).FirstOrDefault().ID;
                review.Author = "guest";
                review.Date = DateTime.Now;
                review.Rating = random.Next(1,10);
                review.Text = generateRandomText(2);
                db.AttractionReview.Add(review);
                db.SaveChanges();
            }

            return RedirectToAction("Index", "Attraction");
        }
Exemplo n.º 2
0
        public ActionResult ReviewEdit(AttractionReview review, int oldRating)
        {
            if (!review.Author.Equals(Membership.GetUser().UserName) && !Roles.IsUserInRole(Membership.GetUser().UserName, "admin"))
                return RedirectToAction("Details", new { id = review.AttractionID });

            db.Entry(review).State = EntityState.Modified;

            //update avg rating
            var attraction = db.Attraction.Include(c => c.Country).Include(c => c.AttractionType)
                .Include(r => r.Reviews).Where(x => x.ID == review.AttractionID).SingleOrDefault();
            int reviewsCount = attraction.Reviews.Count();
            attraction.AvgRating = (attraction.AvgRating * reviewsCount - oldRating + review.Rating) / reviewsCount;
            //end of update

            db.SaveChanges();
            return RedirectToAction("Details", new { id = review.AttractionID });
        }
Exemplo n.º 3
0
        public ActionResult ReviewCreate(int AttractionId, String Author, int Rating, String Text)
        {
            AttractionReview review = new AttractionReview();
            review.AttractionID = AttractionId;
            review.Author = Author;
            review.Rating = Rating;
            review.Text = Text;
            review.Date = DateTime.Now;

            var attraction = db.Attraction.Include(x => x.AttractionType).Include(x => x.Address).Include(x => x.Coordinates)
                .Include(x => x.Country).SingleOrDefault(x => x.ID == review.AttractionID);

            int reviewsCount = db.AttractionReview.Where(a => a.AttractionID == review.AttractionID).Count();

            if (ModelState.IsValid)
            {
                db.AttractionReview.Add(review);
                attraction.AvgRating = ((attraction.AvgRating * reviewsCount) + review.Rating) / (reviewsCount + 1);
                db.SaveChanges();
            }
            //return RedirectToAction("Details", new { id = review.AttractionID });
            return View("GetReview", review);
        }