@@ -2,19 +2,18 @@ const Author = require("../models/author");
22const Book = require ( "../models/book" ) ;
33
44const { body, validationResult } = require ( "express-validator" ) ;
5- const asyncHandler = require ( "express-async-handler" ) ;
65
76// Display list of all Authors.
8- exports . author_list = asyncHandler ( async ( req , res , next ) => {
7+ exports . author_list = async ( req , res , next ) => {
98 const allAuthors = await Author . find ( ) . sort ( { family_name : 1 } ) . exec ( ) ;
109 res . render ( "author_list" , {
1110 title : "Author List" ,
1211 author_list : allAuthors ,
1312 } ) ;
14- } ) ;
13+ } ;
1514
1615// Display detail page for a specific Author.
17- exports . author_detail = asyncHandler ( async ( req , res , next ) => {
16+ exports . author_detail = async ( req , res , next ) => {
1817 // Get details of author and all their books (in parallel)
1918 const [ author , allBooksByAuthor ] = await Promise . all ( [
2019 Author . findById ( req . params . id ) . exec ( ) ,
@@ -33,7 +32,7 @@ exports.author_detail = asyncHandler(async (req, res, next) => {
3332 author,
3433 author_books : allBooksByAuthor ,
3534 } ) ;
36- } ) ;
35+ } ;
3736
3837// Display Author create form on GET.
3938exports . author_create_get = ( req , res , next ) => {
@@ -67,7 +66,7 @@ exports.author_create_post = [
6766 . toDate ( ) ,
6867
6968 // Process request after validation and sanitization.
70- asyncHandler ( async ( req , res , next ) => {
69+ async ( req , res , next ) => {
7170 // Extract the validation errors from a request.
7271 const errors = validationResult ( req ) ;
7372
@@ -90,14 +89,14 @@ exports.author_create_post = [
9089 }
9190
9291 // Data from form is valid.
92+ // Save and redirect to new author record.
9393 await author . save ( ) ;
94- // Redirect to new author record.
9594 res . redirect ( author . url ) ;
96- } ) ,
95+ } ,
9796] ;
9897
9998// Display Author delete form on GET.
100- exports . author_delete_get = asyncHandler ( async ( req , res , next ) => {
99+ exports . author_delete_get = async ( req , res , next ) => {
101100 // Get details of author and all their books (in parallel)
102101 const [ author , allBooksByAuthor ] = await Promise . all ( [
103102 Author . findById ( req . params . id ) . exec ( ) ,
@@ -107,17 +106,18 @@ exports.author_delete_get = asyncHandler(async (req, res, next) => {
107106 if ( author === null ) {
108107 // No results.
109108 res . redirect ( "/catalog/authors" ) ;
109+ return ;
110110 }
111111
112112 res . render ( "author_delete" , {
113113 title : "Delete Author" ,
114114 author,
115115 author_books : allBooksByAuthor ,
116116 } ) ;
117- } ) ;
117+ } ;
118118
119119// Handle Author delete on POST.
120- exports . author_delete_post = asyncHandler ( async ( req , res , next ) => {
120+ exports . author_delete_post = async ( req , res , next ) => {
121121 // Get details of author and all their books (in parallel)
122122 const [ author , allBooksByAuthor ] = await Promise . all ( [
123123 Author . findById ( req . params . id ) . exec ( ) ,
@@ -137,10 +137,10 @@ exports.author_delete_post = asyncHandler(async (req, res, next) => {
137137 // Author has no books. Delete object and redirect to the list of authors.
138138 await Author . findByIdAndDelete ( req . body . authorid ) ;
139139 res . redirect ( "/catalog/authors" ) ;
140- } ) ;
140+ } ;
141141
142142// Display Author update form on GET.
143- exports . author_update_get = asyncHandler ( async ( req , res , next ) => {
143+ exports . author_update_get = async ( req , res , next ) => {
144144 const author = await Author . findById ( req . params . id ) . exec ( ) ;
145145 if ( author === null ) {
146146 // No results.
@@ -150,7 +150,7 @@ exports.author_update_get = asyncHandler(async (req, res, next) => {
150150 }
151151
152152 res . render ( "author_form" , { title : "Update Author" , author } ) ;
153- } ) ;
153+ } ;
154154
155155// Handle Author update on POST.
156156exports . author_update_post = [
@@ -179,7 +179,7 @@ exports.author_update_post = [
179179 . toDate ( ) ,
180180
181181 // Process request after validation and sanitization.
182- asyncHandler ( async ( req , res , next ) => {
182+ async ( req , res , next ) => {
183183 // Extract the validation errors from a request.
184184 const errors = validationResult ( req ) ;
185185
@@ -205,5 +205,5 @@ exports.author_update_post = [
205205 // Data from form is valid. Update the record.
206206 await Author . findByIdAndUpdate ( req . params . id , author ) ;
207207 res . redirect ( author . url ) ;
208- } ) ,
208+ } ,
209209] ;
0 commit comments