Skip to content

Instantly share code, notes, and snippets.

@JogoShugh
Created February 5, 2015 18:18
Show Gist options
  • Save JogoShugh/e25ffcff4042ee7de444 to your computer and use it in GitHub Desktop.
Save JogoShugh/e25ffcff4042ee7de444 to your computer and use it in GitHub Desktop.
digestsInboxes
app.get('/api/digests/:uuid/inboxes', function(req, res, next) {
function href(path) {
var protocol = config.protocol || req.protocol;
var host = req.get('host');
return protocol + "://" + host + path;
}
function createHyperMediaResponse(digestId, state) {
var inboxIds = _.keys(state.inboxes);
var response = {
"_links": {
"self": {
"href": href("/api/digests/" + digestId + "/inboxes"),
},
"digest": {
"href": href("/api/digests/" + digestId)
},
"inbox-create": {
"href": href("/api/inboxes"),
"method": "POST",
"title": "Endpoint for creating an inbox for a repository on digest " + digestId + "."
}
},
"count": inboxIds.length,
"_embedded": {
"inboxes": []
}
};
function createInboxHyperMediaResult(inbox) {
var result = {
"_links": {
"self": {
"href": href("/api/inboxes/" + inbox.inboxId)
},
"inbox-commits": {
"href": href("/api/inboxes/" + inbox.inboxId + "/commits"),
"method": "POST"
}
}
};
result = _.extend(result, _.omit(inbox, 'digestId'));
return result;
}
inboxIds.forEach(function(inboxId) {
response._embedded.inboxes.push(createInboxHyperMediaResult(state.inboxes[inboxId]));
});
return response;
}
if (!validator.isUUID(req.params.uuid)) {
res.status(400).send('The value "' + req.params.uuid + '" is not recognized as a valid digest identifier.');
} else {
eventStore.projection.getState({ name: 'inboxes-for-digest', partition: 'digestInbox-' + req.params.uuid }, function(err, resp) {
if (err) {
res.status(500).json({'error': 'There was an internal error when trying to process your request'});
} else if (!resp.body || resp.body.length < 1) {
res.status(404).json({'error': 'Could not find a digest with id ' + req.params.uuid});
} else { // all good
var state = JSON.parse(resp.body);
var hypermediaResponse = createHyperMediaResponse(req.params.uuid, state);
res.set('Content-Type', 'application/hal+json; charset=utf-8');
res.send(hypermediaResponse);
}
});
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment