diff --git a/routes/storage.js b/routes/storage.js index 32b886529c97ed3beb6c45e00d066f9cbbc8926c..ec2c8217c392b827b7c960360ce424b4c1ed9892 100644 --- a/routes/storage.js +++ b/routes/storage.js @@ -8,7 +8,7 @@ var ObjectID = require('mongodb').ObjectID; var assert = assert = require('assert'); // Connection URL -var mongoUrl = 'mongodb://icarus:icarus@localhost:27017/icarus'; +var mongoUrl = 'mongodb://icarus:icarus@webdev.ewlab.di.unimi.it:27017/icarus'; //Connect to the server and intialise the pool var icarusDb; @@ -58,6 +58,14 @@ router.post('/', function(req, res, next){ if(Object.keys(resource).length>0) { + var timestampContainer = getTimestampContainer(resource); + + if(timestampContainer!=undefined){ + + preprocessTimestampContainer(timestampContainer); + + } + collection.insertOne(resource, function (err, r) { if (err != null) { @@ -86,4 +94,34 @@ router.post('/', function(req, res, next){ }); +function getTimestampContainer(arg){ + + if(arg['timestamp']!=undefined&&(arg['timestamp']['utc']!=undefined)||arg['timestamp']['user']!=undefined) + return arg['timestamp']; + + return undefined; + +} + +function preprocessTimestampContainer(container){ + + var utcSeconds = container['utc']; + var userSeconds = container['user']; + + if(utcSeconds!=undefined){ + + var utcMsec = utcSeconds * 1000; + container['utc'] = new Date(utcMsec); + + } + + if(userSeconds!=undefined){ + + var userMsec = userSeconds * 1000; + container['user'] = new Date(userMsec); + + } + +} + module.exports = router;