Skip to content

Commit

Permalink
add: create author
Browse files Browse the repository at this point in the history
  • Loading branch information
img02 committed Jul 20, 2023
1 parent 0fa81e0 commit acde261
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
/* eslint-disable import/no-extraneous-dependencies */
/* eslint-disable no-unused-vars */
// eslint-disable-next-line import/no-extraneous-dependencies
const asyncHandler = require("express-async-handler");
const { body, validationResult } = require("express-validator");

const Author = require("../models/author");
const Book = require("../models/book");

Expand Down Expand Up @@ -36,14 +38,66 @@ exports.author_detail = asyncHandler(async (req, res, next) => {
});

// Display Author create form on GET.
exports.author_create_get = asyncHandler(async (req, res, next) => {
res.send("NOT IMPLEMENTED: Author create GET");
});
exports.author_create_get = (req, res, next) => {
res.render("author_form", { title: "Create Author" });
};

// Handle Author create on POST.
exports.author_create_post = asyncHandler(async (req, res, next) => {
res.send("NOT IMPLEMENTED: Author create POST");
});
exports.author_create_post = [
// Validate and sanitize fields.
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(),

// Process request after validation and sanitization.
asyncHandler(async (req, res, next) => {
// Extract the validation errors from a request.
const errors = validationResult(req);

// Create Author object with escaped and trimmed data
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
});

if (!errors.isEmpty()) {
// There are errors. Render form again with sanitized values/errors messages.
res.render("author_form", {
title: "Create Author",
author,
errors: errors.array()
});
return;
}
// Data from form is valid.

// Save author.
await author.save();
// Redirect to new author record.
res.redirect(author.url);
})
];

// Display Author delete form on GET.
exports.author_delete_get = asyncHandler(async (req, res, next) => {
Expand Down
21 changes: 21 additions & 0 deletions node/express/express-locallibrary-tutorial/views/author_form.pug
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
extends layout

block content
h1=title

form(method='POST' action='')
div.form-group
label(for='first_name') First Name:
input#first_name.form-control(type='text' placeholder='First name' name='first_name' required='true' value=(undefined===author ? '' : author.first_name) )
label(for='family_name') Family Name:
input#family_name.form-control(type='text' placeholder='Family name' name='family_name' required='true' value=(undefined===author ? '' : author.family_name))
div.form-group
label(for='date_of_birth') Date of birth:
input#date_of_birth.form-control(type='date' name='date_of_birth' value=(undefined===author ? '' : author.date_of_birth) )
label(for='date_of_death') Date of death:
input#date_of_birth.form-control(type='date' name='date_of_death' value=(undefined===author ? '' : author.date_of_death) )
button.btn.btn-primary(type='submit') Submit
if errors
ul
for error in errors
li!= error.msg

0 comments on commit acde261

Please sign in to comment.