public async static Task <ObservableCollection <Book> > SearchBooks(string searchTerm) { HttpClient client = new HttpClient(); try { string jsonString = await client.GetStringAsync($"https://www.googleapis.com/books/v1/volumes?q={searchTerm.Replace(' ','+')}"); BookSearchResult searchResult = JsonConvert.DeserializeObject <BookSearchResult>(jsonString); ObservableCollection <Book> books = new ObservableCollection <Book>(); foreach (var item in searchResult.items) { string isbn = null; if (item.volumeInfo?.industryIdentifiers != null && item.volumeInfo?.industryIdentifiers.Length > 0) { isbn = item.volumeInfo?.industryIdentifiers?[0].identifier; } Book book = new Book(item.volumeInfo?.title, item.volumeInfo?.description, item.volumeInfo?.authors, isbn, item.volumeInfo?.imageLinks?.smallThumbnail); books.Add(book); } return(books); } catch (Exception) { return(new ObservableCollection <Book>()); } }
public async Task UpdateBookPropertiesAsync(BookSearchResultViewModel book, UserBookPropertiesViewModel properties, [FromServices] IBookPropertiesLogic bookLogic) { try { var userId = this.User.FindFirstValue(ClaimTypes.NameIdentifier); var bookToSave = new BookSearchResult(); bookToSave.Title = book.Title; bookToSave.Description = book.Description; bookToSave.CoverImageUrl = book.CoverImageUrl; bookToSave.Rating = ParseExtensions.TryParseDouble(book.Rating); bookToSave.RatingCount = ParseExtensions.TryParseInt(book.RatingCount, 0).Value; bookToSave.PublishedDate = ParseExtensions.TryParseDateTime(book.PublishedDate); bookToSave.PageCount = ParseExtensions.TryParseInt(book.PageCount); bookToSave.Isbn = book.Isbn; if (book.Authors != null) { bookToSave.Authors = book.Authors.Split(","); } if (book.Genres != null) { bookToSave.Genres = book.Genres.Split(","); } await bookLogic.UpdateBookProperties(userId, bookToSave, properties.IsCurrentlyReading, properties.IsBookToRead, properties.IsInBookshelf, properties.IsInterested); } catch (Exception ex) { logger.LogError(ex, "Unable to update book properties."); Response.StatusCode = 500; } }
public async Task UpdateBookProperties(string userId, BookSearchResult bookSearchResult, bool isCurrentlyReading, bool isBookToRead, bool isInBookshelf, bool isInterested) { var book = new Book(); book.Title = bookSearchResult.Title; book.Description = bookSearchResult.Description; book.Rating = bookSearchResult.Rating; book.RatingCount = bookSearchResult.RatingCount; book.PublishedDate = bookSearchResult.PublishedDate; book.PageCount = bookSearchResult.PageCount; book.Isbn = bookSearchResult.Isbn; if (bookSearchResult.Authors != null) { book.Authors = bookSearchResult.Authors.Select(x => new Author(x)); } if (bookSearchResult.Genres != null) { book.Genres = bookSearchResult.Genres.Select(x => new Genre(x)); } var savedAuthors = await this.authorLogic.SaveAuthorsAsync(book.Authors); var savedGenres = await this.genreLogic.SaveGenresAsync(book.Genres); var savedBook = await this.bookLogic.GetBookByIsbnAsync(book.Isbn); if (savedBook == null) { book.CoverImage = await this.imageLogic.GetImageFromUrlAsync(bookSearchResult.CoverImageUrl); savedBook = await this.bookLogic.SaveBookAsync(book); } }
public static async Task <ObservableCollection <Book> > Suche(string suchbegriff) { HttpClient client = new HttpClient(); string jsonString = await client.GetStringAsync($"https://www.googleapis.com/books/v1/volumes?q={suchbegriff}"); await Task.Delay(1000); BookSearchResult result = JsonConvert.DeserializeObject <BookSearchResult>(jsonString); return(new ObservableCollection <Book>(result.items)); }
public SearchInBookResultItemDataModel ToDataModel( BookSearchResult searchResult, string queryString, string bookId) { var query = BookSearch.PrepareQuery(queryString); var item = new SearchInBookResultItemDataModel(); if (searchResult.PreviousContext.Any()) { item.TokenId = searchResult.PreviousContext[0].ID; } else { item.TokenId = searchResult.SearchResult[0].ID; } item.BookId = bookId; var firstWord = query.First(); var lastWord = query.Last(); var firstToken = searchResult.SearchResult.First(); var lastToken = searchResult.SearchResult.Last(); var beforeWords = searchResult.PreviousContext.Select(r => r.Text).ToList(); var afterWords = searchResult.NextContext.Select(r => r.Text).ToList(); var intermediateWords = searchResult.SearchResult.Skip(1).Take(searchResult.SearchResult.Count - 2).Select(r => r.Text).ToList(); var firstWordIndex = firstToken.Text.IndexOf(firstWord, StringComparison.InvariantCultureIgnoreCase); beforeWords.Add(firstToken.Text.Substring(0, firstWordIndex)); intermediateWords.Insert(0, firstToken.Text.Substring(firstWordIndex, firstWord.Length)); var lastWordIndex = lastToken.Text.IndexOf(lastWord, StringComparison.InvariantCultureIgnoreCase); afterWords.Insert(0, lastToken.Text.Substring(lastWordIndex + lastWord.Length)); if (query.Count > 1) { intermediateWords.Add(lastToken.Text.Substring(lastWordIndex, lastWord.Length)); } item.TextBefore = string.Join(" ", beforeWords); item.SearchedText = string.Join(" ", intermediateWords); item.TextAfter = string.Join(" ", afterWords); return(item); }
public ActionResult BookList() { BookSearchResult model = new BookSearchResult(); ISitecoreService service = new SitecoreService(Sitecore.Context.Database); SitecoreIndexableItem contextItem = Sitecore.Context.Item; SearchResults <CustomSearchResultItem> results; using (var context = ContentSearchManager.GetIndex(contextItem).CreateSearchContext()) { IQueryable <CustomSearchResultItem> query = context.GetQueryable <CustomSearchResultItem>(); query = query.Where(f => f.Paths.Contains(Sitecore.Context.Item.ID) && f.TemplateId == BookTemplate.Book.ID && f.Language == Sitecore.Context.Language.Name).Page(0, 30); results = query.GetResults(); } List <CustomSearchResultItem> resultItems = results?.Hits.Select(f => f.Document).ToList(); List <IBook> books = new List <IBook>(); //List<BookItem> bookItems = new List<BookItem>(); foreach (var bookItem in resultItems) { books.Add(service.GetItem <IBook>(bookItem.ItemId.ToGuid())); //bookItems.Add(new BookItem // { // Title = bookItem.Title, // Publisher = bookItem.Publisher, // AuthorName = bookItem.AuthorName // }); //model.BookItems = bookItems; model.Books = books; } #region Facets //List<Facet> facets = new List<Facet>(); //foreach(var category in results.Facets?.Categories) //{ // foreach(var facet in category.Values) // { // facets.Add(new Facet { Name = facet.Name, Count = facet.AggregateCount }); // } //} //model.Facets = facets; #endregion return(View(model)); }
//Tuple: https://docs.microsoft.com/de-de/dotnet/csharp/tuples public async static Task <(List <Book> books, string status)> SearchBooks(string searchterm) { try { HttpClient client = new HttpClient(); string json = await client.GetStringAsync($"https://www.googleapis.com/books/v1/volumes?q={searchterm.Replace(' ', '+')}"); //NuGet-Package: Newtonsoft.Json //Deserialisierung: Umwandlung eines strings in ein .NET-Objekt BookSearchResult result = JsonConvert.DeserializeObject <BookSearchResult>(json); List <Book> books = new List <Book>(); foreach (var item in result.items) { Book newBook = new Book(item.id, item.volumeInfo?.title, item.volumeInfo?.authors, item.volumeInfo?.previewLink, item.volumeInfo?.imageLinks?.smallThumbnail); books.Add(newBook); } if (books.Count > 0) { LastSearchResult = books; SuccessfulSearch?.Invoke(null, EventArgs.Empty); } return(books, "OK"); } catch (Exception exp) { return(null, exp.Message); } }
private bool ShowResult(bool IsNextPage) { if (!IsNextPage) { bookSearchResult.ItemsSource = null; try { b = JsonConvert.DeserializeObject<BookSearchResult>(jsonData); } catch { return false; } } if(b.errMsg == "图书馆请求信息失败") { NotifyPopup notifyPopup = new NotifyPopup(b.errMsg); notifyPopup.Show(); return false; } bookSearchResult.ItemsSource = b.data; page++; return true; }
public async Task <IEnumerable <BookSearchResult> > SearchBooksAsync(string searchQuery) { var apiResults = await apiClient.SearchBooksAsync(searchQuery); var results = new List <BookSearchResult>(); foreach (var apiResult in apiResults.Items) { if (apiResult.VolumeInfo == null) { continue; } var result = new BookSearchResult(); result.Title = apiResult.VolumeInfo.Title; result.Description = apiResult.VolumeInfo.Description; result.CoverImageUrl = apiResult.VolumeInfo.ImageLinks.Thumbnail; result.Rating = ParseExtensions.TryParseDouble(apiResult.VolumeInfo.AverageRating); result.RatingCount = ParseExtensions.TryParseInt(apiResult.VolumeInfo.RatingsCount, 0).Value; result.PublishedDate = ParseExtensions.TryParseDateTime(apiResult.VolumeInfo.PublishedDate, null); result.PageCount = ParseExtensions.TryParseInt(apiResult.VolumeInfo.PageCount); result.Authors = apiResult.VolumeInfo.Authors; result.Genres = apiResult.VolumeInfo.Categories; result.PublishedDate = ParseExtensions.TryParseDateTime(apiResult.VolumeInfo.PublishedDate); // sometimes the API returns only the year if (result.PublishedDate == null && apiResult.VolumeInfo.PublishedDate.Length == 4) { int year; if (int.TryParse(apiResult.VolumeInfo.PublishedDate, out year)) { result.PublishedDate = new DateTime(year, 1, 1); } } if (apiResult.VolumeInfo.IndustryIdentifiers != null) { var isbn = apiResult.VolumeInfo.IndustryIdentifiers.Where(x => x.Type == "ISBN_13").FirstOrDefault(); if (isbn == null) { isbn = apiResult.VolumeInfo.IndustryIdentifiers.Where(x => x.Type == "ISBN_10").FirstOrDefault(); } if (isbn == null) { isbn = apiResult.VolumeInfo.IndustryIdentifiers.Where(x => x.Type == "ISBN").FirstOrDefault(); } if (isbn != null) { result.Isbn = isbn.Identifier; } } results.Add(result); } return(results); }
private async void loadMore_Click(object sender, RoutedEventArgs e) { refreshIndicator.IsActive = true; jsonData = await HttpRequest.GetBookSearchResult(key, page.ToString()); try { l = JsonConvert.DeserializeObject<BookSearchResult>(jsonData); } catch { refreshIndicator.IsActive = false; return; } b.errMsg = l.errMsg; if (b.errMsg == "图书馆请求信息失败") { loadMore.Content = "没有更多内容"; loadMore.IsEnabled = false; refreshIndicator.IsActive = false; return; } b.data = b.data.Concat(l.data).ToList(); //System.Diagnostics.Debug.WriteLine(b.data.Count); ShowResult(true); refreshIndicator.IsActive = false; }