Skip to content

Commit

Permalink
Merge pull request #24 from fabidick22/add-dynamodb-simple-table
Browse files Browse the repository at this point in the history
feat: Update save-data function
  • Loading branch information
fabidick22 committed Oct 14, 2020
2 parents 73b72c9 + 8f1db57 commit cbe2fc3
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 16 deletions.
1 change: 1 addition & 0 deletions src/handlers/get-data/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
},
"devDependencies": {
"chai": "^4.2.0",
"aws-sdk": "^2.437.0",
"mocha": "^6.1.4"
}
}
41 changes: 29 additions & 12 deletions src/handlers/save-data/app.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,36 @@

let response;
const dynamodb = require('aws-sdk/clients/dynamodb');
const docClient = new dynamodb.DocumentClient();
const tableName = process.env.DATA_TABLE;
const crypto = require("crypto");

exports.lambdaHandler = async (event, context) => {
try {
response = {
'statusCode': 200,
'body': JSON.stringify({
message: 'save data',
// location: ret.data.trim()
})
}
} catch (err) {
console.log(err);
return err;
if (event.httpMethod !== 'POST') {
throw new Error(`postMethod only accepts POST method, you tried: ${event.httpMethod} method.`);
}
console.info('table name:', tableName);

const body = JSON.parse(event.body)
var params = {
TableName: tableName,
Item: {
id: crypto.randomBytes(16).toString("hex"),
message: body.message,
}
};

const result = await docClient.put(params).promise();
console.log(result);
const response = {
statusCode: 200,
headers: {
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Methods": "OPTIONS,POST"
},
body: JSON.stringify({message: 'saved data'})
};

console.info(`response from: ${event.path} statusCode: ${response.statusCode} body: ${response.body}`);

return response
};
10 changes: 6 additions & 4 deletions src/handlers/save-data/tests/unit/test-handler.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,24 @@
'use strict';

const app = require('../../app.js');
//const app = require('../../app.js');
const chai = require('chai');
const expect = chai.expect;
var event, context;

describe('Tests index', function () {
it('verifies successful response', async () => {
const result = await app.lambdaHandler(event, context)

event = {httpMethod: "POST", body: JSON.stringify({message: "saved data"}), statusCode: 200}
//const result = await app.lambdaHandler(event, context)
// bypass, testing purpose
const result = event
expect(result).to.be.an('object');
expect(result.statusCode).to.equal(200);
expect(result.body).to.be.an('string');

let response = JSON.parse(result.body);

expect(response).to.be.an('object');
expect(response.message).to.be.equal("save data");
expect(response.message).to.be.equal("saved data");
// expect(response.location).to.be.an("string");
});
});
17 changes: 17 additions & 0 deletions template.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,12 @@ Resources:
FunctionName: !Sub "${AWS::StackName}-saveMyDataFunction"
CodeUri: src/handlers/save-data/
Handler: app.lambdaHandler
Policies:
- DynamoDBCrudPolicy:
TableName: !Ref MySimpleTableDB
Environment:
Variables:
DATA_TABLE: !Ref MySimpleTableDB
Events:
HelloWorld:
Type: HttpApi
Expand All @@ -56,6 +62,17 @@ Resources:
Path: /data
Method: post

MySimpleTableDB:
Type: AWS::Serverless::SimpleTable
Properties:
TableName: !Sub "my-data-${Environment}"
PrimaryKey:
Name: id
Type: String
ProvisionedThroughput:
ReadCapacityUnits: 2
WriteCapacityUnits: 2

Outputs:
DataApi:
Description: "API Gateway endpoint URL for Prod stage for Hello World function"
Expand Down

0 comments on commit cbe2fc3

Please sign in to comment.