1
1
import { Request , Response , NextFunction } from 'express' ;
2
2
import { HTTP_STATUS } from '../../shared/constants/http/http-status' ;
3
- // import { ITEM_CONSTANTS } from '../constants /city.constant';
4
- // import { validateItem } from '../schemas /city.schema';
3
+ import { ITEM_CONSTANTS } from './city.constant' ;
4
+ import { validateItem } from './city.schema' ;
5
5
import Service from './city.service' ;
6
- // import { City } from '../models /city.model';
6
+ import { City } from './city.model' ;
7
7
8
8
export default class Controller {
9
9
private service : Service ;
@@ -12,10 +12,10 @@ export default class Controller {
12
12
this . service = service ;
13
13
14
14
this . getItems = this . getItems . bind ( this ) ;
15
- // this.getItemById = this.getItemById.bind(this);
16
- // this.createItem = this.createItem.bind(this);
17
- // this.updateItem = this.updateItem.bind(this);
18
- // this.deleteItem = this.deleteItem.bind(this);
15
+ this . getItemById = this . getItemById . bind ( this ) ;
16
+ this . createItem = this . createItem . bind ( this ) ;
17
+ this . updateItem = this . updateItem . bind ( this ) ;
18
+ this . deleteItem = this . deleteItem . bind ( this ) ;
19
19
}
20
20
21
21
async getItems ( req : Request , res : Response , next : NextFunction ) : Promise < void > {
@@ -29,75 +29,86 @@ export default class Controller {
29
29
}
30
30
}
31
31
32
- // async getItemById(req: Request, res: Response, next: NextFunction): Promise<void> {
33
- // try {
34
- // const id = parseInt(req.params.id, 10);
35
- // const result = await this.service.getItemById(id);
36
- // res.locals = { data: result, statusCode: HTTP_STATUS.OK };
37
- // return next();
38
- // } catch (error: any) {
39
- // if (error.message === ITEM_CONSTANTS.NOT_FOUND) {
40
- // return next({
41
- // statusCode: HTTP_STATUS.NOT_FOUND,
42
- // message: error.message,
43
- // context: `${req.method} ${req.originalUrl}`,
44
- // details: {
45
- // path: req.originalUrl,
46
- // errorCode: HTTP_STATUS.NOT_FOUND,
47
- // timestamp: new Date().toISOString(),
48
- // },
49
- // });
50
- // }
51
- // return next(error);
52
- // }
53
- // }
54
-
55
- // async createItem(req: Request, res: Response, next: NextFunction): Promise<void> {
56
- // try {
57
- // validateItem(req.body);
58
- // const result = await this.service.createItem(req.body as City);
59
- // res.locals = { data: result, statusCode: HTTP_STATUS.CREATED };
60
- // return next();
61
- // } catch (error: any) {
62
- // if (error.message === ITEM_CONSTANTS.ALREADY_EXISTS) {
63
- // return next({ statusCode: HTTP_STATUS.CONFLICT, message: error.message });
64
- // }
65
- // if (error.name === 'ValidationError') {
66
- // return next({ statusCode: HTTP_STATUS.BAD_REQUEST, message: error.message });
67
- // }
68
- // return next(error);
69
- // }
70
- // }
71
-
72
- // async updateItem(req: Request, res: Response, next: NextFunction): Promise<void> {
73
- // try {
74
- // validateItem(req.body);
75
- // const id = parseInt(req.params.id, 10);
76
- // const result = await this.service.updateItem(id, req.body as Partial<City>);
77
- // res.locals = { data: result, statusCode: HTTP_STATUS.OK };
78
- // return next();
79
- // } catch (error: any) {
80
- // if (error.message === ITEM_CONSTANTS.NOT_FOUND) {
81
- // return next({ statusCode: HTTP_STATUS.NOT_FOUND, message: error.message });
82
- // }
83
- // if (error.name === 'ValidationError') {
84
- // return next({ statusCode: HTTP_STATUS.BAD_REQUEST, message: error.message });
85
- // }
86
- // return next(error);
87
- // }
88
- // }
89
-
90
- // async deleteItem(req: Request, res: Response, next: NextFunction): Promise<void> {
91
- // try {
92
- // const id = parseInt(req.params.id, 10);
93
- // const result = await this.service.deleteItem(id);
94
- // res.locals = { data: result, statusCode: HTTP_STATUS.OK };
95
- // return next();
96
- // } catch (error: any) {
97
- // if (error.message === ITEM_CONSTANTS.NOT_FOUND) {
98
- // return next({ statusCode: HTTP_STATUS.NOT_FOUND, message: error.message });
99
- // }
100
- // return next(error);
101
- // }
102
- // }
32
+ async getItemById ( req : Request , res : Response , next : NextFunction ) : Promise < void > {
33
+ try {
34
+ const id = parseInt ( req . params . id , 10 ) ;
35
+ const result = await this . service . getItemById ( id ) ;
36
+ res . locals = { data : result , statusCode : HTTP_STATUS . OK } ;
37
+
38
+ return next ( ) ;
39
+ } catch ( error : any ) {
40
+ if ( error . message === ITEM_CONSTANTS . NOT_FOUND ) {
41
+
42
+ return next ( {
43
+ statusCode : HTTP_STATUS . NOT_FOUND ,
44
+ message : error . message ,
45
+ context : `${ req . method } ${ req . originalUrl } ` ,
46
+ details : {
47
+ path : req . originalUrl ,
48
+ errorCode : HTTP_STATUS . NOT_FOUND ,
49
+ timestamp : new Date ( ) . toISOString ( ) ,
50
+ } ,
51
+ } ) ;
52
+ }
53
+
54
+ return next ( error ) ;
55
+ }
56
+ }
57
+
58
+ async createItem ( req : Request , res : Response , next : NextFunction ) : Promise < void > {
59
+ try {
60
+ validateItem ( req . body ) ;
61
+ const result = await this . service . createItem ( req . body as City ) ;
62
+ res . locals = { data : result , statusCode : HTTP_STATUS . CREATED } ;
63
+
64
+ return next ( ) ;
65
+ } catch ( error : any ) {
66
+ if ( error . message === ITEM_CONSTANTS . ALREADY_EXISTS ) {
67
+
68
+ return next ( { statusCode : HTTP_STATUS . CONFLICT , message : error . message } ) ;
69
+ }
70
+ if ( error . name === 'ValidationError' ) {
71
+
72
+ return next ( { statusCode : HTTP_STATUS . BAD_REQUEST , message : error . message } ) ;
73
+ }
74
+
75
+ return next ( error ) ;
76
+ }
77
+ }
78
+
79
+ async updateItem ( req : Request , res : Response , next : NextFunction ) : Promise < void > {
80
+ try {
81
+ validateItem ( req . body ) ;
82
+ const id = parseInt ( req . params . id , 10 ) ;
83
+ const result = await this . service . updateItem ( id , req . body ) ;
84
+ res . locals = { data : result , statusCode : HTTP_STATUS . OK } ;
85
+
86
+ return next ( ) ;
87
+ } catch ( error : any ) {
88
+ if ( error . message === ITEM_CONSTANTS . NOT_FOUND ) {
89
+ return next ( { statusCode : HTTP_STATUS . NOT_FOUND , message : error . message } ) ;
90
+ }
91
+ if ( error . name === 'ValidationError' ) {
92
+ return next ( { statusCode : HTTP_STATUS . BAD_REQUEST , message : error . message } ) ;
93
+ }
94
+
95
+ return next ( error ) ;
96
+ }
97
+ }
98
+
99
+ async deleteItem ( req : Request , res : Response , next : NextFunction ) : Promise < void > {
100
+ try {
101
+ const id = parseInt ( req . params . id , 10 ) ;
102
+ const result = await this . service . deleteItem ( id ) ;
103
+ res . locals = { data : result , statusCode : HTTP_STATUS . OK } ;
104
+
105
+ return next ( ) ;
106
+ } catch ( error : any ) {
107
+ if ( error . message === ITEM_CONSTANTS . NOT_FOUND ) {
108
+ return next ( { statusCode : HTTP_STATUS . NOT_FOUND , message : error . message } ) ;
109
+ }
110
+
111
+ return next ( error ) ;
112
+ }
113
+ }
103
114
}
0 commit comments