11const { Client } = require ( 'pg' ) ;
22
33exports . handler = async ( event ) => {
4-
5- // Create a NEW client inside handler (not globally)
6- const client = new Client ( {
7- host : process . env . DB_HOST ,
8- user : process . env . DB_USER ,
9- password : process . env . DB_PASSWORD ,
10- database : process . env . DB_NAME ,
11- port : 5432 ,
12- ssl : {
13- rejectUnauthorized : false // IMPORTANT for RDS
14- }
4+ // Create a NEW client inside handler
5+ const client = new Client ( {
6+ host : process . env . DB_HOST ,
7+ user : process . env . DB_USER ,
8+ password : process . env . DB_PASSWORD ,
9+ database : process . env . DB_NAME ,
10+ port : 5432 ,
11+ ssl : { rejectUnauthorized : false } // IMPORTANT for RDS
1512 } ) ;
16-
13+
1714 await client . connect ( ) ;
1815
1916 const method = event . httpMethod ;
2017 let body = { } ;
18+ let statusCode = 200 ; // default
2119
2220 try {
2321 if ( method === 'POST' ) {
@@ -27,13 +25,25 @@ const client = new Client({
2725 [ data . name , data . email ]
2826 ) ;
2927 body = res . rows [ 0 ] ;
28+ statusCode = 201 ; // Created
3029
3130 } else if ( method === 'GET' ) {
3231 const id = event . pathParameters ? event . pathParameters . id : null ;
3332 const res = id
3433 ? await client . query ( 'SELECT * FROM users WHERE id=$1' , [ id ] )
3534 : await client . query ( 'SELECT * FROM users' ) ;
36- body = res . rows ;
35+ if ( id ) {
36+ if ( ! res . rows [ 0 ] ) {
37+ body = { message : 'User not found' } ;
38+ statusCode = 404 ; // Not found
39+ } else {
40+ body = res . rows [ 0 ] ;
41+ statusCode = 200 ;
42+ }
43+ } else {
44+ body = res . rows ;
45+ statusCode = 200 ;
46+ }
3747
3848 } else if ( method === 'PUT' ) {
3949 const id = event . pathParameters . id ;
@@ -42,29 +52,41 @@ const client = new Client({
4252 'UPDATE users SET name=$1, email=$2 WHERE id=$3 RETURNING *' ,
4353 [ data . name , data . email , id ]
4454 ) ;
45- body = res . rows [ 0 ] ;
55+ if ( ! res . rows [ 0 ] ) {
56+ body = { message : 'User not found' } ;
57+ statusCode = 404 ;
58+ } else {
59+ body = res . rows [ 0 ] ;
60+ statusCode = 200 ;
61+ }
4662
4763 } else if ( method === 'DELETE' ) {
4864 const id = event . pathParameters . id ;
49- await client . query ( 'DELETE FROM users WHERE id=$1' , [ id ] ) ;
50- body = { message : 'Deleted successfully' } ;
65+ const res = await client . query ( 'DELETE FROM users WHERE id=$1 RETURNING *' , [ id ] ) ;
66+ if ( ! res . rows [ 0 ] ) {
67+ body = { message : 'User not found' } ;
68+ statusCode = 404 ;
69+ } else {
70+ body = { message : 'Deleted successfully' } ;
71+ statusCode = 200 ;
72+ }
5173
5274 } else {
5375 body = { message : 'Unsupported method' } ;
76+ statusCode = 400 ;
5477 }
5578
5679 } catch ( err ) {
5780 body = { error : err . message } ;
81+ statusCode = 500 ;
5882
5983 } finally {
6084 await client . end ( ) ; // always close the connection
6185 }
6286
6387 return {
64- statusCode : 200 ,
88+ statusCode,
6589 headers : { "Content-Type" : "application/json" } ,
6690 body : JSON . stringify ( body )
6791 } ;
6892} ;
69-
70-
0 commit comments