const { Book, Location, Checkout, User, Sequelize, Op } = require('./models'); const axios = require('axios'); // Fetch book from the local database by ISBN const fetchBookFromLocalDatabase = async (isbn) => { return await Book.findOne({ where: { isbn }, include: [{ model: Location, as: 'Location' }] }); }; // 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 };