Skip to content

Commit

Permalink
* added 404 errors if indexed list doesn't exist
Browse files Browse the repository at this point in the history
  • Loading branch information
Razvan Botea committed Apr 14, 2016
1 parent c76c3a9 commit 758d91e
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 10 deletions.
2 changes: 2 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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('_');

Expand Down
5 changes: 5 additions & 0 deletions lib/TelepatError.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}

Expand Down
38 changes: 28 additions & 10 deletions lib/TelepatIndexedLists.js
Original file line number Diff line number Diff line change
@@ -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]
Expand Down Expand Up @@ -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();
});
Expand All @@ -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]);
Expand All @@ -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();
Expand All @@ -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));
});
})
Expand All @@ -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;

0 comments on commit 758d91e

Please sign in to comment.