From 4d94acee77cd90341f23ab5d39d2ba90bc7edf2c Mon Sep 17 00:00:00 2001 From: bill bittner Date: Thu, 7 Sep 2017 12:24:40 -0700 Subject: [PATCH] moved sql from controller to model --- controllers/statsController.js | 6 +++--- models/index.js | 12 ++++++++++-- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/controllers/statsController.js b/controllers/statsController.js index 7df80bad..1b400e02 100644 --- a/controllers/statsController.js +++ b/controllers/statsController.js @@ -147,7 +147,7 @@ module.exports = { const dateTime = startDate.toISOString().slice(0, 19).replace('T', ' '); return new Promise((resolve, reject) => { // get the raw requests data - db.sequelize.query(`SELECT COUNT(*), File.* FROM Request LEFT JOIN File ON Request.FileId = File.id WHERE FileId IS NOT NULL AND nsfw != 1 AND trendingEligible = 1 AND Request.createdAt > "${dateTime}" GROUP BY FileId ORDER BY COUNT(*) DESC LIMIT 25;`, { type: db.sequelize.QueryTypes.SELECT }) + db.getTrendingClaims(dateTime) .then(results => { resolve(results); }) @@ -157,11 +157,11 @@ module.exports = { }); }); }, - getRecentClaims (startDate) { + getRecentClaims () { logger.debug('retrieving most recent claims'); return new Promise((resolve, reject) => { // get the raw requests data - db.sequelize.query(`SELECT * FROM File WHERE nsfw != 1 AND trendingEligible = 1 ORDER BY createdAt DESC LIMIT 25;`, { type: db.sequelize.QueryTypes.SELECT }) + db.getRecentClaims() .then(results => { resolve(results); }) diff --git a/models/index.js b/models/index.js index ce5b27c3..8c0fbce5 100644 --- a/models/index.js +++ b/models/index.js @@ -43,6 +43,9 @@ Object.keys(db).forEach(modelName => { } }); +db.sequelize = sequelize; +db.Sequelize = Sequelize; + db['upsert'] = (Model, values, condition, tableName) => { return Model .findOne({ where: condition }) @@ -59,7 +62,12 @@ db['upsert'] = (Model, values, condition, tableName) => { }); }; -db.sequelize = sequelize; -db.Sequelize = Sequelize; +db['getTrendingClaims'] = (startDate) => { + return db.sequelize.query(`SELECT COUNT(*), File.* FROM Request LEFT JOIN File ON Request.FileId = File.id WHERE FileId IS NOT NULL AND nsfw != 1 AND trendingEligible = 1 AND Request.createdAt > "${startDate}" GROUP BY FileId ORDER BY COUNT(*) DESC LIMIT 25;`, { type: db.sequelize.QueryTypes.SELECT }); +}; + +db['getRecentClaims'] = () => { + return db.sequelize.query(`SELECT * FROM File WHERE nsfw != 1 AND trendingEligible = 1 ORDER BY createdAt DESC LIMIT 25;`, { type: db.sequelize.QueryTypes.SELECT }); +}; module.exports = db;