storage.js 2.35 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
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
    mongoClient.connect(mongoUrl, function(err, db) {

        if(err!=null){

            debug(err);
            next(err);

andreagerino's avatar
andreagerino committed
32 33
            db.close();

andreagerino's avatar
andreagerino committed
34 35 36 37
        }else{

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

andreagerino's avatar
andreagerino committed
38
            var _id = new ObjectID(req.params.resource_id);
andreagerino's avatar
andreagerino committed
39 40 41 42 43 44 45 46 47 48 49
            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
50
                        res.sendStatus(404);
andreagerino's avatar
andreagerino committed
51 52 53 54 55 56 57 58 59

                    }else{

                        res.send(doc);

                    }

                }

andreagerino's avatar
andreagerino committed
60 61
                db.close();

andreagerino's avatar
andreagerino committed
62 63 64 65 66
            });

        }

    });
67 68 69

});

andreagerino's avatar
andreagerino committed
70 71 72 73 74 75 76 77 78
router.post('/', function(req, res, next){

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

        if(err!=null){

            debug(err);
            next(err);

andreagerino's avatar
andreagerino committed
79 80
            db.close();

andreagerino's avatar
andreagerino committed
81 82 83 84 85
        }else{

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

andreagerino's avatar
andreagerino committed
86
            if(Object.keys(resource).length>0) {
andreagerino's avatar
andreagerino committed
87 88 89 90 91 92 93 94 95 96 97

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

                    if (err != null) {

                        debug(err);
                        next(err);

                    } else {

                        db.close();
andreagerino's avatar
andreagerino committed
98
                        res.statusCode = 201;
andreagerino's avatar
andreagerino committed
99
                        res.send({"_id":r.insertedId});
andreagerino's avatar
andreagerino committed
100 101 102 103 104 105 106 107 108 109

                    }

                });

            }else{

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

andreagerino's avatar
andreagerino committed
110
                debug(error);
andreagerino's avatar
andreagerino committed
111 112
                next(error);

andreagerino's avatar
andreagerino committed
113 114
                db.close();

andreagerino's avatar
andreagerino committed
115 116 117 118 119 120 121 122
            }

        }

    });

});

123
module.exports = router;