latest bits

This commit is contained in:
Dobie Wollert
2015-08-03 05:00:22 -04:00
parent 3964ec1321
commit b4e727c0e6
19 changed files with 1054 additions and 699 deletions

View File

@ -6,127 +6,93 @@ var md5 = require('MD5');
var log = require('log4node');
exports.index = function(req, res) {
log.info('devices.index');
var query = Device.find({ deleted: false })
.exec(function(err, results) {
if (err) {
res.json(500, err);
} else {
res.json(results);
}
});
exports.index = function(req, res, next) {
log.info('devices.index');
Device.find({ deleted: false })
.exec(returnResult(res));
};
exports.get = function(req, res, next) {
var id = req.param('device_id');
var id = req.param('device_id');
log.info("devices.get %s", id);
Device.findById(id)
.exec(function(err, device) {
if (err) return next(err);
if (!device) return next(new Error('failed to load device ' + id));
log.info("devices.get %s", id);
res.json(device);
});
};
exports.deviceTypes = function(req, res, next) {
log.info("devices.deviceTypes");
var query = Device.find({ deleted: false })
.select('deviceType')
.exec(function(err, results) {
if (err) {
res.json(500, err);
} else {
res.json(_.uniq(_.pluck(results, 'deviceType')));
}
});
};
exports.makes = function(req, res, next) {
log.info("devices.makes");
var query = Device.find({ deleted: false })
.select('make')
.exec(function(err, results) {
if (err) {
res.json(500, err);
} else {
res.json(_.uniq(_.pluck(results, 'make')));
}
});
};
exports.models = function(req, res, next) {
log.info("devices.models");
var query = Device.find({ deleted: false })
.select('model')
.exec(function(err, results) {
if (err) {
res.json(500, err);
} else {
res.json(_.uniq(_.pluck(results, 'model')));
}
});
Device.findById(id)
.exec(returnResult(res));
};
exports.create = function(req, res, next) {
log.info("devices.create %j", res.body);
log.info("devices.create %j", res.body);
var device = new Device({
deviceType: req.body.deviceType,
make: req.body.make,
model: req.body.model,
technicalData: req.body.technicalData,
links: req.body.links,
partsRecommended: req.body.partsRecommended,
images: req.body.images
});
return device.save(function(err) {
if (err)
log.error("Error: %s", err);
return res.json(device);
});
var device = new Device(req.body);
device.save(returnResult(res));
};
exports.update = function(req, res, next) {
var id = req.param('device_id');
log.info('devices.update %s %j', id, req.body);
var id = req.param('device_id');
log.info('devices.update %s %j', id, req.body);
return Device.findById(id, function(err, device) {
device.deviceType = req.body.deviceType;
device.make = req.body.make;
device.model = req.body.model;
device.technicalData = req.body.technicalData;
device.links = req.body.links;
device.partsRecommended = req.body.partsRecommended;
device.images = req.body.images;
return device.save(function(err) {
if (err)
log.error("Error: %s", err);
return res.json(device);
});
});
Device.findById(id, function(err, device) {
if (err) {
log.error("Error: %s", err);
res.json(500, err);
} else if (!device) {
res.json(404, 'Unknown Device: %s', id);
} else {
_.assign(device, req.body);
device.save(returnResult(res, device));
}
});
};
exports.upload = function(req, res, next) {
var path = req.files.file.path;
exports.isUnique = function(req, res, next) {
var field = req.param('field');
var value = req.param('value');
var key = req.param('key');
fs.readFile(path, function(err, data) {
var hash = md5(data);
if (!field || !value) {
return res.json(400, 'missing field or value');
}
fs.writeFile('/srv/biomed-site/images/devices/' + hash, data, function(err) {
if (err)
log.error("Error: %s", err);
var query = {};
query[field] = value;
return res.json({
filename: hash
});
});
});
if (key) {
query['_id'] = { $ne: key };
}
Device.find(query)
.exec(function(err, result) {
if (err) return next(err);
res.json({
isUnique: result.length === 0
});
});
};
function returnResult(res, result) {
return function(err, data) {
if (err) {
log.error("Error: %s", err);
res.json(500, err);
} else {
if (result) {
res.json(result);
} else {
res.json(data);
}
}
}
}
function mutateResult(res, mutator) {
return function(err, results) {
if (err) {
log.error("Error: %s", err);
res.json(500, err);
} else {
res.json(mutator(results));
}
}
}