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

router.post("/", authorize(), getAll);
router.post('/unassigned', authorize(), getUnassignedMenusToRole);
router.post("/create", authorize(), create);
router.delete("/:id", authorize(), _delete);

module.exports = router;

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

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

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

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