bookManagement/bookHelpers.js
knight 3de9f3d8ee Refactor book management system:
- Updated .gitignore to exclude node_modules directory.
- Enhanced index.js with new endpoints for book fetching and checkout requests, integrating nodemailer and express-rate-limit for email notifications and request management.
- Added functionality to confirm book presence in the library and improved error handling for external book sources.
- Updated package.json and package-lock.json to include new dependencies (nodemailer, express-rate-limit) and their respective versions.
- Modified public HTML and JavaScript files to support new features, including a confirm mode for book scanning and improved UI elements.
- Updated styles for better user experience in the library interface.
2024-12-11 09:41:22 -05:00

95 lines
3.4 KiB
JavaScript

const { Book, Location, Checkout, User, Sequelize, Op } = require('./models');
// Fetch book from the local database by ISBN
const fetchBookFromLocalDatabase = async (isbn) => {
return await Book.findOne({ where: { isbn } });
};
// Fetch book from Google Books API
const fetchBookFromGoogleBooks = async (isbn, apiKey) => {
try {
const response = await axios.get(`https://www.googleapis.com/books/v1/volumes?q=${isbn}&key=${apiKey}`);
if (response.data.items && response.data.items.length > 0) {
return library.formatGoogleBooksData(response.data.items[0]);
}
return null;
} catch (error) {
if (error.response && (error.response.status === 429 || error.response.status === 408)) {
throw error.response.status;
}
throw new Error('Google Books API Error');
}
};
// Fetch book from Open Library API
const fetchBookFromOpenLibrary = async (isbn) => {
try {
const response = await axios.get(`https://openlibrary.org/api/books?bibkeys=ISBN:${isbn}&format=json&jscmd=data`);
return response.data[`ISBN:${isbn}`] ? library.formatOpenLibraryData(response.data[`ISBN:${isbn}`]) : null;
} catch (error) {
if (error.response && (error.response.status === 429 || error.response.status === 408)) {
throw error.response.status;
}
throw new Error('Open Library API Error');
}
};
// Fetch book from the Internet Archive
const fetchBookFromInternetArchive = async (isbn) => {
try {
const response = await axios.get(`https://openlibrary.org/api/books?bibkeys=ISBN:${isbn}&format=json&jscmd=data`);
return response.data[`ISBN:${isbn}`] ? library.formatArchiveData(response.data[`ISBN:${isbn}`]) : null;
} catch (error) {
throw new Error('Internet Archive API Error');
}
};
// Search books in the local database by title or related fields
const searchBooksInLocalDatabase = async (title, searchDescription=false) => {
if (!title) {
// Return all books if the title is empty
return await Book.findAll();
}
const searchConditions = [
{ title: { [Op.like]: `%${title}%` } },
{ authors: { [Op.like]: `%${title}%` } },
{ publishers: { [Op.like]: `%${title}%` } },
{ subjects: { [Op.like]: `%${title}%` } }
];
if (searchDescription) {
searchConditions.push({ description: { [Op.like]: `%${title}%` } });
}
return await Book.findAll({
where: {
[Op.or]: searchConditions
}
});
};
// Search books in Open Library by title
const searchBooksInOpenLibrary = async (title) => {
try {
const response = await axios.get(`https://openlibrary.org/search.json?q=${encodeURIComponent(title)}&limit=7`);
return response.data.docs.map(result => ({
title: result.title,
authors: result.author_name || [],
publish_date: result.first_publish_year,
isbn: result.isbn ? result.isbn[0] : '',
publisher: result.publisher ? result.publisher[0] : '',
key: result.key
}));
} catch (error) {
throw new Error('Open Library API Error');
}
};
module.exports = {
fetchBookFromLocalDatabase,
fetchBookFromGoogleBooks,
fetchBookFromOpenLibrary,
fetchBookFromInternetArchive,
searchBooksInLocalDatabase,
searchBooksInOpenLibrary
};