コード例 #1
0
        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;
            }
        }
コード例 #2
0
        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);
        }