Skip to content

Commit 95b39a5

Browse files
committed
feat: added update todo function.
1 parent f2e0f03 commit 95b39a5

File tree

1 file changed

+36
-1
lines changed

1 file changed

+36
-1
lines changed

todos/routes.js

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,42 @@ router
7777
});
7878
})
7979
.put(function update(req, res) {
80-
res.send("update");
80+
const timestamp = new Date().getTime();
81+
const { text, checked } = req.body;
82+
83+
// validation
84+
if (typeof text !== "string" || typeof checked !== "boolean") {
85+
console.error("Validation failed");
86+
res.status(400).send({ error: "Validation failed" });
87+
}
88+
89+
const params = Object.assign({}, tableParams, {
90+
Key: {
91+
id: req.params.id
92+
},
93+
ExpressionAttributeNames: {
94+
"#todo_text": "text"
95+
},
96+
ExpressionAttributeValues: {
97+
":text": text,
98+
":checked": checked,
99+
":updatedAt": timestamp
100+
},
101+
UpdateExpression:
102+
"SET #todo_text = :text, checked = :checked, updatedAt = :updatedAt",
103+
ReturnValues: "ALL_NEW"
104+
});
105+
106+
dynamoDb.update(params, (error, result) => {
107+
if (error) {
108+
console.error(error);
109+
res
110+
.status(error.statusCoee || 501)
111+
.send({ error: "Could not update todo item" });
112+
} else {
113+
res.send(result.Attributes);
114+
}
115+
});
81116
})
82117
.delete((req, res) => {
83118
res.end("delete");

0 commit comments

Comments
 (0)