const express = require('express');
const router = express.Router();
const authorize = require('middleware/authorize-sapps');
const udkService = require('./udk.service');

// routes
router.post('/create', authorize(), create);
router.get('/', authorize(), getAll);
router.get('/filter', authorize(), getFilter);
router.get('/:id', authorize(), getOne);
router.get('/byudk/:id', authorize(), getUDKbyUDKid);
router.get('/name/:id', authorize(), getName);
router.get('/option/list', authorize(), getOption);
router.get('/count', authorize(), getCount);
router.put('/:id', authorize(), update);
router.put('/document/:id', authorize(), updateByDocType);
router.delete('/:id', authorize(), _delete);

module.exports = router;

function create(req, res, next) {
    udkService.create(req.body)
        .then(result => res.json(result))
        .catch(next);
}

function getAll(req, res, next) {
    udkService.getAll()
        .then(result => res.json(result))
        .catch(next);
}

function getFilter(req, res, next) {
    udkService.getFilter(req.query)
        .then(result => res.json(result))
        .catch(next);
}

function getOne(req, res, next) {
    udkService.getOne(req.params.id)
        .then(result => res.json(result))
        .catch(next);
}

function getUDKbyUDKid(req, res, next) {
    udkService.getUDKbyUDKid(req.params.id)
        .then(result => res.json(result))
        .catch(next);
}

function getName(req, res, next) {
    udkService.getName(req.params.id)
        .then(result => res.json(result))
        .catch(next);
}

function getOption(req, res, next) {
    udkService.getOption()
        .then(result => res.json(result))
        .catch(next);
}

function getCount(req, res, next) {
    udkService.getCount()
        .then(result => res.json(result))
        .catch(next);
}

function update(req, res, next) {
    udkService.update(req.params.id, req.body)
        .then(result => res.json(result))
        .catch(next);
}
function updateByDocType(req, res, next) {
    udkService.updateByDocType(req.params.id, req.body)
        .then(result => res.json(result))
        .catch(next);
}

function _delete(req, res, next) {
    udkService.delete(req.params.id)
        .then(result => res.json(result))
        .catch(next);
}
