import express from 'express'; import PostController from '../controllers/Post.controller.js'; import OptionController from '../controllers/Option.controller.js'; var router = express.Router(); function handleOptionOperation (option) { switch(option.action) { case 'create': OptionController.create(option.name, option.value); break; case 'update': OptionController.update(option.name, option.value); break; case 'delete': OptionController.delete(option.name); break; } } router.get('/', function (_, res) { res.render('conclave/admin_panel'); }); router.get('/new_post', async function(_, res) { res.render('conclave'); }); router.post('/posts/new', async function(req, res) { await PostController.create(req.body.title, req.body.content); res.redirect('/conclave'); }); router.get('/options', async function(_, res) { const options = await OptionController.getAll(); res.render('conclave/options', { options }); }); router.get('/options/edit', async function (_, res) { const options = await OptionController.getAll(); res.render('conclave/options_edit', { options }); }); // TODO: currently we send the whole options even the one that arent changed. we need to update the front-end to only send the updated ones. I think we will need javascript. router.post('/options/edit', async function (req, res) { if (Array.isArray(req.body.name)) { const options = req.body.name.map((name, index) => { return { name, action: req.body.action[index], value: req.body.value[index] } }); options.forEach(handleOptionOperation); } else { handleOptionOperation(req.body); } res.redirect('/conclave/options'); }); export default router;