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

router.post("/create", authorize(), create);
router.post("/holiday-by-holiday-group/", authorize(), getHolidayByHolidayGroupId);
router.get("/", authorize(), getAll);
router.get("/filter", authorize(), filter);
router.get("/option", authorize(), option);
router.get("/count", authorize(), count);
router.get("/by-date", authorize(), getByDate);
router.put("/:id", authorize(), update);
router.put("/enabled/:id", authorize(), enabled);
router.delete("/:id", authorize(), _delete);

module.exports = router;

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

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

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

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

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

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

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

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

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


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