From 9d8004ee4cf04ea2554fa8d91923a3824f91f0da Mon Sep 17 00:00:00 2001 From: Drasko DRASKOVIC Date: Wed, 23 Sep 2015 22:45:15 +0200 Subject: [PATCH] Add device schema mock-up --- app/models/device.js | 123 ++++++++++++++++++++++++++++++++++++++++++ app/models/thing.js | 10 ---- app/routes/devices.js | 85 +++++++++++++++++++++++++++++ app/routes/things.js | 85 ----------------------------- server.js | 2 +- 5 files changed, 209 insertions(+), 96 deletions(-) create mode 100644 app/models/device.js delete mode 100644 app/models/thing.js create mode 100644 app/routes/devices.js delete mode 100644 app/routes/things.js diff --git a/app/models/device.js b/app/models/device.js new file mode 100644 index 00000000..7f9b607d --- /dev/null +++ b/app/models/device.js @@ -0,0 +1,123 @@ +/** + * Dependencies + */ +var mongoose = require('mongoose'); + + +/** + * Private variables and functions + */ +var Schema = mongoose.Schema; + + +/** + * Exports + */ +var DeviceSchema = new Schema({ + name: { + type: String, + required: true + }, + description: { + type: String, + required: false + }, + creator: { + type: String, + required: true, + }, + owner: { + type: String, + required: true, + }, + group: { + type: Array, + default: [] + }, + deviceId: { + type: String, + required: true, + index: true, + match: /^[0-9a-f]{10}$/ + }, + apiKey: { + type: String, + required: true, + index: true + }, + createdAt: { + type: Date, + index: true, + default: Date.now + }, + isPublic: { + type: Boolean, + index: true, + default: false + }, + online: { + type: Boolean, + index: true, + default: false + }, + lastSeen: { + type: Date + }, + updatedAt: { + type: Date + }, + manufacturerId: { + type: String, + required: false, + index: true, + match: /^[0-9a-f]{10}$/ + }, + serialNumber: { + type: String, + required: false, + index: true, + match: /^[0-9a-f]{10}$/ + }, + productId: { + type: String, + required: false, + index: true, + match: /^[0-9a-f]{10}$/ + }, + activationCode: { + type: String, + required: false, + index: true, + match: /^[0-9a-f]{10}$/ + }, + deviceLocation: { + type: String, + required: false, + index: true, + match: /^[0-9a-f]{10}$/ + }, + firmwareVersion: { + type: String, + required: false, + index: true, + match: /^[0-9a-f]{10}$/ + } + +}); + + +DeviceSchema.static('exists', function (apikey, deviceid, callback) { + this.where({ apiKey: apikey, deviceId: deviceid }).findOne(callback); +}); + +DeviceSchema.static('getDeviceByDeviceId', function (deviceid, callback) { + this.where({ deviceId: deviceid }).findOne(callback); +}); + +DeviceSchema.static('getDevicesByApikey', function (apikey, callback) { + this.where('apiKey', apikey).find(callback); +}); + + +module.exports = mongoose.model('Device', DeviceSchema); + diff --git a/app/models/thing.js b/app/models/thing.js deleted file mode 100644 index 1cfbec64..00000000 --- a/app/models/thing.js +++ /dev/null @@ -1,10 +0,0 @@ -// app/models/thing.js - -var mongoose = require('mongoose'); -var Schema = mongoose.Schema; - -var ThingSchema = new Schema({ - name: String -}); - -module.exports = mongoose.model('Thing', ThingSchema); diff --git a/app/routes/devices.js b/app/routes/devices.js new file mode 100644 index 00000000..8af653a5 --- /dev/null +++ b/app/routes/devices.js @@ -0,0 +1,85 @@ +var express = require('express'); +var router = express.Router(); // get an instance of the express Router + +var Device = require('../models/device'); + +// on routes that end in /devices +// ---------------------------------------------------- +router.route('/') + + // create a devices (accessed at POST http://localhost:8080/devices) + .post(function(req, res) { + + var device = new Device(); // create a new instance of the Bear model + device.name = req.body.name; // set the device's name (comes from the request) + + // save the device and check for errors + device.save(function(err) { + if (err) + res.send(err); + + res.json({ message: 'Device created!' }); + }); + + }) + + // get all the devices (accessed at GET http://localhost:8080/devices) + .get(function(req, res) { + Device.find(function(err, devices) { + if (err) + res.send(err); + + res.json(devices); + }); + }); + + +// on routes that end in /devices/:device_id +// ---------------------------------------------------- +router.route('/:device_id') + + // get the device with that id (accessed at GET http://localhost:8080/devices/:device_id) + .get(function(req, res) { + Device.findById(req.params.device_id, function(err, device) { + if (err) + res.send(err); + res.json(device); + }); + }) + + // update the device with this id (accessed at PUT http://localhost:8080/devices/:device_id) + .put(function(req, res) { + + // use our device model to find the device we want + Device.findById(req.params.device_id, function(err, device) { + + if (err) + res.send(err); + + device.name = req.body.name; // update the devices info + + // save the device + device.save(function(err) { + if (err) + res.send(err); + + res.json({ message: 'Device updated!' }); + }); + + }) + }) + + // delete the device with this id (accessed at DELETE http://localhost:8080/devices/:device_id) + .delete(function(req, res) { + Device.remove({ + _id: req.params.device_id + }, function(err, device) { + if (err) + res.send(err); + + res.json({ message: 'Successfully deleted' }); + }); + }); + +// export router module +module.exports = router; diff --git a/app/routes/things.js b/app/routes/things.js deleted file mode 100644 index f3f41816..00000000 --- a/app/routes/things.js +++ /dev/null @@ -1,85 +0,0 @@ -var express = require('express'); -var router = express.Router(); // get an instance of the express Router - -var Thing = require('../models/thing'); - -// on routes that end in /things -// ---------------------------------------------------- -router.route('/') - - // create a things (accessed at POST http://localhost:8080/things) - .post(function(req, res) { - - var thing = new Thing(); // create a new instance of the Bear model - thing.name = req.body.name; // set the thing's name (comes from the request) - - // save the thing and check for errors - thing.save(function(err) { - if (err) - res.send(err); - - res.json({ message: 'Thing created!' }); - }); - - }) - - // get all the things (accessed at GET http://localhost:8080/things) - .get(function(req, res) { - Thing.find(function(err, things) { - if (err) - res.send(err); - - res.json(things); - }); - }); - - -// on routes that end in /things/:thing_id -// ---------------------------------------------------- -router.route('/:thing_id') - - // get the thing with that id (accessed at GET http://localhost:8080/things/:thing_id) - .get(function(req, res) { - Thing.findById(req.params.thing_id, function(err, thing) { - if (err) - res.send(err); - res.json(thing); - }); - }) - - // update the thing with this id (accessed at PUT http://localhost:8080/things/:thing_id) - .put(function(req, res) { - - // use our thing model to find the thing we want - Thing.findById(req.params.thing_id, function(err, thing) { - - if (err) - res.send(err); - - thing.name = req.body.name; // update the things info - - // save the thing - thing.save(function(err) { - if (err) - res.send(err); - - res.json({ message: 'Thing updated!' }); - }); - - }) - }) - - // delete the thing with this id (accessed at DELETE http://localhost:8080/things/:thing_id) - .delete(function(req, res) { - Thing.remove({ - _id: req.params.thing_id - }, function(err, thing) { - if (err) - res.send(err); - - res.json({ message: 'Successfully deleted' }); - }); - }); - -// export router module -module.exports = router; diff --git a/server.js b/server.js index 29155d6b..8c60e330 100644 --- a/server.js +++ b/server.js @@ -33,7 +33,7 @@ var port = process.env.PORT || config.port; // set our port // ROUTES FOR OUR API // ============================================================================= app.use('/status', require('./app/routes/status')); -app.use('/things', require('./app/routes/things')); +app.use('/devices', require('./app/routes/devices')); // START THE SERVER