/**
 * Created by Mai on 2017/1/18.
 */
var ProjectsData = require('../models/project');

//统一回调函数
var callback = function(req, res, err, message, data){
    res.json({error: err, message: message, data: data});
}

module.exports = {
    checkRight: function (req, res) {
        var data = JSON.parse(req.body.data);
        if (data.user_id) {
            return data.user_id === req.session.userID;
        } else {
            return false;
        }
    },
    getProjects: function(req, res){
        var data = JSON.parse(req.body.data);
        ProjectsData.getUserProjects(data.user_id, function(err, message, projects){
            if (projects) {
                callback(req, res, err, message, projects);
            } else {
                callback(req, res, err, message, null);
            }
        });
    },
    updateProjects: function (req, res) {
        var data = JSON.parse(req.body.data);
        ProjectsData.updateUserProjects(data.user_id, data.updateData, function (err, message, data) {
            if (err === 0) {
                callback(req, res, err, message, data);
            } else {
                callback(req, res, err, message, null);
            }
        });
    },
    copyProjects: function (req, res) {
        var data = JSON.parse(req.body.data);
        ProjectsData.copyUserProjects(data.user_id, data.updateData, function (err, message, data) {
            if (err === 0) {
                callback(req, res, err, message, data);
            } else {
                callback(req, res, err, message, null);
            }
        });
    },
    rename: function (req, res) {
        var data = JSON.parse(req.body.data);
        ProjectsData.rename(data.user_id, data.id, data.newName, function (err, message) {
            callback(req, res, err, message, null);
        });
    },
    getProject: function(req, res){
        var data = JSON.parse(req.body.data);
        ProjectsData.getUserProject(data.user_id, data.proj_id, function(err, message, data){
            if (err === 0) {
                callback(req, res, err, message, data);
            } else {
                callback(req, res, err, message, null);
            }
        });
    },
    beforeOpenProject: function (req, res) {
        var data = JSON.parse(req.body.data);
        ProjectsData.beforeOpenProject(data.user_id, data.proj_id, data.updateData, function (err, message, data) {
            callback(req, res, err, message, data);
        });
    },
    getNewProjectID: function (req, res) {
        var data = JSON.parse(req.body.data);
        ProjectsData.getNewProjectID(data.count, function (err, message, data) {
            callback(req, res, err, message, data);
        });
    }
}