Skip to content

Commit

Permalink
update author
Browse files Browse the repository at this point in the history
  • Loading branch information
felixtanhm committed Apr 16, 2024
1 parent ce47cad commit 76e5510
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -170,17 +170,71 @@ exports.author_delete_post = async function (req, res, next) {
// Display Author update form on GET.
exports.author_update_get = async function (req, res, next) {
try {
res.send("NOT IMPLEMENTED: Author update GET");
const author = await Author.findById(req.params.id).exec();

res.render("author_form", {
title: "Update Author",
author: author,
errors: null,
});
} catch (error) {
return next(error);
}
};

// Handle Author update on POST.
exports.author_update_post = async function (req, res, next) {
try {
res.send("NOT IMPLEMENTED: Author update POST");
} catch (error) {
return next(error);
}
};
exports.author_update_post = [
body("first_name")
.trim()
.isLength({ min: 1 })
.escape()
.withMessage("First name must be specified.")
.isAlphanumeric()
.withMessage("First name has non-alphanumeric characters."),
body("family_name")
.trim()
.isLength({ min: 1 })
.escape()
.withMessage("Family name must be specified.")
.isAlphanumeric()
.withMessage("Family name has non-alphanumeric characters."),
body("date_of_birth", "Invalid date of birth")
.optional({ values: "falsy" })
.isISO8601()
.toDate(),
body("date_of_death", "Invalid date of death")
.optional({ values: "falsy" })
.isISO8601()
.toDate(),

async function (req, res, next) {
try {
const errors = validationResult(req);

const author = new Author({
first_name: req.body.first_name,
family_name: req.body.family_name,
date_of_birth: req.body.date_of_birth,
date_of_death: req.body.date_of_death,
_id: req.params.id,
});

if (!errors.isEmpty()) {
res.render("author_form", {
title: "Create Author",
author: author,
errors: errors.array(),
});
return;
} else {
const updatedAuthor = await Author.findByIdAndUpdate(
req.params.id,
author
);
res.redirect(updatedAuthor.url);
}
} catch (error) {
return next(error);
}
},
];
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ exports.bookinstance_update_get = async function (req, res, next) {
Book.find({}, "title").sort({ title: 1 }).exec(),
]);
res.render("bookinstance_form", {
title: "Create BookInstance",
title: "Update Copy",
book_list: allBooks,
selected_book: bookinstance.book._id,
errors: null,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
name="family_name"
id="family_name"
placeholder="ex. Smith"
value="<%= locals.author ? author.family_name : '' %>"
value="<%= author ? author.family_name : '' %>"
/>
</div>
<div class="input-group">
Expand All @@ -37,7 +37,7 @@
name="date_of_birth"
id="date_of_birth"
placeholder="dd/mm/yyyy"
value="<%= locals.author ? author.date_of_birth_yyyy_mm_dd : '' %>"
value="<%= author ? author.date_of_birth : '' %>"
/>
</div>
<div class="input-group">
Expand All @@ -47,7 +47,7 @@
name="date_of_death"
id="date_of_death"
placeholder="dd/mm/yyyy"
value="<%= locals.author ? author.date_of_death_yyyy_mm_dd : '' %>"
value="<%= author ? author.date_of_death : '' %>"
/>
</div>
<button type="submit">Submit</button>
Expand Down

0 comments on commit 76e5510

Please sign in to comment.