1- import { Request as ExpressRequest , Response , NextFunction } from 'express' ;
1+ import { Request , Response , NextFunction } from 'express' ;
22import {
33 BAD_REQUEST ,
44 NOT_FOUND ,
55 INTERNAL_SERVER_ERROR ,
66} from 'http-status-codes' ;
77import { HttpError } from '@boringcodes/utils/error' ;
88
9- import { NAME } from './constants' ;
9+ import { ENTITY } from './constants' ;
1010
11- interface Request extends ExpressRequest {
12- readonly [ NAME ] : any ;
11+ interface MyRequest extends Request {
12+ readonly [ ENTITY ] : any ;
1313}
1414
1515const list = async ( _ : Request , res : Response , next : NextFunction ) => {
1616 try {
1717 // TODO: list objects
18- const objects = [ ] ;
18+ const objects : any [ ] = [ ] ;
1919
2020 res . send ( objects ) ;
2121 } catch ( err ) {
2222 next ( new HttpError ( err . code || INTERNAL_SERVER_ERROR , err ) ) ;
2323 }
2424} ;
2525
26- const count = async ( _ : Request , res : Response , next : NextFunction ) => {
27- try {
28- // TODO: count objects
29- const count = 0 ;
30-
31- res . send ( { count } ) ;
32- } catch ( err ) {
33- next ( new HttpError ( err . code || INTERNAL_SERVER_ERROR , err ) ) ;
34- }
35- } ;
36-
3726const create = async ( req : Request , res : Response , next : NextFunction ) => {
3827 try {
3928 // TODO: create object
@@ -46,65 +35,68 @@ const create = async (req: Request, res: Response, next: NextFunction) => {
4635} ;
4736
4837const getById = async ( req : Request , _ : Response , next : NextFunction ) => {
49- if ( ! req . params . id ) {
50- next ( new HttpError ( BAD_REQUEST , 'Invalid resource Id' ) ) ;
51-
52- return ;
53- }
54-
5538 try {
56- // TODO: get object
57- const object = { } ;
39+ if ( ! req . params . id ) {
40+ throw new HttpError ( BAD_REQUEST , 'Invalid resource Id' ) ;
41+ }
5842
43+ // TODO: get object by id
44+ const object = { } ;
5945 if ( ! object ) {
60- next ( new HttpError ( NOT_FOUND , 'Resource not found' ) ) ;
61-
62- return ;
46+ return next ( new HttpError ( NOT_FOUND , 'Resource not found' ) ) ;
6347 }
64- // tslint:disable-next-line:no-object-mutation
65- Object . assign ( req , { [ NAME ] : object } ) ;
48+ Object . assign ( req , { [ ENTITY ] : object } ) ;
6649
6750 next ( ) ;
6851 } catch ( err ) {
6952 next ( new HttpError ( err . code || INTERNAL_SERVER_ERROR , err ) ) ;
7053 }
7154} ;
7255
73- const get = ( req : Request , res : Response ) => {
74- res . send ( req [ NAME ] ) ;
56+ const get = ( req : Request , res : Response , next : NextFunction ) => {
57+ try {
58+ // TODO: get object
59+ res . send ( ( req as MyRequest ) [ ENTITY ] ) ;
60+ } catch ( err ) {
61+ next ( new HttpError ( err . code || INTERNAL_SERVER_ERROR , err ) ) ;
62+ }
7563} ;
7664
77- const patch = async ( req : Request , res : Response , next : NextFunction ) => {
65+ const updatePartial = async (
66+ req : Request ,
67+ res : Response ,
68+ next : NextFunction ,
69+ ) => {
7870 try {
79- // TODO: patch object
80- const object = { ...req [ NAME ] , ...req . body } ;
71+ // TODO: update partial object
72+ const object = { ...( req as MyRequest ) [ ENTITY ] , ...req . body } ;
8173
8274 res . send ( object ) ;
8375 } catch ( err ) {
84- next ( err ) ;
76+ next ( new HttpError ( err . code || INTERNAL_SERVER_ERROR , err ) ) ;
8577 }
8678} ;
8779
8880const update = async ( req : Request , res : Response , next : NextFunction ) => {
8981 try {
90- // TODO: update object
82+ // TODO: replace object
9183 const object = req . body ;
9284
9385 res . send ( object ) ;
9486 } catch ( err ) {
95- next ( err ) ;
87+ next ( new HttpError ( err . code || INTERNAL_SERVER_ERROR , err ) ) ;
9688 }
9789} ;
9890
9991const del = async ( req : Request , res : Response , next : NextFunction ) => {
10092 try {
10193 // TODO: delete object
102- const object = req [ NAME ] ;
94+ const object = ( req as MyRequest ) [ ENTITY ] ;
10395
10496 res . send ( object ) ;
10597 } catch ( err ) {
10698 next ( new HttpError ( err . code || INTERNAL_SERVER_ERROR , err ) ) ;
10799 }
108100} ;
109101
110- export { list , create , count , getById , get , patch , update , del } ;
102+ export { list , create , getById , get , updatePartial , update , del } ;
0 commit comments