Fix quotes web component [Close #419]

This commit is contained in:
reality 2013-04-23 17:26:44 +00:00
parent cca4fadc42
commit 41f4a830b2
2 changed files with 43 additions and 19 deletions

View File

@ -3,24 +3,33 @@ var pages = function(dbot) {
return { return {
// Lists quotes in a category // Lists quotes in a category
'/quotes/:key': function(req, res) { '/quotes/:key': function(req, res) {
var key = req.params.key.toLowerCase(); this.api.getQuoteCategory(req.params.key, function(category) {
if(_.has(dbot.db.quoteArrs, key)) { if(category) {
res.render('quotes', { 'name': dbot.config.name, 'quotes': dbot.db.quoteArrs[key], locals: { 'url_regex': RegExp.prototype.url_regex() } }); res.render('quotes', {
} else { 'name': dbot.config.name,
res.render('error', { 'name': dbot.config.name, 'message': 'No quotes under that key.' }); 'quotes': category.quotes,
} 'locals': {
'url_regex': RegExp.prototype.url_regex()
}
});
} else {
res.render('error', {
'name': dbot.config.name,
'message': 'No quotes under that key.'
});
}
});
}, },
// Show quote list. // Show quote list.
'/quotes': function(req, res) { '/quotes': function(req, res) {
res.render('quotelist', { 'name': dbot.config.name, 'quotelist': Object.keys(dbot.db.quoteArrs) }); this.api.getCategoryKeys(function(keys) {
res.render('quotelist', {
'name': dbot.config.name,
'quotelist': keys
});
});
}, },
// Load random quote category page
'/rq': function(req, res) {
var rCategory = Object.keys(dbot.db.quoteArrs).random();
res.render('quotes', { 'name': dbot.config.name, 'quotes': dbot.db.quoteArrs[rCategory], locals: { 'url_regex': RegExp.prototype.url_regex() } });
}
} }
}; };

View File

@ -90,12 +90,7 @@ var quotes = function(dbot) {
}, },
'getQuote': function(key, callback) { 'getQuote': function(key, callback) {
var category = false, this.api.getQuoteCategory(key, function(category) {
key = key.trim().toLowerCase();
this.db.search('quote_category', { 'name': key }, function(result) {
category = result;
}, function(err) {
if(category) { if(category) {
var quotes = category.quotes; var quotes = category.quotes;
var index = _.random(0, quotes.length - 1); var index = _.random(0, quotes.length - 1);
@ -116,6 +111,26 @@ var quotes = function(dbot) {
callback(quote); callback(quote);
} }
}.bind(this)); }.bind(this));
},
'getQuoteCategory': function(key, callback) {
var category = false,
key = key.trim().toLowerCase();
this.db.search('quote_category', { 'name': key }, function(result) {
category = result;
}, function(err) {
callback(category);
});
},
'getCategoryKeys': function(callback) {
var keys = [];
this.db.scan('quote_category', function(result) {
if(result) keys.push(result.name);
}, function(err) {
callback(keys);
});
} }
}; };