diff --git a/modules/quotes/pages.js b/modules/quotes/pages.js index 7800cd0..0f0c977 100644 --- a/modules/quotes/pages.js +++ b/modules/quotes/pages.js @@ -3,24 +3,33 @@ var pages = function(dbot) { return { // Lists quotes in a category '/quotes/:key': function(req, res) { - var key = req.params.key.toLowerCase(); - if(_.has(dbot.db.quoteArrs, key)) { - res.render('quotes', { 'name': dbot.config.name, 'quotes': dbot.db.quoteArrs[key], locals: { 'url_regex': RegExp.prototype.url_regex() } }); - } else { - res.render('error', { 'name': dbot.config.name, 'message': 'No quotes under that key.' }); - } + this.api.getQuoteCategory(req.params.key, function(category) { + if(category) { + res.render('quotes', { + 'name': dbot.config.name, + '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. '/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() } }); - } } }; diff --git a/modules/quotes/quotes.js b/modules/quotes/quotes.js index 36287f1..1d0b842 100644 --- a/modules/quotes/quotes.js +++ b/modules/quotes/quotes.js @@ -90,12 +90,7 @@ var quotes = function(dbot) { }, 'getQuote': function(key, callback) { - var category = false, - key = key.trim().toLowerCase(); - - this.db.search('quote_category', { 'name': key }, function(result) { - category = result; - }, function(err) { + this.api.getQuoteCategory(key, function(category) { if(category) { var quotes = category.quotes; var index = _.random(0, quotes.length - 1); @@ -116,6 +111,26 @@ var quotes = function(dbot) { callback(quote); } }.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); + }); } };