-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathindex.js
62 lines (54 loc) · 1.46 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
50
51
52
53
54
55
56
57
58
59
60
61
62
/**
* Copyright (c) 2016 Jeremy Thomerson
* Licensed under the MIT license.
*/
'use strict';
var AWS = require('aws-sdk'),
dynamo = new AWS.DynamoDB.DocumentClient();
exports.handler = function(event, context, callback) {
var method = event.context['http-method'].toUpperCase();
switch (method) {
case 'GET':
handleGet(event, context, callback);
break;
case 'POST':
handlePost(event, context, callback);
break;
default:
callback('Unsupported method');
}
};
function handlePost(event, context, callback) {
var params = {
TableName: event.custom.DynamoDBTableName,
// TODO: obviously there's no request validation going on here like there should be
Item: {
GUID: event['body-json'].GUID,
DueDate: event['body-json'].DueDate,
Title: event['body-json'].Title,
IsCompleted: event['body-json'].IsCompleted,
}
};
dynamo.put(params, function(err, data) {
if (err) {
callback(null, { customError: 'Error adding item: ' + JSON.stringify(err, null, 2) });
} else {
callback(null, {
data: data
});
}
});
}
function handleGet(event, context, callback) {
var params = {
TableName: event.custom.DynamoDBTableName,
Limit: 10,
};
dynamo.scan(params, function(err, data) {
callback(null, {
input: event,
err: err,
output: data
});
});
}