Exemplo n.º 1
0
        //
        // 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));
        }
Exemplo n.º 2
0
        //DangVH. Create. End (14/11/2016)


        //
        // GET: /Books/
        public ActionResult Index(string searchString)
        {
            //DangVH. Create. Start (02/11/2016)
            ViewBag.currentUser = User.Identity.GetUserName();
            ViewBag.bookNumber  = BooksControllerHelper.GetBookNumber();
            //DangVH. Create. End (02/11/2016)
            ViewBag.allCategories = BooksControllerHelper.ListAllCategory();
            ViewBag.allPublishers = Context.Publishers.Find(_ => true).ToList();
            var books = Context.Books.Find(_ => true).ToEnumerable();

            //DangVH. Create. Start (02/11/2016)
            if (!string.IsNullOrEmpty(searchString))
            {
                books = books.Where(x => x.BookName.Contains(searchString) || x.Authors.Contains(searchString));
            }
            //DangVH. Create. End (02/11/2016)
            return(View(books));
        }
Exemplo n.º 3
0
        //DangVH. Create. End (14/11/2016)


        //
        // GET: /Books/
        public ActionResult Index(string searchString, string currentFilter, int?page)
        {
            if (Context.Users.Find(x => x.Id.Equals(User.Identity.GetUserId())).FirstOrDefault().Roles.Contains("admin"))
            {
                //DangVH. Create. Start (02/11/2016)
                ViewBag.currentUser = Context.Users.Find(x => x.Id.Equals(new ObjectId(User.Identity.GetUserId()))).FirstOrDefault();
                ViewBag.bookNumber  = BooksControllerHelper.GetBookNumber();
                //DangVH. Create. End (02/11/2016)
                ViewBag.allCategories = BooksControllerHelper.ListAllCategory();
                ViewBag.allPublishers = Context.Publishers.Find(_ => true).ToList();
                ViewBag.allAuthors    = Context.Authors.Find(_ => true).ToList();
                var books = Context.Books.Find(x => x.Requested.Equals(false)).ToEnumerable();
                if (searchString != null)
                {
                    page = 1;
                }
                else
                {
                    searchString = currentFilter;
                }
                ViewBag.currentFilter = searchString;
                //DangVH. Create. Start (02/11/2016)
                if (!string.IsNullOrEmpty(searchString))
                {
                    var           authors       = Context.Authors.Find(_ => true).ToList().Where(x => x.AuthorName.Contains(searchString)).ToList();
                    List <string> searchAuthors = new List <string>();
                    foreach (var author in authors)
                    {
                        searchAuthors.Add(author.Id.ToString());
                    }
                    books = books.Where(x => x.BookName.Contains(searchString) || x.Authors.Intersect(searchAuthors).Any());
                }
                int pageSize   = 6;
                int pageNumber = (page ?? 1);
                //DangVH. Create. End (02/11/2016)
                return(View(books.ToPagedList(pageNumber, pageSize)));
            }
            else
            {
                ViewBag.errorMessage = "Bạn không có quyền truy cập vào chức năng này";
                return(View("NotFoundError"));
            }
        }
Exemplo n.º 4
0
        //
        // 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));
            }
        }