Add handlers for rendering user history

This commit is contained in:
Aron Carroll 2012-05-18 15:51:20 +01:00
parent 7139077f0e
commit b831501ec6

View File

@ -48,6 +48,18 @@ module.exports = handlers = {
handlers.getBinPreview(req, res);
}
},
getUserBins: function (req, res, next) {
if (!req.session.user) {
return res.send('');
}
req.models.user.getBins(req.session.user.name, function (err, bins) {
if (err) {
return next(err);
}
handlers.renderHistory(req, res, bins);
});
},
redirectToLatest: function (req, res) {
var path = req.originalUrl.replace('latest', req.bin.revision);
res.redirect(303, path);
@ -184,6 +196,11 @@ module.exports = handlers = {
res.redirect(303, req.helpers.url(bin.url + '/' + bin.revision + '/edit'));
}
},
renderHistory: function (req, res, bins) {
handlers.formatHistory(bins, req.helpers, function (err, history) {
res.send(history);
});
},
jsbin: function (bin, version, token) {
return {
root: '',
@ -261,5 +278,33 @@ module.exports = handlers = {
} else {
fn(null, formatted);
}
},
formatHistory: function (bins, helpers, fn) {
var map = {}, data = [], key;
bins.forEach(function (bin) {
if (!map[bin.url]) {
map[bin.url] = [];
}
map[bin.url].push({
title: utils.titleForBin(bin),
code: bin.url,
revision: bin.revision,
url: helpers.urlForBin(bin),
edit_url: helpers.editUrlForBin(bin),
created: bin.created.toISOString(),
pretty_created: utils.since(bin.created),
is_first: !map[bin.url].length
});
});
for (key in map) {
if (map.hasOwnProperty(key)) {
data.push(map[key]);
}
}
helpers.render('history', {bins: data}, fn);
}
};