-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbook_service.js
69 lines (61 loc) · 2.05 KB
/
book_service.js
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
import axios from 'axios';
import { jsonReader, jsonWriter, errorLog, successLog } from './util.js';
import { isNewBook } from './validations.js';
const URL = 'https://www.googleapis.com/books/v1/volumes?q=';
export default class BookService {
constructor(filePath, api_key) {
this.readingListFilePath = filePath;
this.url = URL;
this.fetchedBooks = [];
this.api_key = api_key;
}
fetchBooks = async (searchTerm) => {
const search = `${this.url}${searchTerm}&maxResults=5&key=${this.api_key}`;
let response;
try {
response = await axios.get(search, { headers: { Accept: 'applicaiton/json' } });
} catch (err) {
errorLog(err);
return;
}
this.fetchedBooks = this.formatBooks(response.data.items);
return this.fetchedBooks;
};
formatBooks = (books) => {
return books.map((book) => {
return {
title: book.volumeInfo.title,
author: book.volumeInfo.authors ? book.volumeInfo.authors.join(', ') : 'N/A',
publisher: book.volumeInfo.publisher || 'N/A',
};
});
};
saveBook = (bookNumber) => {
let idx = parseInt(bookNumber) - 1;
let book = this.fetchedBooks[idx];
let readingList = [];
try {
readingList = this.fetchReadingList();
if (!isNewBook(readingList, book)) {
errorLog('This book is already on your reading list!');
return;
}
readingList.push(book);
} catch {
readingList = [book];
}
try {
jsonWriter(this.readingListFilePath, readingList);
successLog('Book successfully saved!');
} catch (err) {
errorLog(err);
}
};
fetchReadingList = () => {
const readingList = jsonReader(this.readingListFilePath);
if (!readingList) {
throw new Error('Your Reading List is empty!');
}
return readingList;
};
}