public ActionResult DeleteConfirmed(int Id, int bookId, PartialTestViewModel pvm)
        {
            if (ModelState.IsValid)
            {
                Review review = uow.ReviewRepository.Get(item => item.Id == Id);
                uow.ReviewRepository.Delete(review);
                uow.SaveChanges();



                Book book = uow.BookRepository.Get(item => item.BookId == bookId);

                pvm.Reviews = book.Reviews.ToList();

                ViewData["CanLeaveReview"] = true;

                return(PartialView("_ReviewPartial", pvm));
            }

            return(View());
        }
        public ActionResult Create(Review review, PartialTestViewModel pvm)
        {
            if (ModelState.IsValid)
            {
                string          currentUserId = User.Identity.GetUserId();
                ApplicationUser currentUser   = uow.UserRepository.Get(x => x.Id == currentUserId);

                review.Author    = User.Identity.Name;
                review.CreatedAt = DateTime.Now;
                review.BookId    = pvm.book.BookId;
                review.User      = currentUser;
                int id = pvm.book.BookId;

                currentUser.Reviews.Add(review);

                uow.ReviewRepository.Add(review);
                uow.SaveChanges();
                Book book = uow.BookRepository.Get(item => item.BookId == id);

                ModelState.Clear();



                PartialTestViewModel newPvm = new PartialTestViewModel();

                newPvm.Reviews          = book.Reviews.ToList();
                newPvm.book             = book;
                newPvm.book.BookId      = book.BookId;
                newPvm.book.ArtworkURL  = book.ArtworkURL;
                newPvm.book.Genre       = book.Genre;
                newPvm.book.BookTitle   = book.BookTitle;
                newPvm.book.Publisher   = book.Publisher;
                newPvm.book.Description = book.Description;
                newPvm.ReviewCount      = book.Reviews.Count();
                newPvm.user             = GetCurrentUser();


                int total = 0;

                foreach (var r in book.Reviews)
                {
                    int stars = r.stars;

                    total = total + stars;
                }

                if (newPvm.ReviewCount != 0)
                {
                    newPvm.AvgStars      = (total / newPvm.ReviewCount);
                    TempData["AvgStars"] = newPvm.AvgStars;
                }



                ViewData["CanLeaveReview"]  = false;
                ViewData["AlreadyReviewed"] = "You have already reviewed this book";

                return(PartialView("_ReviewPartial", newPvm));
            }

            return(View(pvm));
        }
Exemple #3
0
        /// <summary>
        /// method which displays details of a specific book in database
        /// </summary>
        /// <param name="id">book to be displayed</param>
        /// <returns>a formated view of the book to the view</returns>
        // GET: Books/Details/5
        public ActionResult Details(int id)
        {
            ViewData["CanUpdate"]       = false;
            ViewData["CanLeaveReview"]  = false;
            ViewData["AlreadyReviewed"] = " ";


            if (TempData["AlreadyInCart"] == null)
            {
                TempData["AlreadyInCart"] = "";
            }

            //get book
            Book book = UoW.BookRepository.Get(item => item.BookId == id);

            //add book details to viewmodel
            PartialTestViewModel pvm = new PartialTestViewModel();

            pvm.book             = book;
            pvm.book.Author      = book.Author;
            pvm.book.BookTitle   = book.BookTitle;
            pvm.book.Description = book.Description;

            pvm.book.Added     = book.Added;
            pvm.book.BookId    = book.BookId;
            pvm.book.Publisher = book.Publisher;
            pvm.book.Supplier  = book.Supplier;
            pvm.Reviews        = book.Reviews.ToList();

            pvm.book.ArtworkURL = "http://placehold.it/300x450";
            pvm.ReviewCount     = book.Reviews.Count();
            pvm.user            = GetCurrentUser();

            //show/hide items based on conditions
            if (GetCurrentUser() != null && GetCurrentUser().AccountRestricted == true)
            {
                ViewData["AlreadyReviewed"] = "Your account is restricted, you are not permitted to leave reviews.";
                ViewData["CanLeaveReview"]  = false;
            }

            //get reviews for the book
            var reviews = UoW.ReviewRepository.GetAll();

            List <string> reviewAuthors = new List <string>();

            //review permissions based on user
            foreach (var r in reviews)
            {
                reviewAuthors.Add(r.Author);

                if (GetCurrentUser() != null && (r.Author.Equals(GetCurrentUser().UserName) && GetCurrentUser().AccountRestricted == false))
                {
                    ViewData["CanUpdate"] = true;
                }
                else
                {
                    ViewData["CanUpdate"] = false;
                }
            }

            if (GetCurrentUser() != null && reviewAuthors.Contains(GetCurrentUser().UserName))
            {
                ViewData["AlreadyReviewed"] = "You have already reviewed this book";
                ViewBag.MustLogIn           = "";
                ViewData["CanLeaveReview"]  = false;
            }
            else if (GetCurrentUser() != null && GetCurrentUser().AccountRestricted == true)
            {
                ViewData["AlreadyReviewed"] = "Your account is restricted, you are not permitted to leave reviews.";
                ViewData["CanLeaveReview"]  = false;
            }
            else
            {
                ViewData["CanLeaveReview"] = true;
            }
            if (IsLoggedIn() == false)
            {
                ViewBag.MustLogIn           = "******";
                ViewData["AlreadyReviewed"] = "";
                ViewData["CanLeaveReview"]  = false;
            }


            int total = 0;

            //work out average stars for book
            foreach (var review in book.Reviews)
            {
                int stars = review.stars;

                total = total + stars;
            }

            if (pvm.ReviewCount != 0)
            {
                pvm.AvgStars = (total / pvm.ReviewCount);
            }

            return(View(pvm));
        }