Skip to content

Commit

Permalink
book delete
Browse files Browse the repository at this point in the history
  • Loading branch information
felixtanhm committed Apr 17, 2024
1 parent 6359c02 commit bafd277
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,20 @@ exports.book_create_post = [
// Display book delete form on GET.
exports.book_delete_get = async function (req, res, next) {
try {
res.send("NOT IMPLEMENTED: Book delete GET");
const [book, allBookCopies] = await Promise.all([
Book.findById(req.params.id).populate("author").exec(),
BookInstance.find({ book: req.params.id }).exec(),
]);

if (book === null) {
res.redirect("/catalog/books");
}

res.render("book_delete", {
title: "Delete Book",
book,
book_copies: allBookCopies,
});
} catch (error) {
return next(error);
}
Expand All @@ -182,7 +195,22 @@ exports.book_delete_get = async function (req, res, next) {
// Handle book delete on POST.
exports.book_delete_post = async function (req, res, next) {
try {
res.send("NOT IMPLEMENTED: Book delete POST");
const [book, allBookCopies] = await Promise.all([
Book.findById(req.params.id).exec(),
BookInstance.find({ book: req.params.id }).exec(),
]);

if (allBookCopies.length > 0) {
res.render("book_delete", {
title: "Delete Book",
book,
book_copies: allBookCopies,
});
return;
} else {
await Book.findByIdAndDelete(req.body.bookid);
res.redirect("/catalog/books");
}
} catch (error) {
return next(error);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<!DOCTYPE html>
<html>
<%- include('./partials/header') %>
<body>
<%- include('./partials/nav.ejs') %>
<main>
<div class="content">
<h1>Book: <%= book.title %></h1>
<p><%= book.author.name %></p>
<p><%= book.summary %></p>
<br />
<% if (book_copies.length) { %>
<p>
Delete the following book copies before attempting to delete this
book.
</p>
<h2>Book Copies</h2>
<% book_copies.forEach(copy => { %>
<a class="text-success" href="<%= copy.url %>"><%= copy.id %></a>
<p>Imprint: <%= copy.imprint %></p>
<% }) %> <% } else { %>
<p>Do you really want to delete this Book?</p>
<form action="" method="post">
<input
type="hidden"
name="bookid"
id="bookid"
value="<%= book._id %>"
/>
<button type="submit">Delete</button>
</form>
<% } %>
</div>
<%- include('./partials/footer') %>
</main>
</body>
</html>

0 comments on commit bafd277

Please sign in to comment.