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); }
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)); }
// 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)); }