{"size": 2392, "ext": "js", "max_stars_repo_path": "back-end/controllers/loan_slip_pay_ctrl.js", "max_stars_repo_name": "nguyendat281097/project-isc-quangtrung", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "back-end/controllers/loan_slip_pay_ctrl.js", "max_issues_repo_name": "nguyendat281097/project-isc-quangtrung", "max_issues_repo_issues_event_min_datetime": "2021-05-10T15:19:07.000Z", "max_issues_repo_issues_event_max_datetime": "2021-08-03T23:40:24.000Z", "max_forks_repo_path": "back-end/controllers/loan_slip_pay_ctrl.js", "max_forks_repo_name": "nguyendat281097/project-isc-quangtrung", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": 2, "max_forks_count": null, "avg_line_length": 26.8764044944, "max_line_length": 66, "alphanum_fraction": 0.4774247492}
const express = require('express');
const { LoanSlipPay, LoanSlipPayInfo } = require('../models/db');
const {ErrorResult, Result} = require('./../utils/base_response');
const router = express.Router();
router.use((req, res, next) => {
    //Authorize here
    next();
});

// Fill customer type apis here
router.get('/', (req, res) =>{
    LoanSlipPay.findAll().then(types => {
        res.json(Result(types));
    });
});

router.get('/:id(\\d+)', (req, res) =>{
    LoanSlipPay.findByPk(req.params.id).then(type => {
        if(type != null){
            res.json(Result(type));
        } else{
             res.json(ErrorResult(200, "Not Found"));
        }
    });
});

router.post('/', (req, res) =>{
    LoanSlipPay.create(req.body).then(type =>{
        res.json(Result(type));
    }).catch(err => {
        res.json(ErrorResult(500, err.errors));
    });
});

router.put('/:id(\\d+)', (req, res) =>{
    LoanSlipPay.findByPk(req.params.id).then(type =>{
        if(type !=null){
            type.update({
                RD_ID: req.body.BOT_ID,
                LIBA_ID: req.body.LIBA_ID,
                loanDate: req.body.loanDate,
                promisedDateToPay: req.body.promisedDateToPay,
            }).then(type => {
                res.json(Result(type));
            }).catch(err =>{
                res.json(ErrorResult(500, err.errors));
            });
        } else {
             res.json(ErrorResult(200, "Not Found"));
        }
    });
});

// router.delete('/:id', (req, res) =>{
//     LoanSlipPay.destroy({
//         where: {
//             id: req.params.id
//         }
//     }).then(type => {
//         res.json(Result(type));
//     }).catch(err => {
//         res.status(500).json(ErrorResult(500, err.errors));
//     });
// });

router.delete('/:id', (req, res) =>{
    LoanSlipPayInfo.findAll({
        where: {
          LSP_ID: req.params.id
        }
      }).then(type =>{
          if(Array.isArray(type) && type.length === 0){
            LoanSlipPay.destroy({
                where: {
                    id: req.params.id
                }
            }).then(type => {
                res.json(Result(type));
            }).catch(err => {
                res.json(ErrorResult(500, err.errors));
            });
          } else {
            res.json(ErrorResult(200, 'Has Frimary Key'));
          }
      })
});

module.exports = router;
