예제 #1
0
        public async Task <List <BooksViewModel> > GetBooksByTypeIdAsync(string typeId, HttpRequest request)
        {
            int size = 6;  //Get ten books first time

            return(await _books.Find(book => book.TypeId == typeId).Limit(size).Project(x =>
                                                                                        new BooksViewModel
            {
                Id = x.Id,
                BookName = x.BookName,
                Price = x.Price.ToString(),
                CoverPrice = x.CoverPrice.ToString(),
                ImgUrl = x.ImgUrl,
                Rating = Rouding.Adjust(Average.CountingAverage(x.Comments)),
                TypeId = x.TypeId
            }).ToListAsync());
        }
예제 #2
0
        public async Task <BooksViewModel> CreateAsync(Book book, HttpRequest request)
        {
            book.Code = DateTime.Now.ToString("yymmssff") + book.PublishDate.DayOfYear.ToString().PadLeft(3, '0');
            await _books.InsertOneAsync(book);

            return(new BooksViewModel()
            {
                Id = book.Id,
                BookName = book.BookName,
                Price = book.Price.ToString(),
                CoverPrice = book.CoverPrice.ToString(),
                ImgUrl = book.ImgUrl,
                Rating = Rouding.Adjust(Average.CountingAverage(book.Comments)),
                TypeId = book.TypeId
            });
        }
예제 #3
0
        public async Task <ActionResult <BookViewModel> > Get(
            [FromQuery(Name = "id")] string id
            )
        {
            var book = await _bookService.GetAsync(id);

            if (book == null)
            {
                return(BadRequest("book not found"));
            }
            var type = await _typeService.GetAsync(book.TypeId);

            var publishingHouse = await _publishingHouseService.GetAsync(book.PublishHouseId);

            var author = await _authorService.GetAsync(book.AuthorId, Request);

            var tag = await _tagService.GetAsync(book.TagId);


            BookViewModel bookViewModel = new BookViewModel()
            {
                Id                  = book.Id,
                BookName            = book.BookName,
                Description         = book.Description,
                Price               = book.Price.ToString(),
                CoverPrice          = book.CoverPrice.ToString(),
                ImgUrl              = book.ImgUrl,
                Rating              = Rouding.Adjust(Average.CountingAverage(book.Comments)),
                BookTypeName        = type.Name,
                PublishingHouseName = publishingHouse.Name,
                Cover_Type          = book.CoverType,
                PageAmount          = book.PageAmount,
                PublishDate         = Convert.ToDateTime(book.PublishDate).ToString("yyyy-MM-dd"),
                Size                = book.Size,
                AuthorName          = author.Name,
                AuthorId            = author.Id,
                PublishingHouseId   = publishingHouse.Id,
                TypeId              = type.Id,
                TagId               = book.TagId,
                TagName             = tag.Name,
                Amount              = book.Amount,
                ZoneType            = book.ZoneType
            };

            return(Ok(bookViewModel));
        }
예제 #4
0
        public async Task <List <BooksViewModel> > GetAsync(int index, HttpRequest request)
        {
            //Get ten books first time
            int size   = 10;
            int length = 10;

            //Get extra five books after first time
            return(await _books.Find(book => book.DeleteAt == null && book.Amount > 0).Limit(index * size + length).Project(x =>
                                                                                                                            new BooksViewModel {
                Id = x.Id,
                BookName = x.BookName,
                Price = x.Price.ToString(),
                CoverPrice = x.CoverPrice.ToString(),
                ImgUrl = x.ImgUrl,
                Rating = Rouding.Adjust(Average.CountingAverage(x.Comments)),
                TypeId = x.TypeId
            }).ToListAsync());
        }
예제 #5
0
        public async Task <EntityList <BooksViewModel> > SearchBooksAdminAsync(FilterDefinition <Book> filter = null, HttpRequest request = null, int page = 1)
        {
            int pageSize = 16;
            var query    = _books.Find(filter);
            var total    = await query.CountDocumentsAsync();

            query = query.Skip((page - 1) * pageSize).Limit(pageSize);

            return(new EntityList <BooksViewModel>
            {
                Total = (int)total,
                Entities = await query.Project(x =>
                                               new BooksViewModel
                {
                    Id = x.Id,
                    BookName = x.BookName,
                    Price = x.Price.ToString(),
                    CoverPrice = x.CoverPrice.ToString(),
                    ImgUrl = x.ImgUrl,
                    Rating = Rouding.Adjust(Average.CountingAverage(x.Comments)),
                    TypeId = x.TypeId
                }).ToListAsync()
            });
        }