secret
Created

  • Download Gist
standardControllers.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214
/**
* 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);
}
});
 
}
};

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.