storage.js 2.24 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 7
var objectID = mongoClient.objectId;

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

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

//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) {
24

andreagerino's avatar
andreagerino committed
25 26 27 28 29 30 31 32 33 34 35
    mongoClient.connect(mongoUrl, function(err, db) {

        if(err!=null){

            debug(err);
            next(err);

        }else{

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

andreagerino's avatar
andreagerino committed
36
            var _id = new objectID(req.params.resource_id);
andreagerino's avatar
andreagerino committed
37 38 39 40 41 42 43 44 45 46 47
            collection.find({"_id":_id}).limit(1).next(function(err, doc){

                if(err!=null){

                    debug(err);
                    next(err);

                }else{

                    if(doc==undefined){

andreagerino's avatar
andreagerino committed
48
                        res.sendStatus(404);
andreagerino's avatar
andreagerino committed
49 50 51 52 53 54 55 56 57 58 59 60 61 62

                    }else{

                        res.send(doc);

                    }

                }

            });

        }

    });
63 64 65

});

andreagerino's avatar
andreagerino committed
66 67 68 69 70 71 72 73 74 75 76 77 78 79
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
80
            if(Object.keys(resource).length>0) {
andreagerino's avatar
andreagerino committed
81 82 83 84 85 86 87 88 89 90 91

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

                    if (err != null) {

                        debug(err);
                        next(err);

                    } else {

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

                    }

                });

            }else{

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

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

            }

        }

    });

});

115
module.exports = router;