public async Task <IActionResult> Index(int page = 1) { ViewBag.Page = page; List <Book> books = _db.Books.Include(b => b.BookAuthors).ThenInclude(b => b.Author).Include(b => b.BookCategories).Include(b => b.Publisher).OrderByDescending(b => b.SaleCount).Take(100).ToList(); ViewBag.PageCount = Math.Ceiling((decimal)books.Count() / 10); List <Book> bestSellers = books.Skip((page - 1) * 10).Take(10).ToList(); ViewBag.Currency = _db.Bios.FirstOrDefault().Currency; AppUser user = new AppUser(); if (User.Identity.IsAuthenticated) { user = await _userManager.FindByNameAsync(User.Identity.Name); } List <BookInCart> bookInCarts = new List <BookInCart>(); List <FavoriteBook> favoriteBooks = new List <FavoriteBook>(); if (user != null) { bookInCarts = _db.BookInCarts.Include(bc => bc.Book).Include(bc => bc.AppUser).Where(bc => bc.AppUserId == user.Id).ToList(); favoriteBooks = _db.FavoriteBooks.Include(bc => bc.Book).Include(bc => bc.AppUser).Where(bc => bc.AppUserId == user.Id).ToList(); } NewBooksVM model = new NewBooksVM { Books = bestSellers, AllBooks = books, BookInCarts = bookInCarts, FavoriteBooks = favoriteBooks }; return(View(model)); }
public IActionResult Index() { List <Book> books = _db.Books.Include(b => b.Publisher).ToList(); Book book = _db.Books.Include(b => b.Publisher).Include(b => b.BookAuthors).ThenInclude(ba => ba.Author).Include(b => b.BookCategories).ThenInclude(b => b.Category).OrderByDescending(b => b.SaleCount).FirstOrDefault(); List <Category> categories = _db.Categories.OrderBy(n => n.Name).ToList(); Bio bio = _db.Bios.FirstOrDefault(); ViewBag.Currency = bio.Currency; List <BookCategory> rBookCategories = new List <BookCategory>(); foreach (BookCategory bookCategory in book.BookCategories) { List <BookCategory> NewrBookCategories = _db.BookCategories.Include(bc => bc.Category).Include(bc => bc.Book).OrderByDescending(nrbc => nrbc.Id).Where(bc => bc.CategoryId == bookCategory.CategoryId).ToList(); foreach (BookCategory newR in NewrBookCategories) { if (rBookCategories.FirstOrDefault(bc => bc.BookId == newR.BookId) == null && newR.BookId != book.Id) { rBookCategories.Add(newR); } } } NewBooksVM model = new NewBooksVM { Books = books, Book = book, Categories = categories, rBookCategories = rBookCategories, Bio = bio }; return(View(model)); }
public async Task <IActionResult> Detail(string slug) { if (slug == null) { return(NotFound()); } Book book = _db.Books.Include(b => b.Publisher).Include(b => b.BookFeature).Include(b => b.BookCategories).ThenInclude(b => b.Category).Include(b => b.BookAuthors).ThenInclude(ba => ba.Author).FirstOrDefault(b => b.Slug == slug); if (book == null) { return(NotFound()); } List <BookAuthor> rBookAuthors = _db.BookAuthors.Include(ba => ba.Author).ToList(); ViewBag.Currency = _db.Bios.FirstOrDefault().Currency; AppUser user = new AppUser(); if (User.Identity.IsAuthenticated) { user = await _userManager.FindByNameAsync(User.Identity.Name); } List <BookInCart> bookInCarts = new List <BookInCart>(); List <FavoriteBook> favoriteBooks = new List <FavoriteBook>(); if (user != null) { bookInCarts = _db.BookInCarts.Include(bc => bc.Book).Include(bc => bc.AppUser).Where(bc => bc.AppUserId == user.Id).ToList(); favoriteBooks = _db.FavoriteBooks.Include(bc => bc.Book).Include(bc => bc.AppUser).Where(bc => bc.AppUserId == user.Id).ToList(); } List <BookCategory> rBookCategories = new List <BookCategory>(); foreach (BookCategory bookCategory in book.BookCategories) { List <BookCategory> NewrBookCategories = _db.BookCategories.Include(bc => bc.Category).Include(bc => bc.Book).OrderByDescending(nrbc => nrbc.Id).Where(bc => bc.CategoryId == bookCategory.CategoryId).ToList(); foreach (BookCategory newR in NewrBookCategories) { if (rBookCategories.FirstOrDefault(bc => bc.BookId == newR.BookId) == null && newR.BookId != book.Id) { rBookCategories.Add(newR); } } } NewBooksVM model = new NewBooksVM { Book = book, rBookCategories = rBookCategories, rBookAuthors = rBookAuthors, BookInCarts = bookInCarts, FavoriteBooks = favoriteBooks }; return(View(model)); }
public async Task <IActionResult> Index(string pslug, string aslug, string cslug, int page = 1) { ViewBag.Page = page; ViewBag.PageCount = Math.Ceiling((decimal)_db.Books.Count() / 10); List <Book> books = new List <Book>(); ViewBag.Currency = _db.Bios.FirstOrDefault().Currency; AppUser user = new AppUser(); if (User.Identity.IsAuthenticated) { user = await _userManager.FindByNameAsync(User.Identity.Name); } List <BookInCart> bookInCarts = new List <BookInCart>(); List <FavoriteBook> favoriteBooks = new List <FavoriteBook>(); if (user != null) { bookInCarts = _db.BookInCarts.Include(bc => bc.Book).Include(bc => bc.AppUser).Where(bc => bc.AppUserId == user.Id).ToList(); favoriteBooks = _db.FavoriteBooks.Include(bc => bc.Book).Include(bc => bc.AppUser).Where(bc => bc.AppUserId == user.Id).ToList(); } List <BookCategory> bookCategories = new List <BookCategory>(); ViewBag.Aslug = ""; ViewBag.Pslug = ""; ViewBag.Cslug = ""; List <Book> allBooks = new List <Book>(); List <Author> authors = _db.Authors.OrderBy(n => n.Fullname).ToList(); List <Publisher> publishers = _db.Publishers.OrderByDescending(n => n.BookCount).ToList(); List <Category> categories = _db.Categories.OrderBy(n => n.Name).ToList(); if (pslug != null && aslug == null && cslug == null) { Publisher publisher = publishers.FirstOrDefault(p => p.Slug == pslug); if (publisher != null) { ViewBag.Pslug = publisher.Name; } List <Book> pBooks = _db.Books.Include(b => b.Publisher).Include(b => b.BookAuthors).ThenInclude(ba => ba.Author).Where(b => b.PublisherId == publisher.Id).ToList(); foreach (Book book in pBooks) { if (books.FirstOrDefault(b => b.Id == book.Id) == null) { books.Add(book); } } ViewBag.PageCount = Math.Ceiling((decimal)books.Count() / 10); allBooks = books; books = books.Skip((page - 1) * 10).Take(10).ToList(); } else if (pslug == null && aslug != null && cslug == null) { List <BookAuthor> aBooks = _db.BookAuthors.Include(ba => ba.Author).Where(ba => ba.Author.Slug == aslug).ToList(); if (authors.FirstOrDefault(a => a.Slug == aslug) != null) { ViewBag.Aslug = authors.FirstOrDefault(a => a.Slug == aslug).Fullname; } foreach (BookAuthor ba in aBooks) { Book newBook = _db.Books.Include(b => b.Publisher).Include(b => b.BookAuthors).ThenInclude(b => b.Author).FirstOrDefault(b => b.Id == ba.BookId); if (books.FirstOrDefault(b => b.Id == newBook.Id) == null) { books.Add(newBook); } } ViewBag.PageCount = Math.Ceiling((decimal)books.Count() / 10); allBooks = books; books = books.Skip((page - 1) * 10).Take(10).ToList(); } else if (pslug == null && aslug == null && cslug != null) { bookCategories = _db.BookCategories.Include(bc => bc.Category).Include(bc => bc.Book).Where(bc => bc.Category.Slug == cslug).ToList(); if (categories.FirstOrDefault(c => c.Slug == cslug) != null) { ViewBag.Cslug = categories.FirstOrDefault(c => c.Slug == cslug).Name; } foreach (BookCategory bc in bookCategories) { Book newBook = _db.Books.Include(b => b.Publisher).Include(b => b.BookAuthors).ThenInclude(ba => ba.Author).FirstOrDefault(b => b.Id == bc.BookId); if (books.FirstOrDefault(b => b.Id == newBook.Id) == null) { books.Add(newBook); } } ViewBag.PageCount = Math.Ceiling((decimal)books.Count() / 10); allBooks = books; books = books.Skip((page - 1) * 10).Take(10).ToList(); } else if (pslug != null && aslug != null && cslug == null) { Publisher publisher = publishers.FirstOrDefault(p => p.Slug == pslug); List <BookAuthor> aBooks = _db.BookAuthors.Include(ba => ba.Author).Include(ba => ba.Book).Where(ba => ba.Author.Slug == aslug).ToList(); if (authors.FirstOrDefault(a => a.Slug == aslug) != null && publisher != null) { ViewBag.Aslug = authors.FirstOrDefault(a => a.Slug == aslug).Fullname; ViewBag.Pslug = publisher.Name; } List <BookAuthor> paBooks = aBooks.Where(ba => ba.Book.PublisherId == publisher.Id).ToList(); foreach (BookAuthor ba in paBooks) { Book newBook = _db.Books.Include(b => b.Publisher).Include(b => b.BookAuthors).ThenInclude(b => b.Author).FirstOrDefault(b => b.Id == ba.BookId); if (books.FirstOrDefault(b => b.Id == newBook.Id) == null) { books.Add(newBook); } } ViewBag.PageCount = Math.Ceiling((decimal)books.Count() / 10); allBooks = books; books = books.Skip((page - 1) * 10).Take(10).ToList(); } else if (pslug != null && aslug == null && cslug != null) { Publisher publisher = publishers.FirstOrDefault(p => p.Slug == pslug); bookCategories = _db.BookCategories.Include(bc => bc.Category).Include(bc => bc.Book).Where(bc => bc.Category.Slug == cslug).ToList(); List <BookCategory> pcBooks = bookCategories.Where(bc => bc.Book.PublisherId == publisher.Id).ToList(); if (categories.FirstOrDefault(c => c.Slug == cslug) != null && publisher != null) { ViewBag.Pslug = publisher.Name; ViewBag.Cslug = categories.FirstOrDefault(c => c.Slug == cslug).Name; } foreach (BookCategory bc in pcBooks) { Book newBook = _db.Books.Include(b => b.Publisher).Include(b => b.BookAuthors).ThenInclude(ba => ba.Author).FirstOrDefault(b => b.Id == bc.BookId); if (books.FirstOrDefault(b => b.Id == newBook.Id) == null) { books.Add(newBook); } } ViewBag.PageCount = Math.Ceiling((decimal)books.Count() / 10); allBooks = books; books = books.Skip((page - 1) * 10).Take(10).ToList(); } else if (pslug == null && aslug != null && cslug != null) { bookCategories = _db.BookCategories.Include(bc => bc.Category).Include(bc => bc.Book).Where(bc => bc.Category.Slug == cslug).ToList(); List <BookAuthor> aBooks = _db.BookAuthors.Include(ba => ba.Author).Where(ba => ba.Author.Slug == aslug).ToList(); List <BookCategory> acBooks = new List <BookCategory>(); if (authors.FirstOrDefault(a => a.Slug == aslug) != null && categories.FirstOrDefault(c => c.Slug == cslug) != null) { ViewBag.Aslug = authors.FirstOrDefault(a => a.Slug == aslug).Fullname; ViewBag.Cslug = categories.FirstOrDefault(c => c.Slug == cslug).Name; } foreach (BookAuthor abook in aBooks) { BookCategory newBc = bookCategories.FirstOrDefault(bc => bc.BookId == abook.BookId); acBooks.Add(newBc); } foreach (BookCategory acBook in acBooks) { if (acBook != null) { Book newBook = _db.Books.Include(b => b.Publisher).Include(b => b.BookAuthors).ThenInclude(ba => ba.Author).FirstOrDefault(b => b.Id == acBook.BookId); if (books.FirstOrDefault(b => b.Id == newBook.Id) == null) { books.Add(newBook); } } } ViewBag.PageCount = Math.Ceiling((decimal)books.Count() / 10); allBooks = books; books = books.Skip((page - 1) * 10).Take(10).ToList(); } else if (pslug != null && aslug != null && cslug != null) { Publisher publisher = publishers.FirstOrDefault(p => p.Slug == pslug); bookCategories = _db.BookCategories.Include(bc => bc.Category).Include(bc => bc.Book).Where(bc => bc.Category.Slug == cslug).ToList(); List <BookAuthor> aBooks = _db.BookAuthors.Include(ba => ba.Author).Where(ba => ba.Author.Slug == aslug).ToList(); List <BookCategory> acBooks = new List <BookCategory>(); if (authors.FirstOrDefault(a => a.Slug == aslug) != null && publisher != null && categories.FirstOrDefault(c => c.Slug == cslug) != null) { ViewBag.Aslug = authors.FirstOrDefault(a => a.Slug == aslug).Fullname; ViewBag.Pslug = publisher.Name; ViewBag.Cslug = categories.FirstOrDefault(c => c.Slug == cslug).Name; } foreach (BookAuthor abook in aBooks) { BookCategory newBc = bookCategories.FirstOrDefault(bc => bc.BookId == abook.BookId); if (newBc != null && newBc.Book.PublisherId == publisher.Id) { acBooks.Add(newBc); } } foreach (BookCategory acBook in acBooks) { if (acBook != null) { Book newBook = _db.Books.Include(b => b.Publisher).Include(b => b.BookAuthors).ThenInclude(ba => ba.Author).FirstOrDefault(b => b.Id == acBook.BookId); if (books.FirstOrDefault(b => b.Id == newBook.Id) == null) { books.Add(newBook); } } } ViewBag.PageCount = Math.Ceiling((decimal)books.Count() / 10); allBooks = books; books = books.Skip((page - 1) * 10).Take(10).ToList(); } else { allBooks = _db.Books.Include(b => b.Publisher).Include(b => b.BookAuthors).ThenInclude(ba => ba.Author).ToList(); books = allBooks.Skip((page - 1) * 10).Take(10).ToList(); } NewBooksVM model = new NewBooksVM { Books = books, AllBooks = allBooks, //BookAuthors = bookAuthors, Authors = authors, Publishers = publishers, Categories = categories, BookInCarts = bookInCarts, FavoriteBooks = favoriteBooks }; return(View(model)); }