-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsmartpill-editPill.js
More file actions
51 lines (42 loc) · 1.38 KB
/
smartpill-editPill.js
File metadata and controls
51 lines (42 loc) · 1.38 KB
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
'use strict'
var aws= require('aws-sdk');
const documentClient = new aws.DynamoDB.DocumentClient({region: 'eu-west-1'});
exports.handler = async (event, context, callback) => {
// Get pillId as the UUID for the lambda event in order to be random and unique
const pillId = event.pathParameters.id;
// Get pill data from request body and print it for debug
var pill = JSON.parse(event.body);
console.log('Pill: \n'+pill);
console.log('Pill name: '+pill.pill);
// Launch database request to edit database
await editPill(pillId, pill).then(() => {
callback(null, {
statusCode: 200,
body: JSON.stringify(pill),
headers: {
'Content-Type': 'application/json',
'Access-Control-Allow-Origin': '*'
}
})
}).catch((err) => {
console.error(err)
});
};
// Edit database function
function editPill(pillId, pill) {
var params = {
TableName: 'smartpill-pills',
HashKey: 'pillId',
Item: {
'id': pillId,
'pill': pill.pill,
'weight': pill.weight,
'deposit': pill.deposit,
'image_url': pill.image_url,
'morning': pill.morning,
'afternoon': pill.afternoon,
'night': pill.night
}
}
return documentClient.put(params).promise();
}