var fs = require('fs-extra');
var extend = require('extend');

var mongo = require('./mongo');

var dataPath = 'source/data/data.json';

function getDataJSON() {
    return fs.readFileSync(dataPath, 'utf8');
}
// console.log(getDataJSON());
exports.put = function(req, res) {
    var data = JSON.parse(req.body.data);
    var target = JSON.parse(getDataJSON());

    extend(true, target, data);

    var extended = JSON.stringify(target);

    fs.writeFileSync(dataPath, extended);
    res.writeHead(200);
    res.end('success');
}


exports.get = function(req, res) {

    mongo.get('omoss', function(value){
        var data = getDataJSON();

        res.writeHead(200, {
            "Content-Type": "application/json"
        });
        res.end(value + data);
    })
    
}