-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-tasks-10-13.js
More file actions
117 lines (101 loc) Β· 4.37 KB
/
Copy pathtest-tasks-10-13.js
File metadata and controls
117 lines (101 loc) Β· 4.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
const axios = require('axios');
const API_BASE = 'http://127.0.0.1:5000/api/v1';
// Configure axios with reasonable timeouts
axios.defaults.timeout = 10000;
console.log('='.repeat(70));
console.log('Book Review API - Tasks 10-13: Async/Callback & Promises with Axios');
console.log('='.repeat(70));
// ============ TASK 10: Get All Books Using Async Callback ============
console.log('\nπ TASK 10: Get All Books - Using Async Callback Function');
console.log('-'.repeat(70));
function getAllBooksWithCallback(callback) {
axios.get(`${API_BASE}/books`)
.then(response => {
callback(null, response.data);
})
.catch(error => {
console.error('Full error details:', error.message, error.code);
callback(error, null);
});
}
getAllBooksWithCallback((err, books) => {
if (err) {
console.error('β Error fetching all books:', err.message);
} else {
console.log('β
Successfully fetched all books!');
console.log(`π Total books: ${books.length}`);
books.forEach((book, idx) => {
console.log(` ${idx + 1}. ${book.title} by ${book.author}`);
console.log(` ISBN: ${book.ISBN}`);
});
}
// ============ TASK 11: Search by ISBN Using Promises ============
console.log('\nπ TASK 11: Search by ISBN - Using Promises');
console.log('-'.repeat(70));
const isbnToSearch = '978-0-13-110362-7'; // Clean Code
axios.get(`${API_BASE}/books/isbn/${isbnToSearch}`)
.then(response => {
console.log('β
Successfully found book by ISBN!');
const book = response.data;
console.log(` Title: ${book.title}`);
console.log(` Author: ${book.author}`);
console.log(` ISBN: ${book.ISBN}`);
console.log(` Reviews: ${book.reviews.length}`);
return book;
})
.catch(error => {
console.error('β Error searching by ISBN:', error.message);
})
// ============ TASK 12: Search by Author Using Promises ============
.then(book => {
console.log('\nπ TASK 12: Search by Author - Using Promises');
console.log('-'.repeat(70));
const authorToSearch = 'Robert C. Martin';
return axios.get(`${API_BASE}/books/author/${encodeURIComponent(authorToSearch)}`)
.then(response => {
console.log(`β
Successfully found books by author: ${authorToSearch}`);
const authorBooks = response.data;
console.log(`π Found ${authorBooks.length} book(s):`);
authorBooks.forEach((b, idx) => {
console.log(` ${idx + 1}. ${b.title} (ISBN: ${b.ISBN})`);
});
return authorBooks;
})
.catch(error => {
console.error('β Error searching by author:', error.message);
});
})
// ============ TASK 13: Search by Title Using Promises ============
.then(authorBooks => {
console.log('\nπ TASK 13: Search by Title - Using Promises');
console.log('-'.repeat(70));
const titleToSearch = 'Clean Code';
return axios.get(`${API_BASE}/books/title/${encodeURIComponent(titleToSearch)}`)
.then(response => {
console.log(`β
Successfully found books with title containing: "${titleToSearch}"`);
const titleBooks = response.data;
console.log(`π Found ${titleBooks.length} book(s):`);
titleBooks.forEach((b, idx) => {
console.log(` ${idx + 1}. ${b.title}`);
console.log(` by ${b.author}`);
console.log(` ISBN: ${b.ISBN}`);
});
// Final summary
console.log('\n' + '='.repeat(70));
console.log('β
ALL TASKS 10-13 COMPLETED SUCCESSFULLY!');
console.log('='.repeat(70));
console.log('\nSummary:');
console.log(' Task 10: β
Fetched all books using async callback');
console.log(' Task 11: β
Searched by ISBN using Promises');
console.log(' Task 12: β
Searched by author using Promises');
console.log(' Task 13: β
Searched by title using Promises');
console.log('\nScreenshot instructions:');
console.log(' - Capture the full console output above');
console.log(' - Save as: 10-asynccallback.png, 11-promiseisbn.png, etc.');
console.log('='.repeat(70) + '\n');
})
.catch(error => {
console.error('β Error searching by title:', error.message);
});
});
});