@@ -2,19 +2,18 @@ const Author = require("../models/author");
2
2
const Book = require ( "../models/book" ) ;
3
3
4
4
const { body, validationResult } = require ( "express-validator" ) ;
5
- const asyncHandler = require ( "express-async-handler" ) ;
6
5
7
6
// Display list of all Authors.
8
- exports . author_list = asyncHandler ( async ( req , res , next ) => {
7
+ exports . author_list = async ( req , res , next ) => {
9
8
const allAuthors = await Author . find ( ) . sort ( { family_name : 1 } ) . exec ( ) ;
10
9
res . render ( "author_list" , {
11
10
title : "Author List" ,
12
11
author_list : allAuthors ,
13
12
} ) ;
14
- } ) ;
13
+ } ;
15
14
16
15
// Display detail page for a specific Author.
17
- exports . author_detail = asyncHandler ( async ( req , res , next ) => {
16
+ exports . author_detail = async ( req , res , next ) => {
18
17
// Get details of author and all their books (in parallel)
19
18
const [ author , allBooksByAuthor ] = await Promise . all ( [
20
19
Author . findById ( req . params . id ) . exec ( ) ,
@@ -33,7 +32,7 @@ exports.author_detail = asyncHandler(async (req, res, next) => {
33
32
author,
34
33
author_books : allBooksByAuthor ,
35
34
} ) ;
36
- } ) ;
35
+ } ;
37
36
38
37
// Display Author create form on GET.
39
38
exports . author_create_get = ( req , res , next ) => {
@@ -67,7 +66,7 @@ exports.author_create_post = [
67
66
. toDate ( ) ,
68
67
69
68
// Process request after validation and sanitization.
70
- asyncHandler ( async ( req , res , next ) => {
69
+ async ( req , res , next ) => {
71
70
// Extract the validation errors from a request.
72
71
const errors = validationResult ( req ) ;
73
72
@@ -90,14 +89,14 @@ exports.author_create_post = [
90
89
}
91
90
92
91
// Data from form is valid.
92
+ // Save and redirect to new author record.
93
93
await author . save ( ) ;
94
- // Redirect to new author record.
95
94
res . redirect ( author . url ) ;
96
- } ) ,
95
+ } ,
97
96
] ;
98
97
99
98
// 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 ) => {
101
100
// Get details of author and all their books (in parallel)
102
101
const [ author , allBooksByAuthor ] = await Promise . all ( [
103
102
Author . findById ( req . params . id ) . exec ( ) ,
@@ -107,17 +106,18 @@ exports.author_delete_get = asyncHandler(async (req, res, next) => {
107
106
if ( author === null ) {
108
107
// No results.
109
108
res . redirect ( "/catalog/authors" ) ;
109
+ return ;
110
110
}
111
111
112
112
res . render ( "author_delete" , {
113
113
title : "Delete Author" ,
114
114
author,
115
115
author_books : allBooksByAuthor ,
116
116
} ) ;
117
- } ) ;
117
+ } ;
118
118
119
119
// Handle Author delete on POST.
120
- exports . author_delete_post = asyncHandler ( async ( req , res , next ) => {
120
+ exports . author_delete_post = async ( req , res , next ) => {
121
121
// Get details of author and all their books (in parallel)
122
122
const [ author , allBooksByAuthor ] = await Promise . all ( [
123
123
Author . findById ( req . params . id ) . exec ( ) ,
@@ -137,10 +137,10 @@ exports.author_delete_post = asyncHandler(async (req, res, next) => {
137
137
// Author has no books. Delete object and redirect to the list of authors.
138
138
await Author . findByIdAndDelete ( req . body . authorid ) ;
139
139
res . redirect ( "/catalog/authors" ) ;
140
- } ) ;
140
+ } ;
141
141
142
142
// 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 ) => {
144
144
const author = await Author . findById ( req . params . id ) . exec ( ) ;
145
145
if ( author === null ) {
146
146
// No results.
@@ -150,7 +150,7 @@ exports.author_update_get = asyncHandler(async (req, res, next) => {
150
150
}
151
151
152
152
res . render ( "author_form" , { title : "Update Author" , author } ) ;
153
- } ) ;
153
+ } ;
154
154
155
155
// Handle Author update on POST.
156
156
exports . author_update_post = [
@@ -179,7 +179,7 @@ exports.author_update_post = [
179
179
. toDate ( ) ,
180
180
181
181
// Process request after validation and sanitization.
182
- asyncHandler ( async ( req , res , next ) => {
182
+ async ( req , res , next ) => {
183
183
// Extract the validation errors from a request.
184
184
const errors = validationResult ( req ) ;
185
185
@@ -205,5 +205,5 @@ exports.author_update_post = [
205
205
// Data from form is valid. Update the record.
206
206
await Author . findByIdAndUpdate ( req . params . id , author ) ;
207
207
res . redirect ( author . url ) ;
208
- } ) ,
208
+ } ,
209
209
] ;
0 commit comments