Last active
September 11, 2015 21:38
-
-
Save chrisckchang/de5463351dca9c713010 to your computer and use it in GitHub Desktop.
Todolist POST endpoint
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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