示例#1
0
        public BookRatingViewModel IncrementBookRating(int rate, int id)
        {
            var book = db.Books
                       .Where(b => b.Id == id)
                       .First();

            book.SumRatings += rate;
            book.NumberOfRatings++;
            db.SaveChanges();
            var bookRating = new BookRatingViewModel()
            {
                BookId          = book.Id,
                SumRatings      = book.SumRatings,
                NumberOfRatings = book.NumberOfRatings,
                AverageRating   = Math.Round((double)book.SumRatings / (double)book.NumberOfRatings, 2)
            };

            return(bookRating);
        }
示例#2
0
        public ActionResult Details(int id)
        {
            var book      = _context.Books.Include(b => b.GenreType).SingleOrDefault(b => b.Id == id);
            var viewModel = new BookRatingViewModel
            {
                Book    = book,
                Ratings = _context.Ratings.ToList()
            };

            if (viewModel == null)
            {
                return(HttpNotFound());
            }

            if (User.IsInRole("CanManageBooks"))
            {
                return(View(viewModel));
            }

            return(View("ReadOnlyDetails", viewModel));
        }
示例#3
0
        // GET: Books/Details/5
        public ActionResult Details(int?id)
        {
            if (id == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }
            var viewModel = new BookRatingViewModel {
                AverageRating = 0
            };

            viewModel.Book = db.Books.Find(id);
            viewModel.Book.NumberOfViews++;
            db.SaveChanges();
            if (viewModel.Book.NumberOfRatings > 0)
            {
                viewModel.AverageRating = Math.Round((double)viewModel.Book.SumRatings / (double)viewModel.Book.NumberOfRatings, 2);
            }
            if (viewModel.Book == null)
            {
                return(HttpNotFound());
            }
            return(View(viewModel));
        }