Ejemplo n.º 1
0
        public async Task <IEnumerable <Volume> > GetBooks(List <int> bookshelfsIds)
        {
            var start = DateTime.Now;

            GoogleData.Bookshelves bookshelfsList = await _booksService.Mylibrary.Bookshelves.List().ExecuteAsync();

            IEnumerable <GoogleData.Bookshelf> bookshelves = bookshelfsList?.Items
                                                             ?.Where(x => x.Id.HasValue && (x.Id.Value < 8 || x.Id.Value > 9));

            var allVolumes = new ConcurrentBag <Volume>();

            Parallel.ForEach(bookshelves, bookshelf =>
            {
                if (bookshelf.Id.HasValue)
                {
                    IList <Volume> bookshelfVolumes = GetBookshelfVolumes(bookshelf.Id.Value);
                    foreach (var item in bookshelfVolumes)
                    {
                        AddToBookshelfList(allVolumes, item, bookshelf.Id.Value, bookshelf.Title);
                    }
                }
            });

            IEnumerable <Volume> result = bookshelfsIds.Any()
                ? allVolumes.Where(x => x.UserBookshelfs.Any(us => bookshelfsIds.Contains(us.Id.Value)))
                : allVolumes;

            var end  = DateTime.Now;
            var time = end.Subtract(start);

            System.Diagnostics.Debug.WriteLine($"The GetBooks request took: {time.TotalMilliseconds} ms.");

            return(result);
        }
Ejemplo n.º 2
0
        private async Task <IEnumerable <BookshelfBase> > SelectBookshelfsAsync(IEnumerable <int> bookshelfsIds = null)
        {
            GoogleData.Bookshelves bookshelfsList = await _booksService.Mylibrary.Bookshelves.List().ExecuteAsync();

            IEnumerable <BookshelfBase> allUserBookshelfs = bookshelfsList?.Items
                                                            ?.Where(x => x.Id.HasValue)
                                                            ?.Select(x => new BookshelfBase(x.Id.Value, x.Title));

            IEnumerable <BookshelfBase> bookshelfs = bookshelfsIds?.Any() ?? false
                ? allUserBookshelfs.Where(x => bookshelfsIds.Contains(x.Id.Value))                                                    // select only bookshelfs for filtered bookshelfIds
                : allUserBookshelfs.Where(x => CommonGoogleBookshelfs.BaseGoogleBookshelfIds.Contains(x.Id.Value) || x.Id.Value > 9); //select base and custom bookshelfs

            return(bookshelfs);
        }
Ejemplo n.º 3
0
            public async Task <GetBookshelfsApiListModel> Handle(GetBookshelfsListQuery request, CancellationToken cancellationToken)
            {
                GoogleData.Bookshelves googleBookshelves = await _booksService.Mylibrary.Bookshelves.List().ExecuteAsync();

                googleBookshelves.Items = googleBookshelves.Items.Where(x => x.Id.HasValue).ToList();

                if (googleBookshelves == null)
                {
                    return(ApiModel.NotFound <GetBookshelfsApiListModel>());
                }

                switch (request.BookshelfOptions)
                {
                case BookshelfsOption.Base:
                    googleBookshelves.Items = googleBookshelves.Items.Where(x => x.Id < 5).ToList();
                    break;

                case BookshelfsOption.Custom:
                    googleBookshelves.Items = googleBookshelves.Items.Where(x => x.Id > 9).ToList();
                    break;

                case BookshelfsOption.BaseAndCustom:
                    googleBookshelves.Items = googleBookshelves.Items.Where(x => x.Id < 5 || x.Id > 9).ToList();
                    break;

                case BookshelfsOption.Others:
                    googleBookshelves.Items = googleBookshelves.Items.Where(x => x.Id >= 5 || x.Id <= 9).ToList();
                    break;

                case BookshelfsOption.Favorites:
                    var favoriteBookshelves = await _context.FavoriteBookshelves
                                              .Where(x => x.UserId == request.UserId)
                                              .Select(x => x.BookshelfId)
                                              .ToListAsync();

                    googleBookshelves.Items = googleBookshelves.Items
                                              .Where(x => favoriteBookshelves.Contains(x.Id.ToString())).ToList();
                    break;

                case BookshelfsOption.All:
                default:
                    break;
                }

                IEnumerable <Bookshelf> bookshelfs = _mapper
                                                     .Map <IEnumerable <GoogleData.Bookshelf>, IEnumerable <Bookshelf> >(googleBookshelves.Items);

                return(new GetBookshelfsApiListModel(bookshelfs));
            }