storage.js 2.18 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 6 7 8
var mongoClient = require('mongodb').MongoClient;
var assert = assert = require('assert');

// Connection URL
andreagerino's avatar
andreagerino committed
9
var mongoUrl = 'mongodb://icarus:icarus@localhost:27017/icarus';
andreagerino's avatar
andreagerino committed
10 11 12 13 14 15 16 17 18 19 20 21

//Use connect method to test the connection to the Server
mongoClient.connect(mongoUrl, function(err, db) {

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

    db.close();

});

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

andreagerino's avatar
andreagerino committed
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
    var _id = req.params.resource_id;
    mongoClient.connect(mongoUrl, function(err, db) {

        if(err!=null){

            debug(err);
            next(err);

        }else{

            var collection = db.collection('resources');

            collection.find({"_id":_id}).limit(1).next(function(err, doc){

                if(err!=null){

                    debug(err);
                    next(err);

                }else{

                    if(doc==undefined){

                        res.sendStatus(400);

                    }else{

                        res.send(doc);

                    }

                }

            });

        }

    });
61 62 63

});

andreagerino's avatar
andreagerino committed
64 65 66 67 68 69 70 71 72 73 74 75 76 77
router.post('/', function(req, res, next){

    mongoClient.connect(mongoUrl, function(err, db) {

        if(err!=null){

            debug(err);
            next(err);

        }else{

            var collection = db.collection('resources');
            var resource = req.body;

andreagerino's avatar
andreagerino committed
78
            if(Object.keys(resource).length>0) {
andreagerino's avatar
andreagerino committed
79 80 81 82 83 84 85 86 87 88 89

                collection.insertOne(resource, function (err, r) {

                    if (err != null) {

                        debug(err);
                        next(err);

                    } else {

                        db.close();
andreagerino's avatar
andreagerino committed
90
                        res.statusCode = 201;
andreagerino's avatar
andreagerino committed
91
                        res.send({"_id":r.insertedId});
andreagerino's avatar
andreagerino committed
92 93 94 95 96 97 98 99 100 101

                    }

                });

            }else{

                var error = new Error("You can't store an empty resource!");
                error.status = 400;

andreagerino's avatar
andreagerino committed
102
                debug(error);
andreagerino's avatar
andreagerino committed
103 104 105 106 107 108 109 110 111 112
                next(error);

            }

        }

    });

});

113
module.exports = router;