public ActionResult RatingBook(string id, int rating) { var book = Context.Books.Find(x => x.Id.Equals(new ObjectId(id))).FirstOrDefault(); int ratingPoint = book.RatingPoint + rating; int ratingTime = book.RateTime + 1; var filter = Builders <Book> .Filter.Where(x => x.Id.Equals(book.Id)); var update = Builders <Book> .Update.Set(x => x.RatingPoint, ratingPoint). Set(x => x.RateTime, ratingTime). Set(x => x.AvarageRating, BooksControllerHelper.GetAverageRatingPoint(ratingPoint, ratingTime)); Context.Books.UpdateOneAsync(filter, update); return(Json("Cảm ơn bạn đã đánh giá :D")); }
// // GET: /Books/Details/5 public ActionResult Details(string id) { //DangVH. Create. Start (02/11/2016) ViewBag.cloudinary = cloudinary; //DangVH. Create. End (02/11/2016) var book = Context.Books.Find(x => x.Id.Equals(new ObjectId(id))).FirstOrDefault(); foreach (var comment in book.Comments) { comment.LinesDescription = comment.Description.Split(new string[] { "\r\n" }, StringSplitOptions.None).ToList(); } book.Comments = book.Comments.OrderByDescending(x => x.CreatedTime).ToList(); Console.Write(book); var allCategory = Context.Categories.Find(_ => true).ToEnumerable(); ViewBag.allCategories = BooksControllerHelper.ListAllCategory(); ViewBag.allPublishers = Context.Publishers.Find(_ => true).ToList(); ViewBag.avarageRatingPoint = BooksControllerHelper.GetAverageRatingPoint(book.RatingPoint, book.RateTime); var booksStatistic = Context.BooksStatistic.Find(_ => true).ToList(); var date = DateTime.Now; var builder = Builders <BookStatistic> .Filter; var filter = builder.Eq("BookId", book.Id) & builder.Eq("EachDate", date.Date.AddHours(7)); if (Context.BooksStatistic.Find(filter).FirstOrDefault() != null) { int count = Context.BooksStatistic.Find(filter).FirstOrDefault().Count + 1; var update = Builders <BookStatistic> .Update.Set(x => x.Count, count); Context.BooksStatistic.UpdateOneAsync(filter, update); } else { var bookStatistic = new BookStatistic() { BookId = book.Id, EachDate = date.Date.AddHours(7) }; Context.BooksStatistic.InsertOneAsync(bookStatistic); } ViewBag.listSameBook = BooksControllerHelper.SuggestBook(id, 4); Random random = new Random((int)(DateTime.Now.Ticks)); ViewBag.randomGroup = Context.Groups.Find(_ => true).ToList().OrderBy(x => random.Next()).Take(4).ToList(); ViewBag.currentUser = Context.Users.Find(x => x.Id.Equals(new ObjectId(User.Identity.GetUserId()))).FirstOrDefault(); ViewBag.allUser = Context.Users.Find(_ => true).ToList(); return(View(book)); }
// // GET: /Books/Details/5 public ActionResult Details(string id) { //DangVH. Create. Start (02/11/2016) ViewBag.cloudinary = cloudinary; //DangVH. Create. End (02/11/2016) var allBook = Context.Books.Find(_ => true).ToList(); var check = allBook.Where(x => x.Id.Equals(id)).Any(); if (check == false) { ViewBag.errorMessage = "Không có kết quả!"; return(View("NotFoundError")); } else { var book = Context.Books.Find(x => x.Id.Equals(new ObjectId(id))).FirstOrDefault(); foreach (var comment in book.Comments) { comment.LinesDescription = comment.Description.Split(new string[] { "\r\n" }, StringSplitOptions.None).ToList(); } book.Comments = book.Comments.OrderByDescending(x => x.CreatedTime).ToList(); Console.Write(book); var allCategory = Context.Categories.Find(_ => true).ToEnumerable(); ViewBag.allCategories = BooksControllerHelper.ListAllCategory(); ViewBag.allPublishers = Context.Publishers.Find(_ => true).ToList(); ViewBag.avarageRatingPoint = BooksControllerHelper.GetAverageRatingPoint(book.RatingPoint, book.RateTime); // Đoạn này để tính số lượt truy cập của sách var booksStatistic = Context.BooksStatistic.Find(_ => true).ToList(); var date = DateTime.Now; var builder = Builders <BookStatistic> .Filter; var filter = builder.Eq("BookId", book.Id) & builder.Eq("EachDate", date.Date.AddHours(7)); if (Context.BooksStatistic.Find(filter).FirstOrDefault() != null) { int count = Context.BooksStatistic.Find(filter).FirstOrDefault().Count + 1; var update = Builders <BookStatistic> .Update.Set(x => x.Count, count); Context.BooksStatistic.UpdateOneAsync(filter, update); } else { var bookStatistic = new BookStatistic() { BookId = book.Id, EachDate = date.Date.AddHours(7) }; Context.BooksStatistic.InsertOneAsync(bookStatistic); } // Hết đoạn tính số lượt truy cập sách // Xử lý sách đã xem và đánh giá gần đây của người dùng hiện tại var userInteractFilter = Builders <ApplicationUser> .Filter.Where(x => x.Id.Equals(User.Identity.GetUserId())); var userInteractUpdate = Builders <ApplicationUser> .Update.Push(x => x.Interacbook, new InteractBookViewModel { Id = ObjectId.GenerateNewId(), BookId = book.Id, InteractTime = DateTime.Now.AddHours(7) }); Context.Users.UpdateOneAsync(userInteractFilter, userInteractUpdate); // Hết phần xử lí sách đã xem và đánh giá gần đây của người dùng hiện tại ViewBag.listSameBook = BooksControllerHelper.SuggestBook(id, 4); // Lấy list sách có chung thể loại Random random = new Random((int)(DateTime.Now.Ticks)); ViewBag.randomGroup = Context.Groups.Find(_ => true).ToList().OrderBy(x => random.Next()).Take(4).ToList(); // List nhóm ngẫu nhiên ViewBag.listGroupHaveTagIsCurrentBook = GroupsControllerHelper.SuggestGroup(id); // List nhóm có thẻ là sách hiện tại ViewBag.allAuthor = Context.Authors.Find(_ => true).ToList(); ViewBag.allUser = Context.Users.Find(_ => true).ToList(); return(View(book)); } }