2019-02-19 19:21:19 +01:00
|
|
|
const Device = require('../models/deviceModel');
|
2019-02-20 11:09:57 +01:00
|
|
|
const logger = require('../logger');
|
2019-02-19 19:21:19 +01:00
|
|
|
|
|
|
|
function index(req, res) {
|
2019-02-20 09:39:54 +01:00
|
|
|
if (req.query.name) {
|
|
|
|
Device.findOne({ name: req.query.name }, (err, device) => {
|
|
|
|
if (!device) {
|
2019-02-20 11:09:57 +01:00
|
|
|
logger.logError(`index: ${err}`);
|
|
|
|
return res.status(404).send();
|
2019-02-20 09:39:54 +01:00
|
|
|
}
|
2019-02-20 11:09:57 +01:00
|
|
|
return res.json(device);
|
2019-02-20 09:39:54 +01:00
|
|
|
});
|
|
|
|
} else {
|
2019-02-22 10:09:38 +01:00
|
|
|
Device.find({}, (err, devices) => {
|
2019-02-20 09:39:54 +01:00
|
|
|
if (err) {
|
2019-02-20 11:09:57 +01:00
|
|
|
logger.logError(`index: ${err}`);
|
|
|
|
return res.status(500).send();
|
2019-02-20 09:39:54 +01:00
|
|
|
}
|
2019-02-22 10:09:38 +01:00
|
|
|
|
|
|
|
if (devices.length === 0) {
|
|
|
|
return res.status(404).send();
|
|
|
|
}
|
2019-02-20 11:09:57 +01:00
|
|
|
return res.json(devices);
|
2019-02-20 09:39:54 +01:00
|
|
|
});
|
|
|
|
}
|
2019-02-19 19:21:19 +01:00
|
|
|
}
|
|
|
|
|
2019-02-19 19:32:28 +01:00
|
|
|
function view(req, res) {
|
|
|
|
Device.findById(req.params.deviceId)
|
2019-02-20 11:09:57 +01:00
|
|
|
.then(device => res.json(device))
|
2019-02-19 19:32:28 +01:00
|
|
|
.catch((err) => {
|
2019-02-20 11:09:57 +01:00
|
|
|
logger.logError(`view: ${err}`);
|
|
|
|
return res.status(500).send();
|
2019-02-19 19:32:28 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-02-20 08:29:32 +01:00
|
|
|
function update(req, res) {
|
|
|
|
Device.findOneAndUpdate({ _id: req.params.deviceId }, req.body, (err) => {
|
|
|
|
if (err) {
|
2019-02-20 11:09:57 +01:00
|
|
|
logger.logError(`update: ${err}`);
|
|
|
|
return res.status(500).send();
|
2019-02-20 08:29:32 +01:00
|
|
|
}
|
2019-02-20 11:09:57 +01:00
|
|
|
return res.status(200).send();
|
2019-02-20 08:29:32 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-02-20 09:39:54 +01:00
|
|
|
function add(req, res) {
|
|
|
|
const device = new Device(req.body);
|
|
|
|
device
|
|
|
|
.save()
|
2019-02-20 11:09:57 +01:00
|
|
|
.then(item => res.status(201).json(item))
|
2019-02-20 09:39:54 +01:00
|
|
|
.catch((err) => {
|
2019-02-20 11:09:57 +01:00
|
|
|
logger.logError(`add: ${err}`);
|
|
|
|
return res.status(500).send();
|
2019-02-20 09:39:54 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-02-20 08:54:48 +01:00
|
|
|
function remove(req, res) {
|
|
|
|
Device.deleteOne({ _id: req.params.deviceId }, (err) => {
|
|
|
|
if (err) {
|
2019-02-20 11:09:57 +01:00
|
|
|
logger.logError(`delete: ${err}`);
|
|
|
|
return res.status(500).send();
|
2019-02-20 08:54:48 +01:00
|
|
|
}
|
2019-02-20 11:09:57 +01:00
|
|
|
return res.status(200).send();
|
2019-02-20 08:54:48 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-02-19 19:21:19 +01:00
|
|
|
module.exports.index = index;
|
2019-02-19 19:32:28 +01:00
|
|
|
module.exports.view = view;
|
2019-02-20 08:29:32 +01:00
|
|
|
module.exports.update = update;
|
2019-02-20 09:39:54 +01:00
|
|
|
module.exports.add = add;
|
2019-02-20 08:54:48 +01:00
|
|
|
module.exports.remove = remove;
|