示例#1
0
        public ActionResult AddBook(FormCollection collection)
        {
            try
            {
                //9780596007126 - Head First Design Patterns
                string               isbn            = collection["isbn"];
                HttpWebRequest       request         = (HttpWebRequest)WebRequest.Create("https://www.googleapis.com/books/v1/volumes?q=isbn:" + isbn + "&key=AIzaSyARYJrBPVJ9B77JveSDkwPI5IvWUGVHe1M");
                GoogleBooksApiReader bookInformation = new GoogleBooksApiReader((HttpWebResponse)request.GetResponse());
                if (bookInformation.isValid())
                {
                    BooksModel addBook = new BooksModel(bookInformation);

                    db.BooksModels.Add(addBook);
                    db.SaveChanges();
                    ViewBag.bookIsbnException = null;
                    return(RedirectToAction("Index"));
                }
                else
                {
                    ViewBag.bookIsbnException = "Wrong ISBN code!";
                    throw new Exception("Wrong ISBN code");
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                return(View());
            }
        }
 public BooksModel(GoogleBooksApiReader bookInformation)
 {
     isbn          = bookInformation.getISBN13().Count() != 0 ? bookInformation.getISBN13() : bookInformation.getISBN10();
     title         = bookInformation.getTitle();
     authors       = bookInformation.getAuthors();
     pages         = bookInformation.getNumberOfPages();
     publisher     = bookInformation.getPublisher();
     publishedDate = bookInformation.getPublishingDate();
     rating        = bookInformation.getAverageRating();
     imagePath     = bookInformation.getThumbnailImage();
     description   = bookInformation.getDescription();
 }