diff --git a/controllers/bookController.js b/controllers/bookController.js index bbd2da283e2eaf4c1cbf8bf09c3005cb526ca5da..3b799f86cfd1dd1c7496ce9ef8cd3908641d3623 100644 --- a/controllers/bookController.js +++ b/controllers/bookController.js @@ -146,7 +146,6 @@ exports.getEditBook = async (req, res) => { const bookModel = new BookModel(req.db); try { - // Check if the book belongs to the current user const book = await bookModel.getBookByIdAndUser(book_id, req.session.user.user_id); if (!book) { @@ -154,8 +153,8 @@ exports.getEditBook = async (req, res) => { } res.render('addbook', { - book, isEditing: true, + book: book, list_id: book.list_id }); } catch (error) { @@ -169,32 +168,46 @@ exports.postEditBook = async (req, res) => { return res.redirect('/login'); } - const { book_id } = req.params; - const bookModel = new BookModel(req.db); - - try { - // Check if the book belongs to the current user - const existingBook = await bookModel.getBookByIdAndUser(book_id, req.session.user.user_id); - - if (!existingBook) { - return res.status(403).send('You do not have permission to edit this book'); + upload(req, res, async (err) => { + if (err) { + return res.status(400).send('Error uploading file: ' + err.message); } - // Process update if user owns the book - const { bookname, author, category, type, image_url } = req.body; - await bookModel.updateBook(book_id, req.session.user.user_id, { - bookname, - author, - category, - type, - image_url - }); + try { + const { book_id } = req.params; + const { bookname, author, category, type, imageOption } = req.body; + + // First check if book exists and user has permission + const bookModel = new BookModel(req.db); + const existingBook = await bookModel.getBookByIdAndUser(book_id, req.session.user.user_id); + + if (!existingBook) { + return res.status(403).send('You do not have permission to edit this book'); + } - res.redirect(`/books/${existingBook.list_id}`); - } catch (error) { - console.error('Error updating book:', error); - res.status(500).send('Error updating book information'); - } + // Determine image URL + let image_url = existingBook.image_url; // Keep existing image by default + if (imageOption === 'file' && req.file) { + image_url = `/uploads/${req.file.filename}`; + } else if (imageOption === 'url' && req.body.image_url) { + image_url = req.body.image_url; + } + + // Update book + await bookModel.updateBook(book_id, req.session.user.user_id, { + bookname, + author, + category, + type, + image_url + }); + + res.redirect(`/books/${existingBook.list_id}`); + } catch (error) { + console.error('Error updating book:', error); + res.status(500).send('Error updating book information'); + } + }); }; exports.deleteBook = async (req, res) => { diff --git a/models/bookModel.js b/models/bookModel.js index 863305d7543942e0d32c30ac3a417f0d57771371..0285f7cb63c223a6f48f31d58f52683b235c8d4d 100644 --- a/models/bookModel.js +++ b/models/bookModel.js @@ -94,7 +94,7 @@ module.exports = class BookModel { }); } - async updateBook(book_id, bookname, author, category, type, image_url, user_id) { + async updateBook(book_id, user_id, { bookname, author, category, type, image_url }) { return new Promise((resolve, reject) => { const sql = ` UPDATE books @@ -109,7 +109,11 @@ module.exports = class BookModel { sql, [bookname, author, category, type, image_url, book_id, user_id], (err, results) => { - if (err) reject(err); + if (err) { + console.error('Database error:', err); + reject(err); + return; + } resolve(results); } ); @@ -249,18 +253,5 @@ module.exports = class BookModel { ); }); } - - async updateBook(book_id, user_id, { bookname, author, category, type, image_url }) { - return new Promise((resolve, reject) => { - this.db.query( - 'UPDATE books SET bookname = ?, author = ?, category = ?, type = ?, image_url = ? WHERE book_id = ? AND user_id = ?', - [bookname, author, category, type, image_url, book_id, user_id], - (err, results) => { - if (err) reject(err); - resolve(results); - } - ); - }); - } }; diff --git a/public/uploads/1742550823551-Evelyn.jpg b/public/uploads/1742550823551-Evelyn.jpg new file mode 100644 index 0000000000000000000000000000000000000000..2e02ff8150136df5f5372ef50476e272193bb0d2 Binary files /dev/null and b/public/uploads/1742550823551-Evelyn.jpg differ diff --git a/views/addbook.ejs b/views/addbook.ejs index 1aa2a1273b79af4f7a3428688839bf3a7f1027e3..effbf9fe768614c102d14d73e70ae9224bc7ff96 100644 --- a/views/addbook.ejs +++ b/views/addbook.ejs @@ -40,27 +40,36 @@ <input type="text" id="type" name="type" value="<%= isEditing ? book.type : '' %>" required> </div> + <input type="hidden" name="book_id" value="<%= isEditing ? book.book_id : '' %>"> + <input type="hidden" name="list_id" value="<%= list_id %>"> + <div class="image-input-group"> <label>Book Image:</label> <div class="input-toggle"> - <input type="radio" id="urlOption" name="imageOption" value="url" checked> + <input type="radio" id="urlOption" name="imageOption" value="url" + <%= !isEditing || (isEditing && !book.image_url.startsWith('/uploads/')) ? 'checked' : '' %>> <label for="urlOption">Image URL</label> - <input type="radio" id="fileOption" name="imageOption" value="file"> + <input type="radio" id="fileOption" name="imageOption" value="file" + <%= isEditing && book.image_url.startsWith('/uploads/') ? 'checked' : '' %>> <label for="fileOption">Upload File</label> </div> - <div id="urlInput"> - <input type="text" id="image_url" name="image_url" value="<%= isEditing ? book.image_url : '' %>" placeholder="Enter image URL"> + <div id="urlInput" style="display: <%= !isEditing || (isEditing && !book.image_url.startsWith('/uploads/')) ? 'block' : 'none' %>"> + <input type="text" id="image_url" name="image_url" + value="<%= isEditing ? book.image_url : '' %>" + placeholder="Enter image URL"> </div> - <div id="fileInput" style="display: none;"> + <div id="fileInput" style="display: <%= isEditing && book.image_url.startsWith('/uploads/') ? 'block' : 'none' %>"> <input type="file" id="image_file" name="image_file" accept="image/*"> </div> - <img id="imagePreview" class="preview-image" src="<%= isEditing ? book.image_url : '' %>" alt="Book cover preview"> + <img id="imagePreview" class="preview-image" + src="<%= isEditing ? book.image_url : '' %>" + style="display: <%= isEditing && book.image_url ? 'block' : 'none' %>" + alt="Book cover preview"> </div> - <input type="hidden" name="list_id" value="<%= list_id %>"> <div class="form-actions"> <button type="submit"><%= isEditing ? 'Update Book' : 'Add Book' %></button> <a href="/books/<%= list_id %>">Cancel</a>