From 758d91ebbf21baeb6d9a095d49282a4a33ebd40f Mon Sep 17 00:00:00 2001 From: Razvan Botea Date: Thu, 14 Apr 2016 17:24:46 +0300 Subject: [PATCH] * added 404 errors if indexed list doesn't exist --- index.js | 2 ++ lib/TelepatError.js | 5 +++++ lib/TelepatIndexedLists.js | 38 ++++++++++++++++++++++++++++---------- 3 files changed, 35 insertions(+), 10 deletions(-) diff --git a/index.js b/index.js index 1e5286e..f93289d 100644 --- a/index.js +++ b/index.js @@ -30,6 +30,8 @@ module.exports.ElasticSearch = require('./lib/database/elasticsearch_adapter'); module.exports.TelepatLogger = require('./lib/logger/logger'); +module.exports.TelepatIndexedList = require('./lib/TelepatIndexedLists'); + fs.readdirSync(__dirname+'/lib/message_queue').forEach(function(filename) { var filenameParts = filename.split('_'); diff --git a/lib/TelepatError.js b/lib/TelepatError.js index 8ef8d07..af3eeea 100644 --- a/lib/TelepatError.js +++ b/lib/TelepatError.js @@ -247,6 +247,11 @@ TelepatError.errors = { code: '047', message: 'This user account has not been confirmed', status: 403 + }, + TilNotFound: { + code: '048', + message: 'TelepatIndexedList with name "%s" does not exist', + status: 404 } } diff --git a/lib/TelepatIndexedLists.js b/lib/TelepatIndexedLists.js index f963730..27d319e 100644 --- a/lib/TelepatIndexedLists.js +++ b/lib/TelepatIndexedLists.js @@ -1,8 +1,9 @@ var Application = require('./Application'); +var TelepatError = require('./TelepatError'); var utils = require('../utils/utils'); var async = require('async'); -var TelepatIndexedLists = { +var TelepatIndexedList = { /** * @callback appendCb * @param {Error|null} [err] @@ -32,7 +33,7 @@ var TelepatIndexedLists = { var memeberName = Object.keys(object)[0]; var memeberValue = object[memeberName]; - this.redis.sadd([redisKey, memeberName, memeberValue], function(err) { + Application.redisCacheClient.sadd([redisKey, memeberName, memeberValue], function(err) { if (err) return callback(err); else callback(); }); @@ -47,7 +48,9 @@ var TelepatIndexedLists = { */ get: function(listName, indexedProperty, members, callback) { var redisKey = 'blg:til:'+listName+':'+indexedProperty; - var tranzaction = this.redis.multi(); + var tranzaction = Application.redisCacheClient.multi(); + + tranzaction.exists([redisKey]); members.forEach(function(member) { tranzaction.sismember([redisKey, member]); @@ -58,6 +61,11 @@ var TelepatIndexedLists = { var memebershipResults = {}; + if (!results[0]) + return callback(new TelepatError(TelepatError.errors.TilNotFound, [listName])); + + results.splice(0, 1); + async.forEachOf(results, function(result, index, c) { memebershipResults[members[index]] = new Boolean(result); c(); @@ -76,11 +84,15 @@ var TelepatIndexedLists = { var keyPattern = 'blg:til:'+listName+':*'; var self = this; - utils.scanRedisKeysPattern(keyPattern, this.redis, function(err, results) { + utils.scanRedisKeysPattern(keyPattern, Application.redisCacheClient, function(err, results) { if (err) return callback(err); - self.redis.del(results, function(err, removed) { + if (!results.length) + return callback(new TelepatError(TelepatError.errors.TilNotFound, [listName])); + + Application.redisCacheClient.del(results, function(err, removed) { if (err) return callback(err); + callback(null, new Boolean(removed)); }); }) @@ -95,14 +107,20 @@ var TelepatIndexedLists = { */ removeMember: function(listName, indexedProperty, memeber, callback) { var redisKey = 'blg:til:'+listName+':'+indexedProperty; + var tranzaction = Application.redisCacheClient.multi(); - this.redis.srem([redisKey, memeber], function(err, removed) { + tranzaction.exists([redisKey]); + tranzaction.srem([redisKey, memeber]); + + tranzaction.exec(function(err, replies) { if (err) return callback(err); - callback(null, new Boolean(removed)); + + if (!replies[0]) + return callback(new TelepatError(TelepatError.errors.TilNotFound, [listName])); + + callback(null, new Boolean(replies[1])); }); } }; -TelepatIndexedLists.redis = Application.redisCacheClient; - -module.exports = TelepatIndexedLists; +module.exports = TelepatIndexedList;