-
Notifications
You must be signed in to change notification settings - Fork 10
/
index.js
49 lines (42 loc) · 1.29 KB
/
index.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
const ApiBuilder = require('claudia-api-builder'),
AWS = require('aws-sdk');
var api = new ApiBuilder(),
dynamoDb = new AWS.DynamoDB.DocumentClient();
api.post('/icecreams', function (request) { // SAVE your icecream
var parameters = {
TableName: 'icecreams',
Item: {
icecreamid: request.body.icecreamId,
name: request.body.name // your icecream name
}
};
return dynamoDb.put(parameters).promise(); // returns dynamo result
}, { success: 201 }); // returns HTTP status 201 - Created if successful
api.get('/icecreams', function (request) { // GET all users
return dynamoDb.scan({ TableName: 'icecreams' }).promise()
.then(response => response.Items)
});
api.patch('/icecreams/{id}', (request) => { //PATCH your icecream
let params = {
TableName: 'icecreams',
Item: {
icecreamid: request.pathParams.id,
name: request.body.name //your icecream name
}
};
return dynamoDb.put(params).promise();
}, {success: 201});
api.delete('/icecreams/{id}', (request) => { //DELETE your icecream
let id = request.pathParams.id;
let params = {
TableName: 'icecreams',
Key: {
icecreamid: id,
}
};
return dynamoDb.delete(params).promise()
.then(() => {
return 'Deleted icecream with id "' + id + '"';
});
}, {success: 201});
module.exports = api;