Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- BookCollection.findOne(newBookCollection, function(err, collection){
- if (err) {
- next(err);
- } else {
- if (collection) {
- collection.update({
- $inc: {
- quantity: 1
- }
- }, function(err){
- if (err) {
- next(err);
- } else {
- Book.findOne({
- isbn: collection.isbn
- }, function(err, book){
- res.json({
- result: 1,
- bookInfo: book
- });
- });
- }
- });
- } else {
- Book.findOne({
- isbn: isbn
- }, function(err, book){
- if (err) {
- next(err);
- } else {
- //Book not exist
- if (!book) {
- var googleBookApiUrl = provider.google.book + isbn + "&key=" + authKey.google.apiKey;
- httpsRequest.get(googleBookApiUrl, function(err, data){
- if (err) {
- next(err);
- } else {
- if (data.totalItems > 0) {
- var volumeInfo = data.items[0].volumeInfo;
- var newBook = {};
- //Need to fix missing thumbnail
- var imageLinks = {};
- //Filter incoming result
- if (volumeInfo.hasOwnProperty('imageLinks')) {
- imageLinks = volumeInfo.imageLinks;
- } else {
- imageLinks = {
- smallThumbnail: "https://lh4.googleusercontent.com/-by3TNG1Dhtw/AAAAAAAAAAI/AAAAAAAAAN0/gOuqGurlI8o/photo.jpg?sz=64",
- thumbnail: "https://lh4.googleusercontent.com/-by3TNG1Dhtw/AAAAAAAAAAI/AAAAAAAAAN0/gOuqGurlI8o/photo.jpg?sz=64"
- };
- }
- newBook = {
- isbn: isbn,
- title: volumeInfo.title,
- subtitle: volumeInfo.subtitle,
- author: volumeInfo.authors,
- description: volumeInfo.description,
- contentVersion: volumeInfo.contentVersion,
- pageNumber: volumeInfo.pageCount,
- publisher: volumeInfo.publisher,
- publishDate: volumeInfo.publishedDate,
- categories: volumeInfo.categories,
- language: volumeInfo.language,
- printType: volumeInfo.printType,
- thumbnail: imageLinks
- };
- Book.create(newBook, function(err, book){
- if (err) {
- next(err);
- } else {
- BookCollection.create(newBookCollection, function(err){
- if (err) {
- next(err);
- } else {
- res.json({
- result: 1,
- bookInfo: book
- });
- }
- });
- }
- });
- } else {
- res.json({
- result: 0,
- err: 'Unknown ISBN code',
- bookInfo: null
- });
- }
- }
- });
- } else {
- BookCollection.create(newBookCollection, function(err){
- if (err) {
- next(err);
- } else {
- res.json({
- result: 1,
- bookInfo: book
- });
- }
- });
- }
- }
- });
- }
- }
- });
Add Comment
Please, Sign In to add comment