Exemplo n.º 1
0
 public async Task <ActionResult> SearchBook([FromBody] SearchBookDTO model)
 {
     return(Ok(new { data = await _bookServices.SearchBook(model), success = true }));
 }
Exemplo n.º 2
0
        public async Task <ActionResult> SearchBook(SearchBookDTO model)
        {
            IQueryable <Book> book = _bookstoreContext.Book
                                     .Where(x => x.Private == false)
                                     .Include(c => c.Category)
                                     .Include(c => c.Author)
                                     .Include(b => b.BookImage);

            if (model.CategoryId is not null)
            {
                book = book.Where(b => b.CategoryId == model.CategoryId);
            }
            //Author
            if (model.AuthorName is not null)
            {
                book = book.Where(x => x.Author.AuthorName.ToLower().Contains(model.AuthorName.ToLower()));
            }
            //Book Name
            if (model.BookName is not null)
            {
                book = book.Where(x => x.BookName.ToLower().Contains(model.BookName.ToLower()));
            }            //Category
            if (model.CategoryName is not null)
            {
                book = book.Where(x => x.Category.CategoryName.ToLower().Contains(model.CategoryName.ToLower()));
            }            //Start price
            if (model.StartPrice is not null)
            {
                book = book.Where(x => x.Price >= model.StartPrice);
            }            //End
            if (model.EndPrice is not null)
            {
                book = book.Where(x => x.Price <= model.EndPrice);
            }

            //Sort
            if (model.SortByPriceAsc is not null)
            {
                book = book.OrderBy(b => b.Price);
            }
            if (model.SortByPriceDesc is not null)
            {
                book = book.OrderByDescending(b => b.Price);
            }
            if (model.SortByNameAsc is not null)
            {
                book = book.OrderBy(b => b.BookName);
            }
            if (model.SortByNameDesc is not null)
            {
                book = book.OrderByDescending(b => b.BookName);
            }
            if (model.SortByTimeAsc is not null)
            {
                book = book.OrderBy(b => b.PublicationDate);
            }
            if (model.SortByTimeDesc is not null)
            {
                book = book.OrderByDescending(b => b.PublicationDate);
            }
            return(await book.AsNoTracking().Paginate(model.TotalPerPage ?? 8, model.CurrentPage ?? 0));

            //var returnModel = _mapper.Map<IList<BookInfoViewModel>>(book.ToList());

            //foreach (var item in returnModel)
            //{
            //    item.Comments = await _bookCommentServices.GetCommentsInBook(item.Id);
            //}
        }