moved sql from controller to model

This commit is contained in:
bill bittner 2017-09-07 12:24:40 -07:00
parent c201e1ecfd
commit 4d94acee77
2 changed files with 13 additions and 5 deletions

View file

@ -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);
})

View file

@ -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;