Skip to content

Commit

Permalink
get all
Browse files Browse the repository at this point in the history
  • Loading branch information
pallavi2209 committed Feb 27, 2017
1 parent 2b3ac69 commit 713a29a
Show file tree
Hide file tree
Showing 3 changed files with 134 additions and 15 deletions.
107 changes: 105 additions & 2 deletions api/v1/controllers/samples.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ const doPut = require('../helpers/verbs/doPut');
const u = require('../helpers/verbs/utils');
const httpStatus = require('../constants').httpStatus;
const logAuditAPI = require('../../../utils/loggingUtil').logAuditAPI;
const sampleStore = require('../../../cache/sampleStore');
const redisClient = require('../../../cache/redisCache').client.sampleStore;
const constants = sampleStore.constants;
const ZERO = 0;
const ONE = 1;

module.exports = {

Expand All @@ -49,7 +54,61 @@ module.exports = {
* @param {Function} next - The next middleware function in the stack
*/
findSamples(req, res, next) {
doFind(req, res, next, helper);
if (featureToggles.isFeatureEnabled(constants.featureName)) {
const sampleCmds = [];
const aspectCmds = [];
const response = {};

// get all Samples
redisClient.smembersAsync(constants.indexKey.sample)
.then((allSampleKeys) => {
// add to commands to get sample
allSampleKeys.forEach((sampleName) => {
sampleCmds.push(
[constants.commands.hgetall, sampleName.toLowerCase()]
);
});

// get all aspect names
return redisClient.smembersAsync(constants.indexKey.aspect);
})
.then((allAspectKeys) => {
// add to commands to get aspect
allAspectKeys.forEach((aspectName) => {
aspectCmds.push(
[constants.commands.hgetall, aspectName.toLowerCase()]
);
});

// get all samples and aspects
return Promise.all([
redisClient.batch(sampleCmds).execAsync(),
redisClient.batch(aspectCmds).execAsync(),
]);
})
.then((sampleAndAspects) => {
const samples = sampleAndAspects[ZERO];
const aspects = sampleAndAspects[ONE];

samples.forEach((sampleObj) => {
// attach aspect to sample
sampleObj.aspect = aspects.find((aspect) =>
aspect.name === sampleObj.name.split('|')[ONE]
);

// add sample to response
response[sampleObj.name] = u.responsify(
sampleObj, helper, req.method
);
});
})
.then(() => {
res.status(httpStatus.OK).json(response);
})
.catch((err) => u.handleError(next, err, helper.modelName));
} else {
doFind(req, res, next, helper);
}
},

/**
Expand All @@ -62,7 +121,51 @@ module.exports = {
* @param {Function} next - The next middleware function in the stack
*/
getSample(req, res, next) {
doGet(req, res, next, helper);
// debugger;
// if (featureToggles.isFeatureEnabled(constants.featureName)) {
// const sampleCmds = [];
// const aspectCmds = [];

// debugger;
// // get all Samples
// redisClient.smembersAsync(constants.indexKey.sample)
// .then((allSampleNames) => {
// debugger;
// // add to commands to get sample
// allSampleNames.forEach((sampleName) => {
// const sampleKey = toKey(
// constants.objectType.sample,
// sampleName.toLowerCase()
// );
// sampleCmds.push([constants.commands.hgetall, sampleKey]);
// });

// // get all aspect names
// return redisClient.smembersAsync(constants.indexKey.aspect);
// })
// .then((allAspectNames) => {
// debugger;
// // add to commands to get aspect
// allAspectNames.forEach((aspectName) => {
// const aspectKey = toKey(
// constants.objectType.aspect,
// aspectName.toLowerCase()
// );
// aspectCmds.push([constants.commands.hgetall, aspectKey]);
// });

// // get all samples and aspects
// return Promise.all([
// redisClient.batch(sampleCmds).execAsync(),
// redisClient.batch(aspectCmds).execAsync(),
// ]);
// })
// .then((sampleAndAspects) => {
// debugger;
// });
// } else {
doGet(req, res, next, helper);
// }
},

/**
Expand Down
20 changes: 10 additions & 10 deletions api/v1/errorHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,18 +116,18 @@ module.exports = function errorHandler(err, req, res, next) {
}
}

// console.log('\n-----ERROR HANDLER------------------------------------');
// console.log('ERROR STATUS: ', err.status);
// console.log('ERROR RESPONSE: ', errResponse);
// console.log('REQUEST BODY: ', req.body);
// console.log('STACK: ', err.stack);
// console.log('------------------------------------------------------\n');
console.log('\n-----ERROR HANDLER------------------------------------');
console.log('ERROR STATUS: ', err.status);
console.log('ERROR RESPONSE: ', errResponse);
console.log('REQUEST BODY: ', req.body);
console.log('STACK: ', err.stack);
console.log('------------------------------------------------------\n');
res.status(err.status).json(errResponse);
} catch (err2) {
// console.log('\n-----ERROR HANDLER CATCH------------------------------');
// console.log(err2);
// console.log('STACK: ', util.isError(err2) && err2.stack);
// console.log('------------------------------------------------------\n');
console.log('\n-----ERROR HANDLER CATCH------------------------------');
console.log(err2);
console.log('STACK: ', util.isError(err2) && err2.stack);
console.log('------------------------------------------------------\n');
return next;
}
};
22 changes: 19 additions & 3 deletions cache/sampleStore.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ const constants = {
objectType: { aspect: 'aspect', sample: 'sample', subject: 'subject' },
prefix: PFX,
separator: SEP,
commands: {
hgetall: 'hgetall',
hget: 'hget',
smembers: 'smembers',
},
};

/**
Expand Down Expand Up @@ -81,12 +86,21 @@ function eradicate() {
*/
function removeNullsAndStringifyArrays(obj, arrayFields) {
Object.keys(obj).forEach((key) => {
if (arrayFields.includes(key)) {
obj[key] = JSON.stringify(obj[key]);
} else if (obj[key] === null) {
// delete null fields and empty arrays, then stringify array fields
if (obj[key] === null ||
(Array.isArray(obj[key]) && obj[key].length === 0)) {
delete obj[key];
} else if (arrayFields.includes(key)) {
obj[key] = JSON.stringify(obj[key]);
}
});
// Object.keys(obj).forEach((key) => {
// if (arrayFields.includes(key)) {
// obj[key] = JSON.stringify(obj[key]);
// } else if (obj[key] === null) {
// delete obj[key];
// }
// });
return obj;
} // removeNullsAndStringifyArrays

Expand All @@ -98,6 +112,7 @@ function removeNullsAndStringifyArrays(obj, arrayFields) {
*/
function cleanAspect(a) {
let retval = a.get();

retval = removeNullsAndStringifyArrays(retval,
constants.fieldsToStringify.aspect);
return retval;
Expand All @@ -111,6 +126,7 @@ function cleanAspect(a) {
*/
function cleanSample(s) {
let retval = s.get();

delete retval.aspect;
retval = removeNullsAndStringifyArrays(retval,
constants.fieldsToStringify.sample);
Expand Down

0 comments on commit 713a29a

Please sign in to comment.