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

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

module.exports = router;

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

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

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

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

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

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

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

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

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