Exemplo n.º 1
0
        public async Task <ActionResult> Update()
        {
            DirectoryScanner directoryScanner = new DirectoryScanner();
            BookInfoScanner  infoScanner      = new BookInfoScanner();

            foreach (MediaDirectory dir in _context.MediaDirectories.ToList())
            {
                List <AudioBook> scannedBooks = directoryScanner.ScanForBooks(dir.DirectoryPath);
                foreach (AudioBook scannedBook in scannedBooks)
                {
                    if (_context.AudioBooks.Any(x => x.AudioBookDirectoryPath == scannedBook.AudioBookDirectoryPath))
                    {
                        continue;
                    }
                    //todo: info scanning

                    EntityEntry <AudioBook> bookEntry = _context.AudioBooks.Add(scannedBook);
                    var entitesCreated = await _context.SaveChangesAsync();

                    //todo: log entries created
                }
            }

            return(Ok());
        }
Exemplo n.º 2
0
        public async Task <ActionResult <List <GoogleBook> > > Search([FromQuery] string type  = "",
                                                                      [FromQuery] string query = "")
        {
            try
            {
                BookInfoScanner scanner = new BookInfoScanner();
                switch (type)
                {
                case "title":
                    return(Ok(await scanner.SearchByTitleAsync(query)));

                case "isbn":
                    return
                        (Ok(await scanner.SearchBookByISBNAsync(query)));

                default:
                    return(Ok(await scanner.SearchGeneralAsync(query)));
                }
            }
            catch (Exception ex)
            {
                _logger
                .LogError($"Error fetching books query: ({type}){query}: {ex.Message}");
                return(StatusCode((int)HttpStatusCode
                                  .InternalServerError));
            }
        }
Exemplo n.º 3
0
        public void SearchByISBN()
        {
            BookInfoScanner   scanner = new BookInfoScanner();
            List <GoogleBook> tradingInDangerResults = scanner.SearchBookByISBNAsync("0-345-44760-3").Result;

            Assert.Single(tradingInDangerResults);
            // Assert.Equal("Trading in Danger", tradingInDangerResults[0].VolumeInfo.Title);
        }
Exemplo n.º 4
0
        public async Task <ActionResult> Confirm([FromBody] BookInfoConfirmationModel model)
        {
            try
            {
                BookInfoScanner scanner = new BookInfoScanner();
                var             gBook   = await scanner.GetGoogleBookAsync(model.GoogleBooksId);

                if (gBook == null)
                {
                    return(BadRequest($"Google volume '{model.GoogleBooksId}' not found"));
                }

                AudioBook kBook = await _context.AudioBooks.SingleOrDefaultAsync(x => x.Id == model.KikiBookId);

                if (kBook == null)
                {
                    return(BadRequest($"Kiki audiobook with ID '{model.KikiBookId.ToString()}' not found"));
                }

                kBook.Title = gBook.Title;
                foreach (string newAuthor in gBook.Authors.Where(x => kBook.Authors.All(a => a.Author.Name != x)))
                {
                    if (_context.Authors.All(x => x.Name != newAuthor))
                    {
                        kBook.Authors.Add(new BookAuthor(kBook, new Author(newAuthor)));
                    }
                    else
                    {
                        var author = await _context.Authors.SingleOrDefaultAsync(x => x.Name == newAuthor);

                        kBook.Authors.Add(new BookAuthor(kBook, author));
                    }
                }

                kBook.Description = gBook.Description;

                if (!string.IsNullOrEmpty(gBook.ThumbnailLink))
                {
                    using (HttpClient client = new HttpClient())
                    {
                        var thumbnailBytes = await client.GetByteArrayAsync(gBook.ThumbnailLink);

                        kBook.ThumbnailData = Convert.ToBase64String(thumbnailBytes);
                    }
                }

                kBook.Language = gBook.Language;
                if (!string.IsNullOrEmpty(gBook.Publisher))
                {
                    if (_context.Publishers.All(x => x.Name != gBook.Publisher))
                    {
                        kBook.Publisher = new Publisher()
                        {
                            Name = gBook.Publisher
                        };
                    }
                    else
                    {
                        kBook.Publisher =
                            await _context.Publishers.SingleOrDefaultAsync(x => x.Name == gBook.Publisher);
                    }
                }

                if (gBook.Published.HasValue)
                {
                    kBook.Year = gBook.Published.Value.Year;
                }

                kBook.GoogleBooksID = gBook.GoogleBooksID;

                foreach (var identifier in gBook.IndustryIdentifiers)
                {
                    if (identifier.IdentificationType == "ISBN10")
                    {
                        kBook.ISBN10 = identifier.IdentificationCode;
                    }
                    else if (identifier.IdentificationType == "ISBN13")
                    {
                        kBook.ISBN13 = identifier.IdentificationCode;
                    }
                }

                var changed = await _context.SaveChangesAsync();

                return(Ok());
            }
            catch (Exception ex)
            {
                if (ex is GoogleApiException gae)
                {
                    string err = $"Google API error: {gae.Error.Message}";
                    _logger.LogError(err);
                    return(BadRequest(err));
                }
                if (ex is DbUpdateException due)
                {
                    _logger.LogError($"Error saving changes to book with ID {model.KikiBookId} from Google book {model.GoogleBooksId}: {due.Message}");
                }
                else
                {
                    _logger.LogError($"Unexpected error while updating book: {ex.Message}");
                }

                return(StatusCode((int)HttpStatusCode.InternalServerError));
            }
        }