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

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

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

9
module.exports = (
andreagerino's avatar
andreagerino committed
10

11
    function(mongoUrl){
andreagerino's avatar
andreagerino committed
12

13 14
        var icarusDb;
        var router = express.Router();
andreagerino's avatar
andreagerino committed
15

16
        mongoClient.connect(mongoUrl , function(err, db) {
andreagerino's avatar
andreagerino committed
17

18 19
            assert.equal(null, err, err);
            debug("Connected to MongoDB database: "+db.databaseName);
andreagerino's avatar
andreagerino committed
20

21
            icarusDb = db;
22

23
        });
andreagerino's avatar
andreagerino committed
24

25
        router.get('/:resource_id', function(req, res, next) {
andreagerino's avatar
andreagerino committed
26

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

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

32
                if(err!=null){
andreagerino's avatar
andreagerino committed
33

34 35
                    debug(err);
                    next(err);
andreagerino's avatar
andreagerino committed
36

37
                }else{
andreagerino's avatar
andreagerino committed
38

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

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

43
                    }else{
andreagerino's avatar
andreagerino committed
44

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

47
                    }
48

49
                }
50

51
            });
andreagerino's avatar
andreagerino committed
52

53
        });
andreagerino's avatar
andreagerino committed
54

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

57 58
            var collection = icarusDb.collection('resources');
            var resource = req.body;
59

60
            if(Object.keys(resource).length>0) {
61

62
                var timestampContainer = getTimestampContainer(resource);
63

64
                if(timestampContainer!=undefined){
65

66
                    preprocessTimestampContainer(timestampContainer);
andreagerino's avatar
andreagerino committed
67

68
                }
andreagerino's avatar
andreagerino committed
69

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

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

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

77
                    } else {
andreagerino's avatar
andreagerino committed
78

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

82
                    }
andreagerino's avatar
andreagerino committed
83

84
                });
andreagerino's avatar
andreagerino committed
85

86
            }else{
andreagerino's avatar
andreagerino committed
87

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

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

94
            }
95

96
        });
97

98
        function getTimestampContainer(arg){
99

100 101
            if(arg['timestamp']!=undefined&&(arg['timestamp']['utc']!=undefined||arg['timestamp']['user']!=undefined))
                return arg['timestamp'];
102

103
            return {};
104

105
        }
106

107
        function preprocessTimestampContainer(container){
108

109 110
            var utcSeconds = container['utc'];
            var userSeconds = container['user'];
111

112 113 114 115
            if(utcSeconds!=undefined){

                var utcMsec = utcSeconds * 1000;
                container['utc'] = new Date(utcMsec);
116

117
            }
118

119
            if(userSeconds!=undefined){
120

121 122
                var userMsec = userSeconds * 1000;
                container['user'] = new Date(userMsec);
123

124
            }
125

126 127 128 129 130 131 132
            container['server'] = new Date();

        }

        return router;

    }
133

134
);