@@ -8,90 +8,86 @@ interface MyRequest extends Request {
88 readonly [ ENTITY ] : any ;
99}
1010
11- const list = async ( _ : Request , res : Response , next : NextFunction ) => {
11+ const list = ( _ : Request , res : Response , next : NextFunction ) : void => {
1212 try {
1313 // TODO: list objects
1414 const objects : any [ ] = [ ] ;
1515
1616 res . send ( objects ) ;
1717 } catch ( err ) {
18- next ( new HttpError ( err . code || StatusCodes . INTERNAL_SERVER_ERROR , err ) ) ;
18+ next ( new HttpError ( err . code ?? StatusCodes . INTERNAL_SERVER_ERROR , err ) ) ;
1919 }
2020} ;
2121
22- const create = async ( req : Request , res : Response , next : NextFunction ) => {
22+ const create = ( req : Request , res : Response , next : NextFunction ) : void => {
2323 try {
2424 // TODO: create object
25- const object = req . body ;
25+ const object = { ... req . body } ;
2626
2727 res . send ( object ) ;
2828 } catch ( err ) {
29- next ( new HttpError ( err . code || StatusCodes . INTERNAL_SERVER_ERROR , err ) ) ;
29+ next ( new HttpError ( err . code ?? StatusCodes . INTERNAL_SERVER_ERROR , err ) ) ;
3030 }
3131} ;
3232
33- const getById = async ( req : Request , _ : Response , next : NextFunction ) => {
33+ const getById = ( req : Request , _ : Response , next : NextFunction ) : void => {
3434 try {
35- if ( ! req . params . id ) {
35+ const { id = '' } = req . params ;
36+ if ( id === '' ) {
3637 throw new HttpError ( StatusCodes . BAD_REQUEST , 'Invalid resource Id' ) ;
3738 }
3839
3940 // TODO: get object by id
4041 const object = { } ;
41- if ( ! object ) {
42- return next ( new HttpError ( StatusCodes . NOT_FOUND , 'Resource not found' ) ) ;
43- }
4442 Object . assign ( req , { [ ENTITY ] : object } ) ;
4543
4644 next ( ) ;
4745 } catch ( err ) {
48- next ( new HttpError ( err . code || StatusCodes . INTERNAL_SERVER_ERROR , err ) ) ;
46+ next ( new HttpError ( err . code ?? StatusCodes . INTERNAL_SERVER_ERROR , err ) ) ;
4947 }
5048} ;
5149
52- const get = ( req : Request , res : Response , next : NextFunction ) => {
50+ const get = ( req : Request , res : Response , next : NextFunction ) : void => {
5351 try {
5452 // TODO: get object
5553 res . send ( ( req as MyRequest ) [ ENTITY ] ) ;
5654 } catch ( err ) {
57- next ( new HttpError ( err . code || StatusCodes . INTERNAL_SERVER_ERROR , err ) ) ;
55+ next ( new HttpError ( err . code ?? StatusCodes . INTERNAL_SERVER_ERROR , err ) ) ;
5856 }
5957} ;
6058
61- const updatePartial = async (
59+ const updatePartial = (
6260 req : Request ,
6361 res : Response ,
6462 next : NextFunction ,
65- ) => {
63+ ) : void => {
6664 try {
6765 // TODO: update partial object
6866 const object = { ...( req as MyRequest ) [ ENTITY ] , ...req . body } ;
6967
7068 res . send ( object ) ;
7169 } catch ( err ) {
72- next ( new HttpError ( err . code || StatusCodes . INTERNAL_SERVER_ERROR , err ) ) ;
70+ next ( new HttpError ( err . code ?? StatusCodes . INTERNAL_SERVER_ERROR , err ) ) ;
7371 }
7472} ;
7573
76- const update = async ( req : Request , res : Response , next : NextFunction ) => {
74+ const update = ( req : Request , res : Response , next : NextFunction ) : void => {
7775 try {
7876 // TODO: replace object
79- const object = req . body ;
77+ const object = { ... req . body } ;
8078
8179 res . send ( object ) ;
8280 } catch ( err ) {
83- next ( new HttpError ( err . code || StatusCodes . INTERNAL_SERVER_ERROR , err ) ) ;
81+ next ( new HttpError ( err . code ?? StatusCodes . INTERNAL_SERVER_ERROR , err ) ) ;
8482 }
8583} ;
8684
87- const del = async ( req : Request , res : Response , next : NextFunction ) => {
85+ const del = ( req : Request , res : Response , next : NextFunction ) : void => {
8886 try {
8987 // TODO: delete object
90- const object = ( req as MyRequest ) [ ENTITY ] ;
91-
92- res . send ( object ) ;
88+ res . send ( ( req as MyRequest ) [ ENTITY ] ) ;
9389 } catch ( err ) {
94- next ( new HttpError ( err . code || StatusCodes . INTERNAL_SERVER_ERROR , err ) ) ;
90+ next ( new HttpError ( err . code ?? StatusCodes . INTERNAL_SERVER_ERROR , err ) ) ;
9591 }
9692} ;
9793
0 commit comments