const express = require("express");
const router = express.Router();
const authorize = require("../../../../middlewares/authorize-sapps");
const ucSettingService = require("./ucSetting.service");

router.post("/create-or-update", authorize(), createOrUpdate);
router.get("/", authorize(), getAll);
router.get("/filter", authorize(), filter);
router.get("/option", authorize(), option);
router.get("/count", authorize(), count);
router.put("/:id", authorize(), update);
router.put('/enabled/:id', authorize(), enabled);
router.delete("/:id", authorize(), _delete);

module.exports = router;

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

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

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

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

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

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

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

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

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