2015-04-12 08:42:47 +02:00
|
|
|
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
var _ = require('lodash');
|
|
|
|
var Comparison = require('./comparison.model');
|
|
|
|
var wdiff = require('../../components/wdiff');
|
2015-04-18 01:53:16 +02:00
|
|
|
var jf = require('jsonfile');
|
|
|
|
var fs = require('fs');
|
|
|
|
|
2015-04-12 08:42:47 +02:00
|
|
|
|
|
|
|
|
|
|
|
//return the comparison given an id, if it exsits
|
|
|
|
exports.showComparison = function showComparison(req, res) {
|
2015-04-18 01:53:16 +02:00
|
|
|
//generate a filename
|
|
|
|
var filename = fnComparison(req.params.id);
|
|
|
|
|
|
|
|
//check if that file exists
|
|
|
|
fs.exists(filename, function (exists) {
|
|
|
|
//if the file does not exist, return a 404
|
|
|
|
if (!exists) return res.send(404);
|
|
|
|
|
|
|
|
//otherwise, read the file as JSON
|
|
|
|
jf.readFile(filename, function(err, comparison) {
|
|
|
|
if(err) { return handleError(res, err); }
|
|
|
|
|
|
|
|
//and return
|
|
|
|
return res.json(comparison);
|
|
|
|
});
|
2015-04-12 08:42:47 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
//return a markdown wdiff for the comparison given an id, if it exsits
|
|
|
|
exports.wdiffMarkdownComparison = function wdiffMarkdownComparison(req, res) {
|
2015-04-18 01:53:16 +02:00
|
|
|
//generate a filename
|
|
|
|
var filename = fnComparison(req.params.id);
|
2015-04-12 08:42:47 +02:00
|
|
|
|
2015-04-18 01:53:16 +02:00
|
|
|
//check if that file exists
|
|
|
|
fs.exists(filename, function (exists) {
|
|
|
|
//if the file does not exist, return a 404
|
|
|
|
if (!exists) return res.send(404);
|
2015-04-12 08:42:47 +02:00
|
|
|
|
2015-04-18 01:53:16 +02:00
|
|
|
//otherwise, read the file as JSON
|
|
|
|
jf.readFile(filename, function(err, comparison) {
|
|
|
|
if(err) { return handleError(res, err); }
|
|
|
|
|
|
|
|
//now perform a wdiff on the result
|
|
|
|
wdiff(comparison.a,comparison.b, true, function(err, result) {
|
|
|
|
if (err)
|
|
|
|
return handleError(res, err);
|
|
|
|
|
|
|
|
_.merge(result, comparison)
|
|
|
|
return res.json(result);
|
|
|
|
});
|
2015-04-12 08:42:47 +02:00
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// Creates a new comparison
|
2015-04-18 01:53:16 +02:00
|
|
|
exports.create = function create(req, res) {
|
|
|
|
var a = req.body.a;
|
|
|
|
var b = req.body.b;
|
2015-04-12 08:42:47 +02:00
|
|
|
|
2015-04-18 01:53:16 +02:00
|
|
|
//create the comparison
|
|
|
|
var comparison = new Comparison(a,b);
|
2015-04-12 08:42:47 +02:00
|
|
|
|
2015-04-18 01:53:16 +02:00
|
|
|
//look up its filename
|
|
|
|
var filename = fnComparison(comparison._id);
|
2015-04-12 08:42:47 +02:00
|
|
|
|
2015-04-18 01:53:16 +02:00
|
|
|
//and write it to the filesystem
|
|
|
|
jf.writeFile(filename, comparison, function(err) {
|
2015-04-12 08:42:47 +02:00
|
|
|
if(err) { return handleError(res, err); }
|
|
|
|
|
2015-04-18 01:53:16 +02:00
|
|
|
//if successful, return the comparison object
|
|
|
|
return res.json(201, comparison);
|
2015-04-12 08:42:47 +02:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
function handleError(res, err) {
|
2015-04-18 01:53:16 +02:00
|
|
|
console.log(err);
|
2015-04-12 08:42:47 +02:00
|
|
|
return res.send(500, err);
|
|
|
|
}
|
2015-04-18 01:53:16 +02:00
|
|
|
|
|
|
|
|
|
|
|
// returns a filename for the given comparison
|
|
|
|
function fnComparison (id) {
|
|
|
|
return "./data/" + "comp-" + id + ".json";
|
|
|
|
}
|