Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions pte-api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
Original file line number Diff line number Diff line change
@@ -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<any> => {
// 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'
};
}
};
30 changes: 30 additions & 0 deletions pte-api/template.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down