Skip to content

Commit

Permalink
dep & revised
Browse files Browse the repository at this point in the history
  • Loading branch information
solaris0051 committed Sep 19, 2024
1 parent 5aa1d75 commit eb8a5e0
Show file tree
Hide file tree
Showing 21 changed files with 148 additions and 131 deletions.
2 changes: 1 addition & 1 deletion controllers/authorController.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ exports.author_delete_post = asyncHandler(async (req, res, next) => {
});
return;
} else {
await Author.findByIdAndRemove(req.body.authorid);
await Author.findByIdAndDelete(req.body.authorid);
res.redirect("/catalog/authors");
}
});
Expand Down
55 changes: 29 additions & 26 deletions controllers/bookController.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ exports.index = asyncHandler(async (req, res, next) => {
BookInstance.countDocuments({}).exec(),
BookInstance.countDocuments({ status: "貸出可能" }).exec(),
Author.countDocuments({}).exec(),
Author.countDocuments({}).exec(),
Genre.countDocuments({}).exec(),
]);
res.render("index", {
title: "書籍管理ホーム",
Expand Down Expand Up @@ -58,8 +58,8 @@ exports.book_detail = asyncHandler(async (req, res, next) => {

exports.book_create_get = asyncHandler(async (req, res, next) => {
const [allAuthors, allGenres] = await Promise.all([
Author.find().exec(),
Genre.find().exec(),
Author.find().sort({ family_name: 1 }).exec(),
Genre.find().sort({ name: 1 }).exec(),
]);
res.render("book_form", {
title: "書籍登録フォーム",
Expand All @@ -70,9 +70,8 @@ exports.book_create_get = asyncHandler(async (req, res, next) => {

exports.book_create_post = [
(req, res, next) => {
if (!(req.body.genre instanceof Array)) {
if (typeof req.body.genre === "undefined") req.body.genre = [];
else req.body.genre = new Array(req.body.genre);
if (!Array.isArray(req.body.genre)) {
req.body.genre = typeof req.body.genre === "undefined" ? [] : [req.body.genre];
}
next();
},
Expand All @@ -88,8 +87,12 @@ exports.book_create_post = [
.trim()
.isLength({ min: 1 })
.escape(),
body("isbn", "ISBNを指定してください。").trim().isLength({ min: 1 }).escape(),
body("genre.*").escape(),
body("isbn", "ISBNを指定してください。")
.trim()
.isLength({ min: 1 })
.escape(),
body("genre.*")
.escape(),
asyncHandler(async (req, res, next) => {
const errors = validationResult(req);
const book = new Book({
Expand All @@ -101,8 +104,8 @@ exports.book_create_post = [
});
if (!errors.isEmpty()) {
const [allAuthors, allGenres] = await Promise.all([
Author.find().exec(),
Genre.find().exec(),
Author.find().sort({ family_name: 1 }).exec(),
Genre.find().sort({ name: 1 }).exec(),
]);
for (const genre of allGenres) {
if (book.genre.indexOf(genre._id) > -1) {
Expand Down Expand Up @@ -154,29 +157,25 @@ exports.book_delete_post = asyncHandler(async (req, res, next) => {
});
return;
} else {
await Book.findByIdAndRemove(req.body.id);
await Book.findByIdAndDelete(req.body.id);
res.redirect("/catalog/books");
}
});

exports.book_update_get = asyncHandler(async (req, res, next) => {
const [book, allAuthors, allGenres] = await Promise.all([
Book.findById(req.params.id).populate("author").populate("genre").exec(),
Author.find().exec(),
Genre.find().exec(),
Book.findById(req.params.id).populate("author").exec(),
Author.find().sort({ family_name: 1 }).exec(),
Genre.find().sort({ name: 1 }).exec(),
]);
if (book === null) {
const err = new Error("書籍がありません。");
err.status = 404;
return next(err);
}
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: "書籍更新",
authors: allAuthors,
Expand Down Expand Up @@ -208,8 +207,12 @@ exports.book_update_post = [
.trim()
.isLength({ min: 1 })
.escape(),
body("isbn", "ISBNを指定してください。").trim().isLength({ min: 1 }).escape(),
body("genre.*").escape(),
body("isbn", "ISBNを指定してください。")
.trim()
.isLength({ min: 1 })
.escape(),
body("genre.*")
.escape(),
asyncHandler(async (req, res, next) => {
const errors = validationResult(req);
const book = new Book({
Expand All @@ -222,11 +225,11 @@ exports.book_update_post = [
});
if (!errors.isEmpty()) {
const [allAuthors, allGenres] = await Promise.all([
Author.find().exec(),
Genre.find().exec(),
Author.find().sort({ family_name: 1 }).exec(),
Genre.find().sort({ name: 1 }).exec(),
]);
for (const genre of allGenres) {
if (book.genre.indexOf(genre._id) > -1) {
if (book.genre.includes(genre._id)) {
genre.checked = "true";
}
}
Expand Down
22 changes: 15 additions & 7 deletions controllers/bookinstanceController.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,20 +28,24 @@ exports.bookinstance_detail = asyncHandler(async (req, res, next) => {
});

exports.bookinstance_create_get = asyncHandler(async (req, res, next) => {
const allBooks = await Book.find({}, "title").exec();
const allBooks = await Book.find({}, "title").sort({ title: 1 }).exec();
res.render("bookinstance_form", {
title: "書籍現物登録フォーム",
book_list: allBooks,
});
});

exports.bookinstance_create_post = [
body("book", "書籍名を指定してください").trim().isLength({ min: 1 }).escape(),
body("book", "書籍名を指定してください")
.trim()
.isLength({ min: 1 })
.escape(),
body("imprint", "版を指定してください")
.trim()
.isLength({ min: 1 })
.escape(),
body("status").escape(),
body("status")
.escape(),
body("due_back", "日付が正しくありません。")
.optional({ values: "falsy" })
.isISO8601()
Expand All @@ -55,7 +59,7 @@ exports.bookinstance_create_post = [
due_back: req.body.due_back,
});
if (!errors.isEmpty()) {
const allBooks = await Book.find({}, "title").exec();
const allBooks = await Book.find({}, "title").sort({ title: 1 }).exec();
res.render("bookinstance_form", {
title: "書籍現物登録フォーム",
book_list: allBooks,
Expand Down Expand Up @@ -85,7 +89,7 @@ exports.bookinstance_delete_get = asyncHandler(async (req, res, next) => {
});

exports.bookinstance_delete_post = asyncHandler(async (req, res, next) => {
await BookInstance.findByIdAndRemove(req.body.id);
await BookInstance.findByIdAndDelete(req.body.id);
res.redirect("/catalog/bookinstances");
});

Expand All @@ -108,12 +112,16 @@ exports.bookinstance_update_get = asyncHandler(async (req, res, next) => {
});

exports.bookinstance_update_post = [
body("book", "書籍名を指定してください。").trim().isLength({ min: 1 }).escape(),
body("book", "書籍名を指定してください。")
.trim()
.isLength({ min: 1 })
.escape(),
body("imprint", "版を指定してください。")
.trim()
.isLength({ min: 1 })
.escape(),
body("status").escape(),
body("status")
.escape(),
body("due_back", "日付が正しくありません。")
.optional({ values: "falsy" })
.isISO8601()
Expand Down
2 changes: 1 addition & 1 deletion controllers/genreController.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ exports.genre_delete_post = asyncHandler(async (req, res, next) => {
});
return;
} else {
await Genre.findByIdAndRemove(req.body.id);
await Genre.findByIdAnDelete(req.body.id);
res.redirect("/catalog/genres");
}
});
Expand Down
16 changes: 8 additions & 8 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@
"helmet": "^7.1.0",
"http-errors": "~2.0.0",
"moment": "^2.30.1",
"mongoose": "^8.6.2",
"mongoose": "^8.6.3",
"morgan": "~1.10.0",
"pug": "3.0.3"
},
"devDependencies": {
"nodemon": "^3.1.4"
"nodemon": "^3.1.5"
},
"overrides": {
"semver": "7.5.3"
Expand Down
2 changes: 1 addition & 1 deletion views/author_delete.pug
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ block content
else
p この著者を削除してよろしいですか。

form(method='POST' action='')
form(method='POST')
div.form-group
input#authorid.form-control(type='hidden',name='authorid', required='true', value=author._id )

Expand Down
12 changes: 6 additions & 6 deletions views/author_detail.pug
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ block content
div(style='margin-left:20px;margin-top:20px')

h4 書籍マスタ

dl
each book in author_books
dt
a(href=book.url) #{book.title}
dd #{book.summary}
if author_books.length
dl
each book in author_books
dt
a(href=book.url) #{book.title}
dd #{book.summary}

else
p この著者の書籍マスタはありません。
Expand Down
6 changes: 3 additions & 3 deletions views/author_form.pug
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ extends layout
block content
h1=title

form(method='POST' action='')
form(method='POST')
div.form-group
label(for='family_name') 氏:
input#family_name.form-control(type='text', placeholder='全角で入力' name='family_name' required='true' value=(undefined===author ? '' : author.family_name))
input#family_name.form-control(type='text', placeholder='全角で入力' name='family_name' required value=(undefined===author ? '' : author.family_name))
label(for='first_name') 名:
input#first_name.form-control(type='text', placeholder='全角で入力' name='first_name' required='true' value=(undefined===author ? '' : author.first_name) )
input#first_name.form-control(type='text', placeholder='全角で入力' name='first_name' required value=(undefined===author ? '' : author.first_name) )
div.form-group
label(for='date_of_birth') 生年月日:
input#date_of_birth.form-control(type='date', name='date_of_birth' value=(undefined===author ? '' : author.date_of_birth_yyyy_mm_dd) )
Expand Down
2 changes: 1 addition & 1 deletion views/author_list.pug
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@ block content
| (#{author.lifespan})

else
li 著者名がありません。
p 著者名がありません。
10 changes: 6 additions & 4 deletions views/book_delete.pug
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@ block content
p #[strong 要約:] #{book.summary}
p #[strong ISBN:] #{book.isbn}
p #[strong ジャンル:]
each val in book.genre
each val, index in book.genre
a(href=val.url) #{val.name}
|,
if index < book.genre.length - 1
|,&nbsp;


hr

Expand Down Expand Up @@ -40,8 +42,8 @@ block content
else
p この書籍を削除してよろしいですか。

form(method='POST' action='')
form(method='POST')
div.form-group
input#id.form-control(type='hidden',name='id', required='true', value=book._id )
input#id.form-control(type='hidden',name='id', value=book._id )

button.btn.btn-primary(type='submit') 削除
2 changes: 1 addition & 1 deletion views/book_detail.pug
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ block content
each val, index in book.genre
a(href=val.url) #{val.name}
if index < book.genre.length - 1
|,
|,&nbsp;

div(style='margin-left:20px;margin-top:20px')
h4 書籍現物
Expand Down
24 changes: 15 additions & 9 deletions views/book_form.pug
Original file line number Diff line number Diff line change
Expand Up @@ -3,32 +3,38 @@ extends layout
block content
h1= title

form(method='POST' action='')
form(method='POST')
div.form-group
label(for='title') 書籍名:
input#title.form-control(type='text', placeholder='書籍名' name='title' required='true' value=(undefined===book ? '' : book.title) )
input#title.form-control(type='text', placeholder='書籍名' name='title' required value=(undefined===book ? '' : book.title) )
div.form-group
label(for='author') 著者:
select#author.form-control(type='select', placeholder='著者を選択' name='author' required='true' )
- authors.sort(function(a, b) {let textA = a.family_name.toUpperCase(); let textB = b.family_name.toUpperCase(); return (textA < textB) ? -1 : (textA > textB) ? 1 : 0;});
select#author.form-control(name='author' required)
option(value='') --著者を選んでください。--
for author in authors
if book
option(value=author._id selected=(author._id.toString()===book.author._id.toString() ? 'selected' : false) ) #{author.name}
if author._id.toString()===book.author._id.toString()
option(value=author._id selected) #{author.name}
else
option(value=author._id) #{author.name}
else
option(value=author._id) #{author.name}
div.form-group
label(for='summary') 要約:
input#summary.form-control(type='textarea', placeholder='要約' name='summary' value=(undefined===book ? '' : book.summary) required='true')
textarea#summary.form-control(placeholder='要約' name='summary' required)= undefined===book ? '' : book.summary
div.form-group
label(for='isbn') ISBN:
input#isbn.form-control(type='text', placeholder='ISBN13' name='isbn' value=(undefined===book ? '' : book.isbn) required='true')
input#isbn.form-control(type='text', placeholder='ISBN13' name='isbn' value=(undefined===book ? '' : book.isbn) required)
div.form-group
label ジャンル:
div
for genre in genres
div(style='display: inline; padding-right:10px;')
input.checkbox-input(type='checkbox', name='genre', id=genre._id, value=genre._id, checked=genre.checked )
label(for=genre._id) #{genre.name}
if genre.checked
input.checkbox-input(type='checkbox', name='genre', id=genre._id, value=genre._id, checked)
else
input.checkbox-input(type='checkbox', name='genre', id=genre._id, value=genre._id)
label(for=genre._id) &nbsp;#{genre.name}
button.btn.btn-primary(type='submit') 送信

if errors
Expand Down
Loading

0 comments on commit eb8a5e0

Please sign in to comment.