Guest User

Untitled

a guest
Nov 26th, 2014
173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.09 KB | None | 0 0
  1. BookCollection.findOne(newBookCollection, function(err, collection){
  2. if (err) {
  3. next(err);
  4. } else {
  5. if (collection) {
  6. collection.update({
  7. $inc: {
  8. quantity: 1
  9. }
  10. }, function(err){
  11. if (err) {
  12. next(err);
  13. } else {
  14. Book.findOne({
  15. isbn: collection.isbn
  16. }, function(err, book){
  17. res.json({
  18. result: 1,
  19. bookInfo: book
  20. });
  21. });
  22. }
  23. });
  24. } else {
  25. Book.findOne({
  26. isbn: isbn
  27. }, function(err, book){
  28. if (err) {
  29. next(err);
  30. } else {
  31. //Book not exist
  32. if (!book) {
  33. var googleBookApiUrl = provider.google.book + isbn + "&key=" + authKey.google.apiKey;
  34.  
  35. httpsRequest.get(googleBookApiUrl, function(err, data){
  36. if (err) {
  37. next(err);
  38. } else {
  39. if (data.totalItems > 0) {
  40. var volumeInfo = data.items[0].volumeInfo;
  41. var newBook = {};
  42. //Need to fix missing thumbnail
  43. var imageLinks = {};
  44.  
  45. //Filter incoming result
  46. if (volumeInfo.hasOwnProperty('imageLinks')) {
  47. imageLinks = volumeInfo.imageLinks;
  48. } else {
  49. imageLinks = {
  50. smallThumbnail: "https://lh4.googleusercontent.com/-by3TNG1Dhtw/AAAAAAAAAAI/AAAAAAAAAN0/gOuqGurlI8o/photo.jpg?sz=64",
  51. thumbnail: "https://lh4.googleusercontent.com/-by3TNG1Dhtw/AAAAAAAAAAI/AAAAAAAAAN0/gOuqGurlI8o/photo.jpg?sz=64"
  52. };
  53. }
  54.  
  55. newBook = {
  56. isbn: isbn,
  57. title: volumeInfo.title,
  58. subtitle: volumeInfo.subtitle,
  59. author: volumeInfo.authors,
  60. description: volumeInfo.description,
  61. contentVersion: volumeInfo.contentVersion,
  62. pageNumber: volumeInfo.pageCount,
  63. publisher: volumeInfo.publisher,
  64. publishDate: volumeInfo.publishedDate,
  65. categories: volumeInfo.categories,
  66. language: volumeInfo.language,
  67. printType: volumeInfo.printType,
  68. thumbnail: imageLinks
  69. };
  70.  
  71. Book.create(newBook, function(err, book){
  72. if (err) {
  73. next(err);
  74. } else {
  75. BookCollection.create(newBookCollection, function(err){
  76. if (err) {
  77. next(err);
  78. } else {
  79. res.json({
  80. result: 1,
  81. bookInfo: book
  82. });
  83. }
  84. });
  85. }
  86. });
  87. } else {
  88. res.json({
  89. result: 0,
  90. err: 'Unknown ISBN code',
  91. bookInfo: null
  92. });
  93. }
  94. }
  95. });
  96. } else {
  97. BookCollection.create(newBookCollection, function(err){
  98. if (err) {
  99. next(err);
  100. } else {
  101. res.json({
  102. result: 1,
  103. bookInfo: book
  104. });
  105. }
  106. });
  107. }
  108. }
  109. });
  110. }
  111. }
  112. });
Add Comment
Please, Sign In to add comment