diff --git a/pte-api/package.json b/pte-api/package.json index 1ef0ac4..0780601 100644 --- a/pte-api/package.json +++ b/pte-api/package.json @@ -7,10 +7,13 @@ "@aws-sdk/client-dynamodb": "^3.188.0", "@aws-sdk/lib-dynamodb": "^3.188.0", "@types/aws-lambda": "^8.10.126", + "dotenv": "^16.3.1", + "pg": "^8.11.3", "ts-jest": "^29.1.1" }, "devDependencies": { "@types/jest": "^29.5.10", + "@types/pg": "^8.10.9", "aws-sdk-client-mock": "^2.0.0", "jest": "^29.7.0", "typescript": "^5.3.2" diff --git a/pte-api/src/handlers/predefined-question/get-predefined-questions.ts b/pte-api/src/handlers/predefined-question/get-predefined-questions.ts new file mode 100644 index 0000000..993cb59 --- /dev/null +++ b/pte-api/src/handlers/predefined-question/get-predefined-questions.ts @@ -0,0 +1,43 @@ +import { Client } from 'pg'; +import { APIGatewayProxyHandler } from 'aws-lambda'; +import dotenv from 'dotenv'; + +// help get .env variable +dotenv.config(); + +export const getAllPredefinedQuestions: APIGatewayProxyHandler = async (event, context): Promise => { + // create a DB client to interact with DB + const client = new Client({ + host: process.env.DB_HOST, + port: parseInt(process.env.DB_PORT || '5432'), + user: process.env.DB_USER, + password: process.env.DB_PASSWORD, + database: process.env.DB_NAME + }); + + try { + // connect to DB + await client.connect(); + + // use query to get data from DB + const res = await client.query('SELECT * FROM predefined_question'); + + // disconnect from DB + await client.end(); + + // success: return data + return { + statusCode: 200, + body: JSON.stringify(res.rows) + }; + } catch (err) { + // failure: log and return error message + if (err instanceof Error) { + console.error('Database error', err.stack); + } + return { + statusCode: 500, + body: 'Error connecting to the database' + }; + } +}; diff --git a/pte-api/template.yaml b/pte-api/template.yaml index 45895d9..68a9daf 100644 --- a/pte-api/template.yaml +++ b/pte-api/template.yaml @@ -33,6 +33,22 @@ Resources: passthroughBehavior: "when_no_match" httpMethod: "POST" type: "aws_proxy" + /predefined-questions: + get: + produces: + - "application/json" + responses: + "200": + description: "200 response" + x-amazon-apigateway-integration: + uri: + Fn::Sub: arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${GetAllPredefinedQuestionsFunction.Arn}/invocations + responses: + default: + statusCode: "200" + passthroughBehavior: "when_no_match" + httpMethod: "POST" + type: "aws_proxy" HelloWorldFunction: Type: AWS::Serverless::Function @@ -48,6 +64,20 @@ Resources: Method: get RestApiId: Ref: HelloWorldApi + GetAllPredefinedQuestionsFunction: + Type: AWS::Serverless::Function + Properties: + CodeUri: ./ + Handler: dist/handlers/predefined-question/get-predefined-questions.getAllPredefinedQuestions + Runtime: nodejs18.x + Events: + ApiEvent: + Type: Api + Properties: + Path: /predefined-questions + Method: get + RestApiId: + Ref: HelloWorldApi MyS3Bucket: Type: AWS::S3::Bucket