var nguyenLieuDao = require('../dao/NguyenLieu');
var router = require('express').Router();


router.get('/', getAll);
function getAll(req, res, next) {
    nguyenLieuDao.getAll(function (err, response) {
        if (err) next(err);
        else res.json(response);
    });
}

router.get('/:id', get);
function get(req, res, next) {
    nguyenLieuDao.get(req.params.id, function (err, response) {
        if (err) next(err);
        else res.json(response);
    });
}

router.post('/', add);
function add(req, res, next) {
    var nguyenLieu = req.body;
    if (!nguyenLieu.ten || !nguyenLieu.donVi) {
        next({ message: "Fields empty" });
    } else {
        nguyenLieuDao.add(nguyenLieu, function (err, response) {
            if (err) next(err);
            else res.json(response);
        });
    }
}

router.put('/:id', update);
function update(req, res, next) {
    let nguyenLieu = req.body;
    if (!nguyenLieu.ten || !nguyenLieu.donVi) {
        next({ message: "Fields empty" });
    } else {
        nguyenLieuDao.update(nguyenLieu, function (err, response) {
            if (err) next(err);
            else res.json(response);
        });
    }
}
router.put('/:id/xuat', update);
function update(req, res, next) {
    let nguyenLieu = req.body;
    if (!nguyenLieu.soLuong) {
        next({ message: "Fields empty" });
    } else {
        nguyenLieuDao.xuat(nguyenLieu, function (err, response) {
            if (err) next(err);
            else res.json(response);
        });
    }
}

router.put('/:id/nhap', update);
function update(req, res, next) {
    let nguyenLieu = req.body;
    if (!nguyenLieu.soLuong) {
        next({ message: "Fields empty" });
    } else {
        nguyenLieuDao.nhap(nguyenLieu, function (err, response) {
            if (err) next(err);
            else res.json(response);
        });
    }
}

router.delete('/:id', remove);
function remove(req, res, next) {
    nguyenLieuDao.delete(req.params.id, function (err, response) {
        if (err) next(err);
        else res.json(response);
    });
}

module.exports = router;
