/**
 * Created by CSL on 2017-10-10.
 */

let mongoose = require('mongoose');
let labourCoeFacade = require('../facade/labour_coe_facade');

module.exports = {
    getProjectLabourCoe: getProjectLabourCoe,
    getStdLabourCoe: getStdLabourCoe,
    save: save
};

async function getProjectLabourCoe(req, res) {
    let result={error: 0};

    try {
        let projLC = await labourCoeFacade.getProjectLabourCoe(req.body.data.labourCoeID);
        result.data= projLC;
    }catch (err){
        console.log(err);
        result.error = 1;
        result.message = err.message;
    }

    res.json(result);
};

async function getStdLabourCoe(req, res) {
    let result = {error: 0, message: '', data: null};

    try {
        let stdLC = await labourCoeFacade.getStdLabourCoe(JSON.parse(req.body.data).ID);
        result.data= stdLC;
    }catch (err){
        console.log(err);
        result.error = 1;
        result.message = err.message;
    }

    res.json(result);
};

function save(req, res) {
    let result = {error: 0, message: '', data: null};

    labourCoeFacade.save(req.body.data, function (err, data) {
        if (err == ''){
            result.data = data;
        }else{
            result.error = 1;
            result.message = err;
        }
    });

    res.json(result);
};
