//revision_rest.js

var dbapi = require('server/dbapi');
module.exports = {
    main : function (app) {
        app.get('/api/revision', function (req, res) {
            dbapi.readAll('Revisions', req.query).done(function (acc) {
                return res.send(acc);
            });
        });
        var reqToRevVal = function (req) {
            var dictValues = {};

            // NOT NULL
            dictValues.nsId = req.body.nsId;
            dictValues.pgId = req.body.pgId;
            dictValues.catPgId = req.body.catPgId;
            dictValues.userId = req.body.userId;
            dictValues.grpId = req.body.grpId;

            if (req.body.crud) {
                dictValues.crud = req.body.crud;
            }
            if (req.body.name) {
                dictValues.name = req.body.name;
            }
            if (req.body.revComment) {
                dictValues.revComment = req.body.revComment;
            }
            if (req.body.textId) {
                dictValues.textId = req.body.textId;
            }
            if (req.body.tmplId) {
                dictValues.tmplId = req.body.tmplId;
            }

            return dictValues;

        }
        app.post('/api/revision', function (req, res) {
            dbapi.create('Revisions', reqToRevVal(req)).done(function (info) {
                return res.send(acc);
            });
        });
        app.get('/api/revision/:id', function (req, res) {
            var id = req.params.id;
            dbapi.read('Revisions', id).done(function (row) {
                return res.send(row);
            });
        });
        // NOTE: updating or deleting history shouldn't really be done

        return app;
    }
};
