Initial commit: Club DynaSQY
This commit is contained in:
@@ -0,0 +1,55 @@
|
||||
const { Router } = require('express');
|
||||
const pool = require('../config/db');
|
||||
const { authenticate } = require('../middleware/auth');
|
||||
|
||||
const router = Router();
|
||||
|
||||
router.get('/:date', authenticate, async (req, res) => {
|
||||
if (req.user.role !== 'admin') return res.status(403).json({ error: 'Admin seulement' });
|
||||
try {
|
||||
const { date } = req.params;
|
||||
const [yr, mo, dy] = date.split('-');
|
||||
const dateKey = `${dy}${mo}${yr}`;
|
||||
const [rows] = await pool.query(
|
||||
'SELECT id, slide_key, content, hidden, sort_order FROM presentation_overrides WHERE date_key = ? ORDER BY sort_order ASC',
|
||||
[dateKey]
|
||||
);
|
||||
res.json(rows);
|
||||
} catch (err) {
|
||||
console.error(err);
|
||||
res.status(500).json({ error: 'Erreur serveur' });
|
||||
}
|
||||
});
|
||||
|
||||
router.put('/:date', authenticate, async (req, res) => {
|
||||
if (req.user.role !== 'admin') return res.status(403).json({ error: 'Admin seulement' });
|
||||
try {
|
||||
const { date } = req.params;
|
||||
const { overrides } = req.body;
|
||||
const [yr, mo, dy] = date.split('-');
|
||||
const dateKey = `${dy}${mo}${yr}`;
|
||||
if (!Array.isArray(overrides)) return res.status(400).json({ error: 'overrides doit être un tableau' });
|
||||
|
||||
for (const ov of overrides) {
|
||||
const { slide_key, content, hidden, sort_order } = ov;
|
||||
if (!slide_key) continue;
|
||||
await pool.query(
|
||||
`INSERT INTO presentation_overrides (date_key, slide_key, content, hidden, sort_order)
|
||||
VALUES (?, ?, ?, ?, ?)
|
||||
ON DUPLICATE KEY UPDATE content = VALUES(content), hidden = VALUES(hidden), sort_order = VALUES(sort_order)`,
|
||||
[dateKey, slide_key, content ? JSON.stringify(content) : null, hidden ? 1 : 0, sort_order ?? 0]
|
||||
);
|
||||
}
|
||||
|
||||
const [rows] = await pool.query(
|
||||
'SELECT slide_key, content, hidden, sort_order FROM presentation_overrides WHERE date_key = ? ORDER BY sort_order ASC',
|
||||
[dateKey]
|
||||
);
|
||||
res.json({ overrides: rows });
|
||||
} catch (err) {
|
||||
console.error(err);
|
||||
res.status(500).json({ error: 'Erreur serveur' });
|
||||
}
|
||||
});
|
||||
|
||||
module.exports = router;
|
||||
Reference in New Issue
Block a user