/standardControllers.js Secret
Created
February 20, 2012 16:27
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* This module provides the default controllers for | |
* all modules. This allows us to easily modify how | |
* many modules query for their data in one fell | |
* swoop. | |
* | |
* However, this does not impact on flexibility, as | |
* it is the module itsself that chooses to use a | |
* standard controller from this list, and so if | |
* necessary it can use its own when needed | |
* (report data is a significant example). | |
* | |
*/ | |
module.exports = { | |
'flow': null, | |
'init': function(par){ | |
this.flow = par; | |
return this; | |
}, | |
'list': function(req,res){ | |
var distinct = false; | |
var query = this.flow.model.find({}); | |
if(req.params.offset !== null){ | |
query.skip(req.params.offset); | |
} | |
if(req.params.limits !== null){ | |
query.limit(req.params.limits); | |
} | |
if(req.params.sorters !== null){ | |
req.params.sorters.forEach(function(val,index,array){ | |
if(val[1] === 'asc' || val[1] === 'desc'){ | |
if(val[1] === 'asc'){ val[1] = 1; } | |
if(val[1] === 'desc'){ val[1] = -1; } | |
query.sort(val[0],val[1]); | |
} else if(val[1] === 'distinct'){ | |
distinct = val[0]; | |
} | |
}); | |
} | |
var responseHandler = function(err,doc){ | |
if(err){ | |
res.json(err); | |
} else { | |
res.json(doc); | |
} | |
}; | |
if(distinct !== false){ | |
query.distinct(distinct,responseHandler); | |
} else { | |
query.run(responseHandler); | |
} | |
}, | |
'show': function(req,res){ | |
this.flow.model.findById(req.params.id,function(err,docs){ | |
if(err){ | |
res.json(err); | |
} else { | |
res.json(docs); | |
} | |
}); | |
}, | |
'create': function(req,res){ | |
var instance = new this.flow.model(req.body); | |
instance.save(function(err){ | |
if(err){ | |
flow.obj.winston.error('DB Error: ' + err); | |
res.json(err); | |
} else { | |
flow.obj.pubsub.emit('newActivity',instance); | |
res.json(instance); | |
} | |
}); | |
}, | |
'update': function(req,res){ | |
this.flow.model.findById(req.params.id, function(err,doc){ | |
Object.keys(req.body).forEach(function(key,index,array){ | |
doc[key] = req.body[key]; | |
}); | |
doc.save(function(err){ | |
if(err){ | |
this.flow.obj.winston.error('DB Error: ' + err); | |
res.json(err); | |
} else { | |
res.json(doc); | |
} | |
}); | |
}); | |
}, | |
'remove': function(req,res){ | |
this.flow.model.findById(req.params.id, function(err,doc){ | |
if(err){ | |
flow.obj.winston.error('DB Error: ' + err); | |
} else if (!doc){ | |
flow.obj.winston.error('DB Error: No matching docs exist.'); | |
res.json({success: false}); | |
} else { | |
doc.remove(function(err){ | |
if(err){ | |
flow.obj.winston.error('DB Error: ' + err); | |
res.json(err); | |
} else { | |
flow.obj.winston.info('Deleted ID ' + req.params.id); | |
res.json({success: true}); | |
} | |
}); | |
} | |
}); | |
}, | |
'search': function(req,res){ | |
var gt = [], | |
lt = [], | |
eq = [], | |
isIn = [], | |
offset = null, | |
limit = null, | |
sort = []; | |
// Where requests. | |
Object.keys(req.body).some(function(key){ | |
if(req.body[key] === null){ | |
return true; | |
} | |
if(req.body[key].indexOf('>') >= 0 ) | |
{ | |
this.flow.obj.winston.info(key + " more than: " + req.body[key]); | |
gt.push({ 'key': key, 'value': req.body[key].replace('>','').trim() }); | |
} | |
else if (req.body[key].indexOf('<') >= 0 ) | |
{ | |
this.flow.obj.winston.info(key + " less than: " + req.body[key]); | |
lt.push({ 'key': key, 'value': req.body[key].replace('<','').trim() }); | |
} | |
else if (req.body[key].indexOf('[') >= 0 && req.body[key].indexOf(']') >= 0 ) | |
{ | |
this.flow.obj.winston.info(key + " is in: " + req.body[key]); | |
isIn.push({ 'key': key, 'value': req.body[key].replace('[','').replace(']','').trim().split(',') }); | |
} | |
else | |
{ | |
this.flow.obj.winston.info(key + " equal to: " + req.body[key]); | |
eq.push({ 'key': key, 'value': req.body[key].trim() }); | |
} | |
}); | |
// Sort, Offset & limit | |
offset = req.params.offset; | |
limit = req.params.limit; | |
sort = req.params.sorters; | |
var query = this.flow.model.find({}); | |
gt.forEach(function(val,index,array){ | |
query.$gt(val.key,val.value); | |
}); | |
lt.forEach(function(val,index,array){ | |
query.$lt(val.key,val.value); | |
}); | |
eq.forEach(function(val,index,array){ | |
query.where(val.key,val.value); | |
}); | |
isIn.forEach(function(val,index,array){ | |
query.where(val.key).in(val.value); | |
}); | |
if(offset){ | |
query.skip(offset); | |
} | |
if(limit){ | |
query.limit(limit); | |
} | |
if(sort){ | |
sort.forEach(function(val,index,array){ | |
if(val === 'asc'){ val = 1; } | |
if(val === 'desc'){ val = -1; } | |
query.sort(index,val); | |
}); | |
} | |
var flow = this.flow; | |
flow.obj.winston.info("Resultant Query:"); | |
flow.obj.winston.info(flow.obj.inspect(query._conditions)); | |
query.run(function(err,doc){ | |
if(err){ | |
res.json(err); | |
} else { | |
flow.obj.winston.info('Result:'); | |
flow.obj.winston.info(doc); | |
flow.obj.winston.info('---'); | |
res.json(doc); | |
} | |
}); | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment