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

router.post("/create", authorize(), create);
router.post("/send-email-distributor", sendEmailDistributor);
router.get("/", authorize(), getAll);
router.get("/filter", authorize(), filter);
router.get("/filter-by-id", authorize(), filterByID);
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 create(req, res, next) {
  udpService
    .create(req.body)
    .then((result) => res.json(result))
    .catch(next);
}

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

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

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

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

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

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

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

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