bookManagement/library.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

103 lines
4.2 KiB
JavaScript

function checkoutBook(bookId, userId) {
// Check if the book is available
Book.findByPk(bookId).then(book => {
if (book.status === 'Available') {
// If available, create a new Checkout record
Checkout.create({ book_id: bookId, user_id: userId }).then(checkout => {
// Update the Book status to 'Checked Out'
book.update({ status: 'Checked Out' }).then(() => {
// Return the Checkout record
return checkout;
});
});
} else {
// If the book is not available, return null
return null;
}
});
}
function returnBook(bookId) {
// Find the Checkout record for the book
Checkout.findOne({ where: { book_id: bookId, returned_date: null } }).then(checkout => {
if (checkout) {
// Update the Checkout record with the returned date
checkout.update({ returned_date: new Date() }).then(() => {
// Update the Book status to 'Available'
Book.findByPk(bookId).then(book => {
book.update({ status: 'Available' });
});
});
}
});
}
function formatOpenLibraryData(data) {
return {
isbn: data.identifiers.isbn_13 ? data.identifiers.isbn_13[0] : '',
title: data.title,
authors: data.authors ? data.authors.map(author => author.name) : [],
publishedDate: data.publish_date,
description: data.excerpts ? data.excerpts[0].text : 'No description available',
url: data.url,
number_of_pages: data.number_of_pages || null,
identifiers: JSON.stringify(data.identifiers), // Store as JSON string
publishers: data.publishers ? data.publishers.map(pub => pub.name).join(', ') : '',
subjects: data.subjects ? data.subjects.map(sub => sub.name).join(', ') : '',
notes: data.notes || '',
cover_small: data.cover ? data.cover.small : '',
cover_medium: data.cover ? data.cover.medium : '',
cover_large: data.cover ? data.cover.large : ''
};
}
function formatArchiveData(data) {
return {
isbn: data.isbn ? data.isbn[0] : '',
title: data.title,
authors: data.contributor ? [data.contributor] : [],
publishedDate: data.date ? new Date(data.date).getFullYear() : '',
description: data.description ? data.description.join(' ') : 'No description available',
url: `https://archive.org/details/${data.identifier}`,
number_of_pages: data.imagecount || null,
identifiers: JSON.stringify({
archive_identifier: data.identifier,
oclc: data['external-identifier'] ? data['external-identifier'].filter(id => id.includes('urn:oclc')).map(id => id.split(':')[2]) : []
}),
publishers: data.publisher || '',
subjects: data.subject ? data.subject.join(', ') : '',
notes: '', // Archive data does not provide specific notes like Open Library
cover_small: '', // Placeholder, as cover image URLs need to be constructed manually
cover_medium: '', // Same as above
cover_large: '' // Same as above
};
}
function formatGoogleBooksData(data) {
return {
isbn: data.volumeInfo.industryIdentifiers ? data.volumeInfo.industryIdentifiers.find(id => id.type === 'ISBN_13').identifier : '',
title: data.volumeInfo.title,
authors: data.volumeInfo.authors || [],
publishedDate: data.volumeInfo.publishedDate,
description: data.volumeInfo.description || 'No description available',
url: data.volumeInfo.previewLink,
number_of_pages: data.volumeInfo.pageCount || null,
identifiers: JSON.stringify(data.volumeInfo.industryIdentifiers),
publishers: data.volumeInfo.publisher || '',
subjects: data.volumeInfo.categories || [],
notes: '', // Placeholder for additional notes
cover_small: data.volumeInfo.imageLinks.smallThumbnail || '',
cover_medium: data.volumeInfo.imageLinks.thumbnail || '',
cover_large: data.volumeInfo.imageLinks.small || ''
};
}
module.exports = {
checkoutBook,
returnBook,
formatOpenLibraryData,
formatArchiveData,
formatGoogleBooksData
};