Skip to content

Instantly share code, notes, and snippets.

@laser
Created December 31, 2014 18:32
Show Gist options
  • Save laser/390eddc614664999f42c to your computer and use it in GitHub Desktop.
Save laser/390eddc614664999f42c to your computer and use it in GitHub Desktop.
o
function ensure(notOkCode, notOkMessage, response, okFx) {
return function(err) {
if (err) {
response.status(notOkCode).json({"message": notOkMessage});
}
else {
okFx.apply(okFx, Array.prototype.slice.call(arguments, 1));
}
};
}
var ensureFound = _.partial(ensure, 404, "Not Found"),
ensureValid = _.partial(ensure, 400, "Bad Request");
expressRouter.get("/api/ledgers/:id", function(req, res) {
UserService.getLedgerById(req.params.id, ensureFound(ledger) {
res.status(200).json(ledger);
});
});
expressRouter.patch("/api/users/:id", function(req, res) {
UserService.getUserById(req.params.id, ensureFound(res, function(user) {
UserService.updateUser(user, req.body, ensureValid(res, function(updated) {
res.status(200).json(updated);
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment