Example #1
0
 public async Task LoadOffers(Session session)
 {
     GiftInfo.GiftAvailable = false;
     var result = await _profileProvider.GetOffers(session.Token);
     var offers = result.Offers;
     Offers = new XCollection<Offer>();
     foreach (var offer in offers.Where(offer => offer.Campaign == 2))
     {
         Offers.Add(offer);
     }
     PresentValidTime = DateTime.MinValue;
     foreach (var time in Offers.Select(offer => DateTime.ParseExact(offer.ValidTill, "yyyy-MM-dd HH:mm:ss", new CultureInfo("ru-RU"), DateTimeStyles.AssumeUniversal | DateTimeStyles.AdjustToUniversal)).Where(time => time > PresentValidTime))
     {
         PresentValidTime = time;
     }
     var count = Offers.Count(of => of.Class == 2);
     if (count <= 0) return;
     GiftInfo.GiftAvailable = true;
     var giftPrice = (from offer in Offers where offer.Xml.Hidden.Present.Price > 0 select offer.Xml.Hidden.Present.Price).Concat(new double[] {99999}).Min();
     GiftInfo.MinGiftPrice = giftPrice;
 }
	    public async Task<XCollection<Book>> GetBooksByTag(int fromPosition, int tagId, CancellationToken cancellationToken)
	    {
            if (_booksByTag == null || _booksByTagId != tagId)
            {
                string limit = string.Format("{0},{1}", fromPosition, BooksInPage);

                var parameters = new Dictionary<string, object>
                        {
                            {"tag", tagId},
                            {"limit", limit},
                        };

                var books = await _client.SearchCatalog(parameters, cancellationToken);

                CheckMyBooks(books.Books);

                _booksByTagId = tagId;

                _booksByTag = books.Books;
            }
            else
            {
                int lastIndex = fromPosition + BooksInPage;

                var collection = new XCollection<Book>(_booksByTag.Take(lastIndex));

                if (collection.Count() + BooksInPageShift < lastIndex)
                {
                    lastIndex = collection.Count();

                    string limit = string.Format("{0},{1}", lastIndex, BooksInPage);

                    var parameters = new Dictionary<string, object>
                        {
                            {"genre", tagId},
                            {"limit", limit},
                        };

                    var books = await _client.SearchCatalog(parameters, cancellationToken);

                    if (books != null && books.Books != null && books.Books.Any())
                    {
                        CheckMyBooks(books.Books);

                        foreach (var book in books.Books)
                        {
                            if (!collection.ContainsKey(book.GetKey()))
                            {
                                collection.Add(book);
                            }
                        }
                    }

                    _booksByTag = collection;
                }

                return collection;
            }
            return _booksByTag;
        }
		public async Task<XCollection<Book>> GetPopularBooksByGenres(int fromPosition, List<int> genreId, CancellationToken cancellationToken)
		{
			if( _popularBooksByGenres == null || _popularBooksByGenresIds == null || !_popularBooksByGenresIds.SequenceEqual( genreId ) )
			{
				string limit = string.Format( "{0},{1}", fromPosition, BooksInPage );

				var parameters = new Dictionary<string, object>
						{																	
							{"genre", genreId},							
							{"limit", limit},																
							{"sort", "pop_desc"},								
						};

				var books = await _client.SearchCatalog(parameters, cancellationToken);

				CheckMyBooks( books.Books );

				_popularBooksByGenresIds = genreId;

				_popularBooksByGenres = books.Books;
			}
			else
			{
				int lastIndex = fromPosition + BooksInPage;

				var collection = new XCollection<Book>( _popularBooksByGenres.Take( lastIndex ) );

				if (collection.Count() + BooksInPageShift < lastIndex)
				{
					lastIndex = collection.Count();

					string limit = string.Format( "{0},{1}", lastIndex, BooksInPage );

					var parameters = new Dictionary<string, object>
						{															
							{"genre", genreId},							
							{"limit", limit},															
							{"sort", "pop_desc"},		
						};

					var books = await _client.SearchCatalog(parameters, cancellationToken);

					if (books != null && books.Books != null && books.Books.Any())
					{
						CheckMyBooks( books.Books );

						foreach (var book in books.Books)
						{
							if (!collection.ContainsKey( book.GetKey() ))
							{
								collection.Add( book );
							}
						}
					}

					_popularBooksByGenres = collection;
				}

				return collection;
			}
			return _popularBooksByGenres;
		}
        public async Task<XCollection<Book>> GetNoveltyBooks(int fromPosition, CancellationToken cancellationToken, int customBooksOnPage = 0)
		{
            int booksOnPage = customBooksOnPage > 0 ? customBooksOnPage : BooksInPage;
			if (_interestingBooks == null)
			{
                string limit = string.Format("{0},{1}", fromPosition, booksOnPage);

				var parameters = new Dictionary<string, object>
						{						
							{"sort", "time_desc"},									
							{"min_person_rating", "6"},								
							{"limit", limit},
						};

				var books = await _client.SearchCatalog(parameters, cancellationToken);

				CheckMyBooks( books.Books );

				_interestingBooks = books.Books;
			}
			else
			{
                int lastIndex = fromPosition + booksOnPage;

				var collection = new XCollection<Book>( _interestingBooks.Take( lastIndex ) );

				if (collection.Count() + BooksInPageShift < lastIndex)
				{
					lastIndex = collection.Count();

                    string limit = string.Format("{0},{1}", lastIndex, booksOnPage);

					var parameters = new Dictionary<string, object>
						{								
#if PDF_ENABLED	
                        {"search_types", "4"},	
#else
                        {"search_types", "0"},	
#endif									
							{"rating", "authors"},								
							{"limit", limit},
						};

					var books = await _client.SearchCatalog(parameters, cancellationToken);

					if (books != null && books.Books != null && books.Books.Any())
					{
						CheckMyBooks( books.Books );

						foreach (var book in books.Books)
						{
							if (!collection.ContainsKey( book.GetKey() ))
							{
								collection.Add( book );
							}
						}
					}

					_interestingBooks = collection;
				}
                _dataCacheService.PutItem(_interestingBooks, InterestingBooksCacheItemName, cancellationToken);
                return collection;
			}
            _dataCacheService.PutItem(_interestingBooks, InterestingBooksCacheItemName, cancellationToken);
            return _interestingBooks;
		}
		public async Task<XCollection<Book>> GetNoveltyBooksByGenre(int fromPosition, int genreId, CancellationToken cancellationToken)
		{
			if (_noveltyBooksByGenre == null || _noveltyBooksByGenreId != genreId)
			{
				string limit = string.Format( "{0},{1}", fromPosition, BooksInPage );

				var parameters = new Dictionary<string, object>
						{								
#if PDF_ENABLED	
                        {"search_types", "4"},	
#else
                        {"search_types", "0"},	
#endif								
							{"genre", genreId},														
							{"limit", limit},									
							{"sort", "time_desc"},
						};

				var books = await _client.SearchCatalog(parameters, cancellationToken);

				CheckMyBooks( books.Books );

				_noveltyBooksByGenreId = genreId;

				_noveltyBooksByGenre = books.Books;
			}
			else
			{
				int lastIndex = fromPosition + BooksInPage;

				XCollection<Book> collection = new XCollection<Book>( _noveltyBooksByGenre.Take( lastIndex ) );

				if (collection.Count() + BooksInPageShift < lastIndex)
				{
					lastIndex = collection.Count();

					string limit = string.Format( "{0},{1}", lastIndex, BooksInPage );

					var parameters = new Dictionary<string, object>
						{								
#if PDF_ENABLED	
                        {"search_types", "4"},	
#else
                        {"search_types", "0"},	
#endif
                        {"genre", genreId},													
							{"limit", limit},										
							{"sort", "time_desc"},		
						};

					var books = await _client.SearchCatalog(parameters, cancellationToken);

					if (books != null && books.Books != null && books.Books.Any())
					{
						CheckMyBooks( books.Books );

						foreach (var book in books.Books)
						{
							if (!collection.ContainsKey( book.GetKey() ))
							{
								collection.Add( book );
							}
						}
					}

					_noveltyBooksByGenre = collection;
				}

				return collection;
			}
			return _noveltyBooksByGenre;
		}
        public async Task<XCollection<Book>> GetPopularBooks(int fromPosition, CancellationToken cancellationToken, int customBooksOnPage = 0)
        {
            int booksOnPage = customBooksOnPage > 0 ? customBooksOnPage : BooksInPage;
            if (_popularBooks == null)
            {
                string limit = string.Format("{0},{1}", fromPosition, booksOnPage);

                var parameters = new Dictionary<string, object>
                {
                    {"sort", "pop_desc"},
                    {"limit", limit},
                };

                var books = await _client.SearchCatalog(parameters, cancellationToken);

                CheckMyBooks(books.Books);

                _popularBooks = books.Books;
            }
            else
            {
                int lastIndex = fromPosition + booksOnPage;

                var collection = new XCollection<Book>(_popularBooks.Take(lastIndex));

                if (collection.Count() + BooksInPageShift < lastIndex)
                {
                    lastIndex = collection.Count();

                    string limit = string.Format("{0},{1}", lastIndex, booksOnPage);

                    var parameters = new Dictionary<string, object>
                    {
                        {"sort", "pop_desc"},
                        {"limit", limit},
                    };

                    var books = await _client.SearchCatalog(parameters, cancellationToken);

                    if (books != null && books.Books != null && books.Books.Any())
                    {
                        CheckMyBooks(books.Books);

                        foreach (var book in books.Books)
                        {
                            if (!collection.ContainsKey(book.GetKey()))
                            {
                                collection.Add(book);
                            }
                        }
                    }

                    _popularBooks = collection;
                }
                _dataCacheService.PutItem(_popularBooks, PopularBooksCacheItemName, cancellationToken);
                return collection;
            }
            _dataCacheService.PutItem(_popularBooks, PopularBooksCacheItemName, cancellationToken);
            return _popularBooks;
        }
        public async Task<XCollection<Book>> GetPopularBooksByCollection(int fromPosition, int collectionId, CancellationToken cancellationToken)
        {
            if (_popularBooksByCollection == null || _popularBooksByCollectionIdCollection != collectionId)
            {
                var limit = string.Format("{0},{1}", fromPosition, BooksInPage);

                var parameters = new Dictionary<string, object>
						{	
							{"sort", "pop_desc"},
#if PDF_ENABLED	
                        {"search_types", "4"},	
#else
                        {"search_types", "0"},	
#endif									
							{"collection", collectionId},							
							{"limit", limit},
						};

                var books = await _client.SearchCatalog(parameters, cancellationToken);

                CheckMyBooks(books.Books);

                _popularBooksByCollection = books.Books;
                _popularBooksByCollectionIdCollection = collectionId;
            }
            else
            {
                int lastIndex = fromPosition + BooksInPage;

                var collection = new XCollection<Book>(_popularBooksByCollection.Take(lastIndex));

                if (collection.Count() + BooksInPageShift < lastIndex)
                {
                    lastIndex = collection.Count();

                    string limit = string.Format("{0},{1}", lastIndex, BooksInPage);

                    var parameters = new Dictionary<string, object>
						{	
                            {"sort", "pop_desc"},							
#if PDF_ENABLED	
                        {"search_types", "4"},	
#else
                        {"search_types", "0"},	
#endif									
							{"collection", collectionId},							
							{"limit", limit},
						};

                    var books = await _client.SearchCatalog(parameters, cancellationToken);

                    if (books != null && books.Books != null && books.Books.Any())
                    {
                        CheckMyBooks(books.Books);

                        foreach (var book in books.Books)
                        {
                            if (!collection.ContainsKey(book.GetKey()))
                            {
                                collection.Add(book);
                            }
                        }
                    }

                    _popularBooksByCollection = collection;
                    _popularBooksByCollectionIdCollection = collectionId;
                }

                return collection;
            }
            return _popularBooksByCollection;
        }
		public async Task<XCollection<Book>> SearchBooks(int fromPosition, string searchString, CancellationToken cancellationToken)
		{
			if (_foundedBooks == null || _foundedBooksQuery != searchString)
			{
				string limit = string.Format("{0},{1}", fromPosition, BooksInPage);

				var parameters = new Dictionary<string, object>
						{								
                        {"search_types", "0"},	

							{"search", searchString},					
							{"limit", limit},			
						};

				var books = await _client.SearchCatalog(parameters, cancellationToken);

				CheckMyBooks( books.Books );

				_foundedBooksQuery = searchString;
				_foundedBooks = books.Books;
			}
			else
			{
				int lastIndex = fromPosition + BooksInPage;

				var collection = new XCollection<Book>(_foundedBooks.Take(lastIndex));

				if (collection.Count() + BooksInPageShift < lastIndex)
				{
					lastIndex = collection.Count();

					string limit = string.Format("{0},{1}", lastIndex, BooksInPage);

					var parameters = new Dictionary<string, object>
						{								
#if PDF_ENABLED	
                        {"search_types", "4"},	
#else
                        {"search_types", "0"},	
#endif
							{"search_title", searchString},						
							{"limit", limit},
						};

					var books = await _client.SearchCatalog(parameters, cancellationToken);

					if (books != null && books.Books != null && books.Books.Any())
					{
						CheckMyBooks( books.Books );

						foreach (var book in books.Books)
						{
							if (!collection.ContainsKey(book.GetKey()))
							{
								collection.Add(book);
							}
						}
					}

					_foundedBooks = collection;
				}

				return collection;
			}
			return _foundedBooks;
		}
		public async Task<XCollection<Book>> GetBooksAreReadWithThisBook(int fromPosition, int bookId, CancellationToken cancellationToken)
		{
			if (_booksByBook == null || _booksByBookID != bookId)
			{
				//string limit = string.Format("{0},{1}", fromPosition, BooksInPage);
				//Get only 5 elements
				var limit = string.Format( "{0},{1}", 0, 5 );

				var parameters = new Dictionary<string, object>
						{								
#if PDF_ENABLED	
                        {"search_types", "4"},	
#else
                        {"search_types", "0"},	
#endif
							{"rating", "with"},
							{"art", bookId},				
							{"limit", limit},			
						};

				var books = await _client.SearchCatalog(parameters, cancellationToken);

				CheckMyBooks( books.Books );

				_booksByBookID = bookId;
				_booksByBook = books.Books;
			}
			else
			{
				int lastIndex = fromPosition + BooksInPage;

				var collection = new XCollection<Book>(_booksByBook.Take(lastIndex));

				if (collection.Count() + BooksInPageShift < lastIndex)
				{
					lastIndex = collection.Count();

					string limit = string.Format("{0},{1}", lastIndex, BooksInPage);

					var parameters = new Dictionary<string, object>
						{								
#if PDF_ENABLED	
                        {"search_types", "4"},	
#else
                        {"search_types", "0"},	
#endif
							{"rating", "with"},
							{"uuid", bookId},					
							{"limit", limit},
						};

					var books = await _client.SearchCatalog(parameters, cancellationToken);

					if (books != null && books.Books != null && books.Books.Any())
					{
						CheckMyBooks( books.Books );

						foreach (var book in books.Books)
						{
							if (!collection.ContainsKey(book.GetKey()))
							{
								collection.Add(book);
							}
						}
					}

					_booksByBook = collection;
				}

				return collection;
			}
			return _booksByBook;
		}
		public async Task<XCollection<Book>> GetBooksByAuthor(int fromPosition, string authorId, CancellationToken cancellationToken)
		{
			if (_booksByAuthor == null || _booksByAuthorID != authorId)
			{
				string limit = string.Format("{0},{1}", fromPosition, BooksInPage);

				var parameters = new Dictionary<string, object>
						{								
#if PDF_ENABLED	
                        {"search_types", "4"},	
#else
                        {"search_types", "0"},	
#endif
                            {"person", authorId},									
							{"limit", limit},			
						};

				var books = await _client.SearchCatalog(parameters, cancellationToken);

				CheckMyBooks( books.Books );

				_booksByAuthorID = authorId;
				_booksByAuthor = books.Books;
			}
			else
			{
				int lastIndex = fromPosition + BooksInPage;

				var collection = new XCollection<Book>(_booksByAuthor.Take(lastIndex));

				if (collection.Count() + BooksInPageShift < lastIndex)
				{
					lastIndex = collection.Count();

					string limit = string.Format("{0},{1}", lastIndex, BooksInPage);

					var parameters = new Dictionary<string, object>
						{								
#if PDF_ENABLED	
                        {"search_types", "4"},	
#else
                        {"search_types", "0"},	
#endif
							{"person", authorId},								
							{"limit", limit},
						};

					var books = await _client.SearchCatalog(parameters, cancellationToken);

					if (books != null && books.Books != null && books.Books.Any())
					{
						CheckMyBooks( books.Books );

						foreach (var book in books.Books)
						{
							if (!collection.ContainsKey(book.GetKey()))
							{
								collection.Add(book);
							}
						}
					}

					_booksByAuthor = collection;
				}

				return collection;
			}
			return _booksByAuthor;
		}