Skip to content

Commit

Permalink
Express: Use sort on Db query rather than in template (#246)
Browse files Browse the repository at this point in the history
  • Loading branch information
hamishwillee authored Nov 30, 2023
1 parent 8dfadaa commit a2fee7a
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 12 deletions.
16 changes: 8 additions & 8 deletions controllers/bookController.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,8 @@ exports.book_detail = asyncHandler(async (req, res, next) => {
exports.book_create_get = asyncHandler(async (req, res, next) => {
// Get all authors and genres, which we can use for adding to our book.
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", {
Expand Down Expand Up @@ -125,8 +125,8 @@ exports.book_create_post = [

// Get all authors and genres for form.
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(),
]);

// Mark our selected genres as checked.
Expand Down Expand Up @@ -203,8 +203,8 @@ 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(),
Author.find().exec(),
Genre.find().exec(),
Author.find().sort({ family_name: 1 }).exec(),
Genre.find().sort({ name: 1 }).exec(),
]);

if (book === null) {
Expand Down Expand Up @@ -278,8 +278,8 @@ exports.book_update_post = [

// Get all authors and genres for form
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(),
]);

// Mark our selected genres as checked.
Expand Down
4 changes: 2 additions & 2 deletions controllers/bookinstanceController.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ exports.bookinstance_detail = asyncHandler(async (req, res, next) => {

// Display BookInstance create form on GET.
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: "Create BookInstance",
Expand Down Expand Up @@ -73,7 +73,7 @@ exports.bookinstance_create_post = [
if (!errors.isEmpty()) {
// There are errors.
// Render form again with sanitized values and error messages.
const allBooks = await Book.find({}, "title").exec();
const allBooks = await Book.find({}, "title").sort({ title: 1 }).exec();

res.render("bookinstance_form", {
title: "Create BookInstance",
Expand Down
1 change: 0 additions & 1 deletion views/book_form.pug
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ block content
div.form-group
label(for='author') Author:
select#author.form-control(type='select', placeholder='Select author' 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;});
for author in authors
if book
option(value=author._id selected=(author._id.toString()===book.author._id.toString() ? 'selected' : false) ) #{author.name}
Expand Down
1 change: 0 additions & 1 deletion views/bookinstance_form.pug
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ block content
div.form-group
label(for='book') Book:
select#book.form-control(type='select' placeholder='Select book' name='book' required='true' )
- book_list.sort(function(a, b) {let textA = a.title.toUpperCase(); let textB = b.title.toUpperCase(); return (textA < textB) ? -1 : (textA > textB) ? 1 : 0;});
for book in book_list
option(value=book._id, selected=(selected_book==book._id.toString() ? '' : false) ) #{book.title}

Expand Down

0 comments on commit a2fee7a

Please sign in to comment.