From c6878eb0a3df76d183186ec5a2b0e9fb4ae7e81d Mon Sep 17 00:00:00 2001 From: bill bittner Date: Tue, 31 Oct 2017 15:28:11 -0700 Subject: [PATCH] updated getClaimIdByLongChannelId --- controllers/serveController.js | 2 +- models/claim.js | 26 ++++++++++++++++++++++++ models/index.js | 37 ---------------------------------- 3 files changed, 27 insertions(+), 38 deletions(-) diff --git a/controllers/serveController.js b/controllers/serveController.js index d65c2b90..f0ed5f05 100644 --- a/controllers/serveController.js +++ b/controllers/serveController.js @@ -136,7 +136,7 @@ module.exports = { resolve(NO_CHANNEL); return; } - return db.getClaimIdByLongChannelId(result, claimName); + return db.Claim.getClaimIdByLongChannelId(result, claimName); }) .then(result => { // 3. get the asset using the long claim id logger.debug('asset claim id =', result); diff --git a/models/claim.js b/models/claim.js index 583e917e..c0df47a9 100644 --- a/models/claim.js +++ b/models/claim.js @@ -1,4 +1,5 @@ const logger = require('winston'); +const NO_CLAIM = 'NO_CLAIM'; function sortResult (result, longId) { let claimIndex; @@ -224,5 +225,30 @@ module.exports = (sequelize, { STRING, BOOLEAN, INTEGER, TEXT, ARRAY, DECIMAL, D }); }; + Claim.getClaimIdByLongChannelId = function (channelId, claimName) { + logger.debug(`finding claim id for claim ${claimName} from channel ${channelId}`); + return new Promise((resolve, reject) => { + Claim + .findAll({ + where: { name: claimName, certificateId: channelId }, + order: [['id', 'ASC']], + }) + .then(result => { + switch (result.length) { + case 0: + return resolve(NO_CLAIM); + case 1: + return resolve(result[0].claimId); + default: + logger.error(`${result.length} records found for ${claimName} from channel ${claimName}`); + return resolve(result[0].claimId); + } + }) + .catch(error => { + reject(error); + }); + }); + }; + return Claim; }; diff --git a/models/index.js b/models/index.js index 4894f2a8..fa527331 100644 --- a/models/index.js +++ b/models/index.js @@ -152,24 +152,6 @@ 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 }); }; -db['getAllFreeClaims'] = (name) => { - return new Promise((resolve, reject) => { - db - .sequelize.query(`SELECT name, claimId, outpoint, height, address FROM Claim WHERE name = '${name}' ORDER BY amount DESC, height ASC`, { type: db.sequelize.QueryTypes.SELECT }) - .then(result => { - switch (result.length) { - case 0: - return resolve(null); - default: - return resolve(result); - } - }) - .catch(error => { - reject(error); - }); - }); -}; - db['resolveClaim'] = (name, claimId) => { return new Promise((resolve, reject) => { db @@ -190,25 +172,6 @@ db['resolveClaim'] = (name, claimId) => { }); }; -db['getClaimIdByLongChannelId'] = (channelId, claimName) => { - return new Promise((resolve, reject) => { - logger.debug(`finding claim id for claim "${claimName}" from channel "${channelId}"`); - db - .sequelize.query(`SELECT claimId FROM Claim WHERE name = '${claimName}' AND certificateId = '${channelId}' LIMIT 1;`, { type: db.sequelize.QueryTypes.SELECT }) - .then(result => { - switch (result.length) { - case 0: - return resolve(NO_CLAIM); - default: - return resolve(result[0].claimId); - } - }) - .catch(error => { - reject(error); - }); - }); -}; - db['getLongClaimId'] = (claimName, claimId) => { logger.debug(`getLongClaimId(${claimName}, ${claimId})`); if (claimId && (claimId.length === 40)) { // if a full claim id is provided