Skip to content

Instantly share code, notes, and snippets.

@chrisckchang
Last active September 11, 2015 21:38
Show Gist options
  • Save chrisckchang/de5463351dca9c713010 to your computer and use it in GitHub Desktop.
Save chrisckchang/de5463351dca9c713010 to your computer and use it in GitHub Desktop.
Todolist POST endpoint
//comments: We’ve added some basic input validation to ensure that the basic requirement that every todo list has a name is met.
app.post("/todolists", function(req, res) {
var postQuery = {createDate: new Date()};
if (req.body.name == "undefined") {
handleError(null, res, "Must provide a todolist name");
}
postQuery.name = req.body.name;
if (req.body.todos !== "undefined") {
var todos = req.body.todos;
todos.forEach(function(elem) {
if (!("_id" in elem)) {
elem._id = new ObjectId();
}
});
postQuery.todos = todos;
}
db.collection("todolists").insertOne(postQuery, function(err, doc) {
if (err) {
handleError(err, res, "Failed to create new todolist.");
} else {
console.log("Successfully inserted new todolist");
res.status(201).json(doc.ops[0]);
}
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment