Gitlab@Informatics

Skip to content
Snippets Groups Projects
Commit 4e3f8e24 authored by 65160118's avatar 65160118
Browse files

4 commit

parent 6a91e8c2
Branches
No related tags found
No related merge requests found
...@@ -146,7 +146,6 @@ exports.getEditBook = async (req, res) => { ...@@ -146,7 +146,6 @@ exports.getEditBook = async (req, res) => {
const bookModel = new BookModel(req.db); const bookModel = new BookModel(req.db);
try { try {
// Check if the book belongs to the current user
const book = await bookModel.getBookByIdAndUser(book_id, req.session.user.user_id); const book = await bookModel.getBookByIdAndUser(book_id, req.session.user.user_id);
if (!book) { if (!book) {
...@@ -154,8 +153,8 @@ exports.getEditBook = async (req, res) => { ...@@ -154,8 +153,8 @@ exports.getEditBook = async (req, res) => {
} }
res.render('addbook', { res.render('addbook', {
book,
isEditing: true, isEditing: true,
book: book,
list_id: book.list_id list_id: book.list_id
}); });
} catch (error) { } catch (error) {
...@@ -169,19 +168,32 @@ exports.postEditBook = async (req, res) => { ...@@ -169,19 +168,32 @@ exports.postEditBook = async (req, res) => {
return res.redirect('/login'); return res.redirect('/login');
} }
const { book_id } = req.params; upload(req, res, async (err) => {
const bookModel = new BookModel(req.db); if (err) {
return res.status(400).send('Error uploading file: ' + err.message);
}
try { try {
// Check if the book belongs to the current user 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); const existingBook = await bookModel.getBookByIdAndUser(book_id, req.session.user.user_id);
if (!existingBook) { if (!existingBook) {
return res.status(403).send('You do not have permission to edit this book'); return res.status(403).send('You do not have permission to edit this book');
} }
// Process update if user owns the book // Determine image URL
const { bookname, author, category, type, image_url } = req.body; 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, { await bookModel.updateBook(book_id, req.session.user.user_id, {
bookname, bookname,
author, author,
...@@ -195,6 +207,7 @@ exports.postEditBook = async (req, res) => { ...@@ -195,6 +207,7 @@ exports.postEditBook = async (req, res) => {
console.error('Error updating book:', error); console.error('Error updating book:', error);
res.status(500).send('Error updating book information'); res.status(500).send('Error updating book information');
} }
});
}; };
exports.deleteBook = async (req, res) => { exports.deleteBook = async (req, res) => {
......
...@@ -94,7 +94,7 @@ module.exports = class BookModel { ...@@ -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) => { return new Promise((resolve, reject) => {
const sql = ` const sql = `
UPDATE books UPDATE books
...@@ -109,7 +109,11 @@ module.exports = class BookModel { ...@@ -109,7 +109,11 @@ module.exports = class BookModel {
sql, sql,
[bookname, author, category, type, image_url, book_id, user_id], [bookname, author, category, type, image_url, book_id, user_id],
(err, results) => { (err, results) => {
if (err) reject(err); if (err) {
console.error('Database error:', err);
reject(err);
return;
}
resolve(results); resolve(results);
} }
); );
...@@ -249,18 +253,5 @@ module.exports = class BookModel { ...@@ -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);
}
);
});
}
}; };
public/uploads/1742550823551-Evelyn.jpg

192 KiB

...@@ -40,27 +40,36 @@ ...@@ -40,27 +40,36 @@
<input type="text" id="type" name="type" value="<%= isEditing ? book.type : '' %>" required> <input type="text" id="type" name="type" value="<%= isEditing ? book.type : '' %>" required>
</div> </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"> <div class="image-input-group">
<label>Book Image:</label> <label>Book Image:</label>
<div class="input-toggle"> <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> <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> <label for="fileOption">Upload File</label>
</div> </div>
<div id="urlInput"> <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"> <input type="text" id="image_url" name="image_url"
value="<%= isEditing ? book.image_url : '' %>"
placeholder="Enter image URL">
</div> </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/*"> <input type="file" id="image_file" name="image_file" accept="image/*">
</div> </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> </div>
<input type="hidden" name="list_id" value="<%= list_id %>">
<div class="form-actions"> <div class="form-actions">
<button type="submit"><%= isEditing ? 'Update Book' : 'Add Book' %></button> <button type="submit"><%= isEditing ? 'Update Book' : 'Add Book' %></button>
<a href="/books/<%= list_id %>">Cancel</a> <a href="/books/<%= list_id %>">Cancel</a>
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment