forked from amazon-archives/aws-full-stack-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUpdateGoal.js
58 lines (52 loc) · 1.91 KB
/
UpdateGoal.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
const AWS = require('aws-sdk');
const dynamoDb = new AWS.DynamoDB.DocumentClient();
exports.handler = (event, context, callback) => {
// Request body is passed in as a JSON encoded string in 'event.body'
const data = JSON.parse(event.body);
const params = {
TableName: process.env.TABLE_NAME, //[ProjectName]-Goals,
// 'Key' defines the partition key and sort key of the item to be updated
// - 'userId': user identities are federated through the Cognito Identity
// Pool, we will use the identity id as the user id of the
// authenticated user
// - 'goalId': a unique identifier for the goal (uuid)
Key: {
userId: event.requestContext.identity.cognitoIdentityId,
goalId: event.pathParameters.id
},
// 'UpdateExpression' defines the attributes to be updated
// 'ExpressionAttributeValues' defines the value in the update expression
// - ':title': defines 'title' to be the title parsed from the request body
// - ':title': defines 'content' to be the content parsed from the request body
UpdateExpression: "SET title = :title, content = :content",
ExpressionAttributeValues: {
":title": data.title ? data.title : null,
":content": data.content ? data.content : null
},
ReturnValues: "ALL_NEW"
};
dynamoDb.update(params, (error, data) => {
// Set response headers to enable CORS (Cross-Origin Resource Sharing)
const headers = {
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Credentials" : true
};
// Return status code 500 on error
if (error) {
console.log(error);
const response = {
statusCode: 500,
headers: headers,
body: error
};
callback(null, response);
return;
}
// Return status code 200 on success
const response = {
statusCode: 200,
headers: headers
};
callback(null, response);
});
};