storage.js 1.66 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

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

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

andreagerino's avatar
andreagerino committed
63
            if (err != null) {
andreagerino's avatar
andreagerino committed
64

andreagerino's avatar
andreagerino committed
65 66
                debug(err);
                next(err);
andreagerino's avatar
andreagerino committed
67

andreagerino's avatar
andreagerino committed
68
            } else {
andreagerino's avatar
andreagerino committed
69

andreagerino's avatar
andreagerino committed
70 71
                res.statusCode = 201;
                res.send({"_id":r.insertedId});
andreagerino's avatar
andreagerino committed
72

andreagerino's avatar
andreagerino committed
73
            }
andreagerino's avatar
andreagerino committed
74

andreagerino's avatar
andreagerino committed
75
        });
andreagerino's avatar
andreagerino committed
76

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

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

andreagerino's avatar
andreagerino committed
82 83
        debug(error);
        next(error);
andreagerino's avatar
andreagerino committed
84

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

andreagerino's avatar
andreagerino committed
87
});
andreagerino's avatar
andreagerino committed
88

89
module.exports = router;