Skip to content

Commit 6770d95

Browse files
committed
feat: added create todo function.
1 parent d9d7636 commit 6770d95

File tree

3 files changed

+137
-6
lines changed

3 files changed

+137
-6
lines changed

package-lock.json

Lines changed: 102 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,11 @@
1717
},
1818
"homepage": "https://github.com/melvinlee/aws-lambda-express-restapi#readme",
1919
"dependencies": {
20+
"aws-sdk": "^2.224.1",
2021
"body-parser": "^1.18.2",
2122
"express": "^4.16.3",
22-
"serverless-http": "^1.5.5"
23+
"serverless-http": "^1.5.5",
24+
"uuid": "^3.2.1"
2325
},
2426
"devDependencies": {
2527
"eslint": "^4.19.1",

todos/routes.js

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,47 @@
11
const express = require("express");
22
const routes = express();
33
const bodyParser = require("body-parser");
4+
const AWS = require("aws-sdk");
5+
const uuid = require("uuid");
46

57
const router = express.Router();
68

9+
const dynamoDb = new AWS.DynamoDB.DocumentClient();
10+
11+
const params = {
12+
TableName: process.env.DYNAMODB_TABLE
13+
};
14+
715
router
816
.route("/todos")
917
.get(function list(req, res) {
1018
res.send("list");
1119
})
1220
.post(function create(req, res) {
13-
res.send("create");
21+
const timestamp = new Date().getTime();
22+
const { text } = req.body;
23+
24+
if (typeof text !== "string") {
25+
res.status(400).send({ error: "Couldn't create todo item." });
26+
}
27+
28+
const Item = {
29+
id: uuid.v1(),
30+
text: text,
31+
checked: false,
32+
createAt: timestamp,
33+
updateAt: timestamp
34+
};
35+
36+
const createParam = Object.assign({}, params, { Item: Item });
37+
38+
dynamoDb.put(createParam, err => {
39+
if (err) {
40+
console.error(err);
41+
res.status(400).send("Couldn't create todo item.");
42+
}
43+
});
44+
res.send(createParam);
1445
});
1546

1647
router

0 commit comments

Comments
 (0)