diff --git a/[Commands For Docker Desktop DB].txt b/[Commands For Docker Desktop DB].txt new file mode 100644 index 0000000000000000000000000000000000000000..57d6c1eb13173c7a5fdc1ac4ba29d66276c5ed5a --- /dev/null +++ b/[Commands For Docker Desktop DB].txt @@ -0,0 +1,10 @@ +[Commands For Docker Desktop DB] + +docker exec -it mongo-dev bash + +mongosh -u admin -p secret --authenticationDatabase admin + +show dbs +show collections + +db.books.find() \ No newline at end of file diff --git a/backend/server.js b/backend/server.js index f456a2bd1741d782d35dff56a2b3d95e34b59d7b..13782e2f407c5c68b9bcee979e3cd8b948a84faf 100644 --- a/backend/server.js +++ b/backend/server.js @@ -61,10 +61,20 @@ app.post('/submit', async (req, res) => { app.get('/books', async (req, res) => { try { const books = await Book.find(); // Fetch all books - console.log('Fetched books:', books); // Log the fetched books res.status(200).json(books); // Send the books as a JSON response } catch (error) { res.status(500).json({ message: 'Error fetching books', error: error.message }); } }); +app.delete('/books/:id', async (req, res) => { + const { id } = req.params; + try { + await Book.findByIdAndDelete(id); + res.status(200).send({ message: 'Book deleted' }); + } catch (err) { + res.status(500).send({ error: 'Deletion failed' }); + } + }); + + diff --git a/frontend/all-books.html b/frontend/all-books.html index 6834b5e0eec2784a1f12de849364cfc5e74eff24..e2358f9cd47880fb9f65609c8d271ba2a55a8044 100644 --- a/frontend/all-books.html +++ b/frontend/all-books.html @@ -1,8 +1,9 @@ <!DOCTYPE html> <html lang="en"> + <head> <meta charset="UTF-8" /> - <meta name="viewport" content="width=device-width, initial-scale=1.0"/> + <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <title>All Books</title> <link rel="stylesheet" href="style.css" /> <style> @@ -54,6 +55,7 @@ } </style> </head> + <body> <h1 style="text-align: center; color: #ff6600;">All Book Entries</h1> <div id="books-container"></div> @@ -64,40 +66,62 @@ <script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script> <script> + + /// Fetch and display all books document.addEventListener('DOMContentLoaded', async () => { try { - - // Fetch all books from the server const response = await axios.get('http://localhost:3000/books'); const books = response.data; - const booksContainer = document.getElementById('books-container'); - - console.log(books); + const booksContainer = document.getElementById('books-container'); // Get the container for books - // Clear the container before adding new books + if (books.length === 0) { booksContainer.innerHTML = "<p style='text-align:center; color:red;'>No books found.</p>"; return; } - // Create book cards for each book books.forEach(book => { const bookElement = document.createElement('div'); bookElement.className = 'book-card'; - // Add book details to the card bookElement.innerHTML = ` - <h3>${book.title}</h3> - <p><strong>Description:</strong> ${book.description}</p> - <p><strong>Rating:</strong> ${book.rating} ⭐</p> + <div class="book-content"> + <div> + <h3>${book.title}</h3> + <p><strong>Description:</strong> ${book.description}</p> + <p><strong>Rating:</strong> ${book.rating} ⭐</p> + </div> + <button class="delete-btn" data-id="${book._id}" title="Delete"> + 🗑️ + </button> + </div> `; + booksContainer.appendChild(bookElement); }); + + document.querySelectorAll('.delete-btn').forEach(button => { + button.addEventListener('click', async (e) => { + const bookId = e.target.getAttribute('data-id'); + if (confirm('Are you sure you want to delete this book?')) { + try { + await axios.delete(`http://localhost:3000/books/${bookId}`); + e.target.closest('.book-card').remove(); + } catch (err) { + console.error('Failed to delete book:', err); + alert('Error deleting book.'); + } + } + }); + }); + } catch (error) { console.error('Error fetching books:', error); document.getElementById('books-container').innerHTML = `<p style="color:red;">Error loading books.</p>`; } }); </script> + </body> -</html> + +</html> \ No newline at end of file