Skip to content

Commit

Permalink
Book update - improve the genre selection (#254)
Browse files Browse the repository at this point in the history
  • Loading branch information
hamishwillee authored Dec 17, 2023
1 parent fef77d9 commit 2d5a189
Showing 1 changed file with 4 additions and 8 deletions.
12 changes: 4 additions & 8 deletions controllers/bookController.js
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ exports.book_delete_post = asyncHandler(async (req, res, next) => {
exports.book_update_get = asyncHandler(async (req, res, next) => {
// Get book, authors and genres for form.
const [book, allAuthors, allGenres] = await Promise.all([
Book.findById(req.params.id).populate("author").populate("genre").exec(),
Book.findById(req.params.id).populate("author").exec(),
Author.find().sort({ family_name: 1 }).exec(),
Genre.find().sort({ name: 1 }).exec(),
]);
Expand All @@ -215,13 +215,9 @@ exports.book_update_get = asyncHandler(async (req, res, next) => {
}

// Mark our selected genres as checked.
for (const genre of allGenres) {
for (const book_g of book.genre) {
if (genre._id.toString() === book_g._id.toString()) {
genre.checked = "true";
}
}
}
allGenres.forEach((genre) => {
if (book.genre.includes(genre._id)) genre.checked = "true";
});

res.render("book_form", {
title: "Update Book",
Expand Down

0 comments on commit 2d5a189

Please sign in to comment.