-
Notifications
You must be signed in to change notification settings - Fork 0
/
routes.js
96 lines (81 loc) · 3.04 KB
/
routes.js
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
var express = require('express');
var router = express.Router();
const bodyParser = require('body-parser');
var mongoose = require('mongoose'),
Schema = mongoose.Schema;
mongoose.set('useFindAndModify', false); // Options
// Request parser
router.use(bodyParser.urlencoded({extended: true}));
router.use(bodyParser.json());
// API documentation
const YAML = require('yamljs');
const swaggerUi = require('swagger-ui-express');
const swaggerDocument = YAML.load('./swagger.yaml');
router.use('/api-docs', swaggerUi.serve);
router.get('/api-docs', swaggerUi.setup(swaggerDocument));
// Sensor dataschema
var SensorSchema = new Schema({
name: { type: String },
value: { type: String },
status: { type: String }
});
mongoose.model('Sensor', SensorSchema);
var Sensor = require('mongoose').model('Sensor');
// Sensor
// 1 - Create sensor
router.post('/sensor', function (req, res, next) {
var sensor = new Sensor(req.body);
sensor.save(function (err) {
if (!sensor || err) { // Sensor not been created or invalid request
res.status(400).json({message: 'Invalid request'});
} else {
res.status(201).json({message: 'Sensor successfully created', id: sensor.id});
}
});
});
// 2 - Get sensor data
router.get('/sensor/:sensor_id', function (req, res, next) {
Sensor.findById(req.params.sensor_id, function (err, sensor) {
if (!sensor || err) { // Sensor not found or invalid request
res.status(400).json({message: 'Invalid request or sensor id'});
} else {
res.status(200).json({id: sensor._id, name: sensor.name, value: sensor.value, status: sensor.status});
}
});
});
// 3 - Update sensor data
router.put('/sensor/:sensor_id', function (req, res, next) {
Sensor.findByIdAndUpdate(req.params.sensor_id, req.body, function (err, sensor) {
if (!sensor || err) { // Sensor not found or invalid request
res.status(400).json({message: 'Invalid request or sensor id'});
} else if (!req.body.name && !req.body.value && !req.body.status) { // Data or header missing --> no data received
res.status(400).json({message: 'No data received to update'});
} else {
res.status(200).json({message: 'Sensor successfully updated', id: sensor._id, name: req.body.name, value: req.body.value, status: req.body.status});
}
});
});
// 4 - Delete sensor
router.delete('/sensor/:sensor_id', function (req, res, next) {
Sensor.findByIdAndDelete(req.params.sensor_id, function (err, sensor) {
if (!sensor || err) { // Sensor not found or invalid request
res.status(400).json({message: 'Invalid request or sensor id'});
} else {
res.status(200).json({message: 'Sensor successfully deleted', id: sensor._id});
}
});
});
// 5 - Any other POST/GET/PUT/DELETE request
router.post('*', function (req, res) {
res.status(404).json({message: 'Invalid URL'});
});
router.get('*', function (req, res) {
res.status(404).json({message: 'Invalid URL'});
});
router.put('*', function (req, res) {
res.status(404).json({message: 'Invalid URL'});
});
router.delete('*', function (req, res) {
res.status(404).json({message: 'Invalid URL'});
});
module.exports = router;