1
1
const register = ( app , pool ) => {
2
2
3
- app . get ( '/v1/categories/count' , ( req , res ) => {
3
+ app . get ( '/v1/categories/count' , ( req , res , next ) => {
4
4
pool . query (
5
5
'SELECT COUNT(*) AS counter FROM categories' ,
6
6
( err , rows , fields ) => {
7
7
if ( err ) {
8
- throw err
8
+ return next ( err )
9
9
}
10
10
if ( rows . length === 0 ) {
11
11
res . status ( 404 ) . end ( )
@@ -16,13 +16,13 @@ const register = (app, pool) => {
16
16
)
17
17
} )
18
18
19
- app . get ( '/v1/collections/:collectionId/categories/count' , ( req , res ) => {
19
+ app . get ( '/v1/collections/:collectionId/categories/count' , ( req , res , next ) => {
20
20
pool . query (
21
21
'SELECT COUNT(DISTINCT s.category_id) AS counter FROM collections_series cs JOIN series s ON s.id = cs.series_id WHERE cs.collection_id = :collectionId' ,
22
22
{ "collectionId" : req . params . collectionId } ,
23
23
( err , rows , fields ) => {
24
24
if ( err ) {
25
- throw err
25
+ return next ( err )
26
26
}
27
27
if ( rows . length === 0 ) {
28
28
res . status ( 404 ) . end ( )
@@ -33,39 +33,39 @@ const register = (app, pool) => {
33
33
)
34
34
} )
35
35
36
- app . get ( '/v1/categories' , ( req , res ) => {
36
+ app . get ( '/v1/categories' , ( req , res , next ) => {
37
37
pool . query (
38
38
'SELECT id , name , name_ru , slug FROM categories LIMIT :limit' ,
39
39
{ "limit" : req . query . limit } ,
40
40
( err , rows , fields ) => {
41
41
if ( err ) {
42
- throw err
42
+ return next ( err )
43
43
}
44
44
res . json ( rows )
45
45
}
46
46
)
47
47
} )
48
48
49
- app . post ( '/v1/categories' , ( req , res ) => {
49
+ app . post ( '/v1/categories' , ( req , res , next ) => {
50
50
pool . query (
51
51
'INSERT INTO categories ( name , name_ru , slug , created_at , created_by , updated_at , updated_by ) VALUES ( :name , :name_ru , :slug , NOW() , :user_id , NOW() , :user_id )' ,
52
52
{ "name" : req . body . name , "name_ru" : req . body . name_ru , "slug" : req . body . slug , "user_id" : req . body . user_id } ,
53
53
( err , rows , fields ) => {
54
54
if ( err ) {
55
- throw err
55
+ return next ( err )
56
56
}
57
57
res . sendStatus ( 204 )
58
58
}
59
59
)
60
60
} )
61
61
62
- app . get ( '/v1/categories/:categoryId' , ( req , res ) => {
62
+ app . get ( '/v1/categories/:categoryId' , ( req , res , next ) => {
63
63
pool . query (
64
64
'SELECT id , name , name_ru , slug FROM categories WHERE id = :categoryId' ,
65
65
{ "categoryId" : req . params . categoryId } ,
66
66
( err , rows , fields ) => {
67
67
if ( err ) {
68
- throw err
68
+ return next ( err )
69
69
}
70
70
if ( rows . length === 0 ) {
71
71
res . status ( 404 ) . end ( )
@@ -76,32 +76,36 @@ const register = (app, pool) => {
76
76
)
77
77
} )
78
78
79
- app . put ( '/v1/categories/:categoryId' , ( req , res ) => {
79
+ app . put ( '/v1/categories/:categoryId' , ( req , res , next ) => {
80
80
pool . query (
81
81
'UPDATE categories SET name = :name , name_ru = :name_ru , slug = :slug , updated_at = NOW() , updated_by = :user_id WHERE id = :categoryId' ,
82
82
{ "name" : req . body . name , "name_ru" : req . body . name_ru , "slug" : req . body . slug , "user_id" : req . body . user_id , "categoryId" : req . params . categoryId } ,
83
83
( err , rows , fields ) => {
84
84
if ( err ) {
85
- throw err
85
+ return next ( err )
86
86
}
87
87
res . sendStatus ( 204 )
88
88
}
89
89
)
90
90
} )
91
91
92
- app . delete ( '/v1/categories/:categoryId' , ( req , res ) => {
92
+ app . delete ( '/v1/categories/:categoryId' , ( req , res , next ) => {
93
93
pool . query (
94
94
'DELETE FROM categories WHERE id = :categoryId' ,
95
95
{ "categoryId" : req . params . categoryId } ,
96
96
( err , rows , fields ) => {
97
97
if ( err ) {
98
- throw err
98
+ return next ( err )
99
99
}
100
100
res . sendStatus ( 204 )
101
101
}
102
102
)
103
103
} )
104
104
105
+ app . use ( ( error , req , res , next ) => {
106
+ console . error ( error )
107
+ res . status ( 500 ) . json ( { "error" : "Internal Server Error" } )
108
+ } )
105
109
}
106
110
107
111
exports . register = register ;
0 commit comments