storage.js 2.47 KB
Newer Older
1 2
var express = require('express');
var router = express.Router();
andreagerino's avatar
andreagerino committed
3
var debug = require('debug')('Icarus:server');
4

andreagerino's avatar
andreagerino committed
5
var mongoClient = require('mongodb').MongoClient;
andreagerino's avatar
andreagerino committed
6
var ObjectID = require('mongodb').ObjectID;
andreagerino's avatar
andreagerino committed
7

andreagerino's avatar
andreagerino committed
8 9 10
var assert = assert = require('assert');

// Connection URL
11
var mongoUrl = 'mongodb://icarus:icarus@localhost:27017/icarus';
andreagerino's avatar
andreagerino committed
12

andreagerino's avatar
andreagerino committed
13
//Connect to the server and intialise the pool
andreagerino's avatar
andreagerino committed
14
var icarusDb;
andreagerino's avatar
andreagerino committed
15 16 17 18 19
mongoClient.connect(mongoUrl, function(err, db) {

    assert.equal(null, err, err);
    debug("Connected to MongoDB database: "+db.databaseName);

andreagerino's avatar
andreagerino committed
20
    icarusDb = db;
andreagerino's avatar
andreagerino committed
21 22 23 24

});

router.get('/:resource_id', function(req, res, next) {
25

andreagerino's avatar
andreagerino committed
26 27 28 29
    var collection = icarusDb.collection('resources');

    var _id = new ObjectID(req.params.resource_id);
    collection.find({"_id":_id}).limit(1).next(function(err, doc){
andreagerino's avatar
andreagerino committed
30 31 32 33 34 35 36 37

        if(err!=null){

            debug(err);
            next(err);

        }else{

andreagerino's avatar
andreagerino committed
38
            if(doc==undefined){
andreagerino's avatar
andreagerino committed
39

andreagerino's avatar
andreagerino committed
40
                res.sendStatus(404);
andreagerino's avatar
andreagerino committed
41

andreagerino's avatar
andreagerino committed
42
            }else{
andreagerino's avatar
andreagerino committed
43

andreagerino's avatar
andreagerino committed
44
                res.send(doc);
andreagerino's avatar
andreagerino committed
45

andreagerino's avatar
andreagerino committed
46
            }
andreagerino's avatar
andreagerino committed
47 48 49 50

        }

    });
51 52 53

});

andreagerino's avatar
andreagerino committed
54 55
router.post('/', function(req, res, next){

andreagerino's avatar
andreagerino committed
56 57
    var collection = icarusDb.collection('resources');
    var resource = req.body;
andreagerino's avatar
andreagerino committed
58

andreagerino's avatar
andreagerino committed
59
    if(Object.keys(resource).length>0) {
andreagerino's avatar
andreagerino committed
60

61 62 63 64 65 66 67 68
        var timestampContainer = getTimestampContainer(resource);

        if(timestampContainer!=undefined){

            preprocessTimestampContainer(timestampContainer);

        }

andreagerino's avatar
andreagerino committed
69
        collection.insertOne(resource, function (err, r) {
andreagerino's avatar
andreagerino committed
70

andreagerino's avatar
andreagerino committed
71
            if (err != null) {
andreagerino's avatar
andreagerino committed
72

andreagerino's avatar
andreagerino committed
73 74
                debug(err);
                next(err);
andreagerino's avatar
andreagerino committed
75

andreagerino's avatar
andreagerino committed
76
            } else {
andreagerino's avatar
andreagerino committed
77

andreagerino's avatar
andreagerino committed
78 79
                res.statusCode = 201;
                res.send({"_id":r.insertedId});
andreagerino's avatar
andreagerino committed
80

andreagerino's avatar
andreagerino committed
81
            }
andreagerino's avatar
andreagerino committed
82

andreagerino's avatar
andreagerino committed
83
        });
andreagerino's avatar
andreagerino committed
84

andreagerino's avatar
andreagerino committed
85
    }else{
andreagerino's avatar
andreagerino committed
86

andreagerino's avatar
andreagerino committed
87 88
        var error = new Error("You can't store an empty resource!");
        error.status = 400;
andreagerino's avatar
andreagerino committed
89

andreagerino's avatar
andreagerino committed
90 91
        debug(error);
        next(error);
andreagerino's avatar
andreagerino committed
92

andreagerino's avatar
andreagerino committed
93
    }
andreagerino's avatar
andreagerino committed
94

andreagerino's avatar
andreagerino committed
95
});
andreagerino's avatar
andreagerino committed
96

97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
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);

    }

125 126
    container['server'] = new Date();

127 128
}

129
module.exports = router;