From 4d23fbbe4ed184c869f9c30e7a9e64ea15bcdd98 Mon Sep 17 00:00:00 2001 From: bill bittner Date: Tue, 31 Oct 2017 17:19:26 -0700 Subject: [PATCH] updated getRecentClaims --- controllers/serveController.js | 8 ++++---- controllers/statsController.js | 2 +- models/certificate.js | 2 -- models/claim.js | 1 - models/file.js | 8 ++++++++ models/index.js | 4 ---- 6 files changed, 13 insertions(+), 12 deletions(-) diff --git a/controllers/serveController.js b/controllers/serveController.js index 023622f3..b4f54b6d 100644 --- a/controllers/serveController.js +++ b/controllers/serveController.js @@ -64,7 +64,7 @@ function getAssetByLongClaimId (fullClaimId, name) { } logger.debug('no local file found for this name and claimId'); // 2. if no local claim, resolve and get the claim - db + db.Claim .resolveClaim(name, fullClaimId) .then(resolveResult => { logger.debug('resolve result >> ', resolveResult); @@ -130,7 +130,7 @@ module.exports = { getAssetByChannel (channelName, channelId, claimName) { logger.debug('getting asset by channel'); return new Promise((resolve, reject) => { - db.getLongChannelId(channelName, channelId) // 1. get the long channel id + db.Certificate.getLongChannelId(channelName, channelId) // 1. get the long channel id .then(result => { // 2. get the long claim Id if (result === NO_CHANNEL) { resolve(NO_CHANNEL); @@ -155,7 +155,7 @@ module.exports = { return new Promise((resolve, reject) => { let longChannelId; let shortChannelId; - db.getLongChannelId(channelName, channelId) // 1. get the long channel Id + db.Certificate.getLongChannelId(channelName, channelId) // 1. get the long channel Id .then(result => { // 2. get all claims for that channel if (result === NO_CHANNEL) { return NO_CHANNEL; @@ -220,7 +220,7 @@ module.exports = { .getShortClaimIdFromLongClaimId(fileInfo.claimId, fileInfo.name) .then(shortId => { fileInfo['shortId'] = shortId; - return db.resolveClaim(fileInfo.name, fileInfo.claimId); + return db.Claim.resolveClaim(fileInfo.name, fileInfo.claimId); }) .then(resolveResult => { logger.debug('resolve result >>', resolveResult); diff --git a/controllers/statsController.js b/controllers/statsController.js index b728ede6..50dcc36d 100644 --- a/controllers/statsController.js +++ b/controllers/statsController.js @@ -97,7 +97,7 @@ module.exports = { logger.debug('retrieving most recent claims'); return new Promise((resolve, reject) => { // get the raw requests data - db.getRecentClaims() + db.File.getRecentClaims() .then(results => { resolve(results); }) diff --git a/models/certificate.js b/models/certificate.js index e21f71b7..dafb8281 100644 --- a/models/certificate.js +++ b/models/certificate.js @@ -146,7 +146,6 @@ module.exports = (sequelize, { STRING, BOOLEAN, INTEGER, TEXT, ARRAY, DECIMAL, D }); }; - // sequelize.query(`SELECT claimId, height FROM Certificate WHERE name = '${channelName}' AND claimId LIKE '${channelId}%' ORDER BY height ASC LIMIT 1;`, { type: db.sequelize.QueryTypes.SELECT }) Certificate.getLongChannelIdFromShortChannelId = function (channelName, channelId) { return new Promise((resolve, reject) => { this @@ -173,7 +172,6 @@ module.exports = (sequelize, { STRING, BOOLEAN, INTEGER, TEXT, ARRAY, DECIMAL, D }); }; - // sequelize.query(`SELECT claimId, amount, height FROM Certificate WHERE name = '${channelName}' ORDER BY effectiveAmount DESC, height ASC LIMIT 1;`, { type: db.sequelize.QueryTypes.SELECT }) Certificate.getLongChannelIdFromChannelName = function (channelName) { logger.debug(`getLongChannelIdFromChannelName(${channelName})`); return new Promise((resolve, reject) => { diff --git a/models/claim.js b/models/claim.js index 66cb3870..6c29010c 100644 --- a/models/claim.js +++ b/models/claim.js @@ -309,7 +309,6 @@ module.exports = (sequelize, { STRING, BOOLEAN, INTEGER, TEXT, ARRAY, DECIMAL, D } }; - // sequelize.query(`SELECT name, claimId, outpoint, height, address, title, description, thumbnail, certificateId, channelName FROM Claim WHERE name = '${name}' AND claimId = '${claimId}'`, { type: db.sequelize.QueryTypes.SELECT }) Claim.resolveClaim = function (name, claimId) { return new Promise((resolve, reject) => { this diff --git a/models/file.js b/models/file.js index c1e86772..1ba0b5fa 100644 --- a/models/file.js +++ b/models/file.js @@ -55,5 +55,13 @@ module.exports = (sequelize, { STRING, BOOLEAN, INTEGER }) => { File.hasOne(db.Claim); }; + File.getRecentClaims = function () { + return this.findAll({ + where: { nsfw: false, trendingEligible: true }, + order: [['createdAt', 'DESC']], + limit: 25, + }); + }; + return File; }; diff --git a/models/index.js b/models/index.js index 5e9bb620..f8293431 100644 --- a/models/index.js +++ b/models/index.js @@ -74,8 +74,4 @@ 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;