1
- import { Express , Request , Response } from 'express'
1
+ import { Express , NextFunction , Request , Response } from 'express'
2
2
import { Pool } from 'mysql'
3
3
4
4
const register = ( app : Express , pool : Pool ) => {
5
5
6
- app . get ( '/v1/categories/count' , ( req : Request , res : Response ) => {
6
+ app . get ( '/v1/categories/count' , ( req : Request , res : Response , next : NextFunction ) => {
7
7
pool . query (
8
8
'SELECT COUNT(*) AS counter FROM categories' ,
9
9
( err , rows , fields ) => {
10
10
if ( err ) {
11
- throw err
11
+ return next ( err )
12
12
}
13
13
if ( rows . length === 0 ) {
14
14
res . status ( 404 ) . end ( )
@@ -19,13 +19,13 @@ const register = (app: Express, pool: Pool) => {
19
19
)
20
20
} )
21
21
22
- app . get ( '/v1/collections/:collectionId/categories/count' , ( req : Request , res : Response ) => {
22
+ app . get ( '/v1/collections/:collectionId/categories/count' , ( req : Request , res : Response , next : NextFunction ) => {
23
23
pool . query (
24
24
'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' ,
25
25
{ "collectionId" : req . params . collectionId } ,
26
26
( err , rows , fields ) => {
27
27
if ( err ) {
28
- throw err
28
+ return next ( err )
29
29
}
30
30
if ( rows . length === 0 ) {
31
31
res . status ( 404 ) . end ( )
@@ -36,39 +36,39 @@ const register = (app: Express, pool: Pool) => {
36
36
)
37
37
} )
38
38
39
- app . get ( '/v1/categories' , ( req : Request , res : Response ) => {
39
+ app . get ( '/v1/categories' , ( req : Request , res : Response , next : NextFunction ) => {
40
40
pool . query (
41
41
'SELECT id , name , name_ru , slug FROM categories LIMIT :limit' ,
42
42
{ "limit" : req . query . limit } ,
43
43
( err , rows , fields ) => {
44
44
if ( err ) {
45
- throw err
45
+ return next ( err )
46
46
}
47
47
res . json ( rows )
48
48
}
49
49
)
50
50
} )
51
51
52
- app . post ( '/v1/categories' , ( req : Request , res : Response ) => {
52
+ app . post ( '/v1/categories' , ( req : Request , res : Response , next : NextFunction ) => {
53
53
pool . query (
54
54
'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 )' ,
55
55
{ "name" : req . body . name , "name_ru" : req . body . name_ru , "slug" : req . body . slug , "user_id" : req . body . user_id } ,
56
56
( err , rows , fields ) => {
57
57
if ( err ) {
58
- throw err
58
+ return next ( err )
59
59
}
60
60
res . sendStatus ( 204 )
61
61
}
62
62
)
63
63
} )
64
64
65
- app . get ( '/v1/categories/:categoryId' , ( req : Request , res : Response ) => {
65
+ app . get ( '/v1/categories/:categoryId' , ( req : Request , res : Response , next : NextFunction ) => {
66
66
pool . query (
67
67
'SELECT id , name , name_ru , slug FROM categories WHERE id = :categoryId' ,
68
68
{ "categoryId" : req . params . categoryId } ,
69
69
( err , rows , fields ) => {
70
70
if ( err ) {
71
- throw err
71
+ return next ( err )
72
72
}
73
73
if ( rows . length === 0 ) {
74
74
res . status ( 404 ) . end ( )
@@ -79,32 +79,36 @@ const register = (app: Express, pool: Pool) => {
79
79
)
80
80
} )
81
81
82
- app . put ( '/v1/categories/:categoryId' , ( req : Request , res : Response ) => {
82
+ app . put ( '/v1/categories/:categoryId' , ( req : Request , res : Response , next : NextFunction ) => {
83
83
pool . query (
84
84
'UPDATE categories SET name = :name , name_ru = :name_ru , slug = :slug , updated_at = NOW() , updated_by = :user_id WHERE id = :categoryId' ,
85
85
{ "name" : req . body . name , "name_ru" : req . body . name_ru , "slug" : req . body . slug , "user_id" : req . body . user_id , "categoryId" : req . params . categoryId } ,
86
86
( err , rows , fields ) => {
87
87
if ( err ) {
88
- throw err
88
+ return next ( err )
89
89
}
90
90
res . sendStatus ( 204 )
91
91
}
92
92
)
93
93
} )
94
94
95
- app . delete ( '/v1/categories/:categoryId' , ( req : Request , res : Response ) => {
95
+ app . delete ( '/v1/categories/:categoryId' , ( req : Request , res : Response , next : NextFunction ) => {
96
96
pool . query (
97
97
'DELETE FROM categories WHERE id = :categoryId' ,
98
98
{ "categoryId" : req . params . categoryId } ,
99
99
( err , rows , fields ) => {
100
100
if ( err ) {
101
- throw err
101
+ return next ( err )
102
102
}
103
103
res . sendStatus ( 204 )
104
104
}
105
105
)
106
106
} )
107
107
108
+ app . use ( ( error : any , req : Request , res : Response , next : NextFunction ) => {
109
+ console . error ( error )
110
+ res . status ( 500 ) . json ( { "error" : "Internal Server Error" } )
111
+ } )
108
112
}
109
113
110
114
exports . register = register ;
0 commit comments