ntp&modercontent_front
This commit is contained in:
@@ -37,7 +37,7 @@ const upload = multer({
|
||||
// Получить все блоки для вкладки
|
||||
router.get('/:tab', async (req, res) => {
|
||||
const { tab } = req.params;
|
||||
if (!['info', 'union'].includes(tab)) return res.status(400).json({ message: 'Некорректная вкладка' });
|
||||
if (!['info', 'union', 'events', 'help', 'housing'].includes(tab)) return res.status(400).json({ message: 'Некорректная вкладка' });
|
||||
try {
|
||||
const [rows] = await db.query('SELECT * FROM content_blocks WHERE tab = ? ORDER BY created_at ASC', [tab]);
|
||||
res.json(rows);
|
||||
@@ -57,7 +57,7 @@ router.post('/:tab/upload', requireModerator, upload.single('file'), (req, res)
|
||||
router.post('/:tab', requireModerator, async (req, res) => {
|
||||
const { tab } = req.params;
|
||||
const { content } = req.body;
|
||||
if (!['info', 'union'].includes(tab)) return res.status(400).json({ message: 'Некорректная вкладка' });
|
||||
if (!['info', 'union', 'events', 'help', 'housing'].includes(tab)) return res.status(400).json({ message: 'Некорректная вкладка' });
|
||||
try {
|
||||
const [result] = await db.query(
|
||||
'INSERT INTO content_blocks (tab, type, content, created_by, created_at, updated_at) VALUES (?, ?, ?, ?, NOW(), NOW())',
|
||||
@@ -73,7 +73,7 @@ router.post('/:tab', requireModerator, async (req, res) => {
|
||||
router.put('/:tab/:id', requireModerator, async (req, res) => {
|
||||
const { tab, id } = req.params;
|
||||
const { content } = req.body;
|
||||
if (!['info', 'union'].includes(tab)) return res.status(400).json({ message: 'Некорректная вкладка' });
|
||||
if (!['info', 'union', 'events', 'help', 'housing'].includes(tab)) return res.status(400).json({ message: 'Некорректная вкладка' });
|
||||
try {
|
||||
await db.query(
|
||||
'UPDATE content_blocks SET content = ?, updated_at = NOW() WHERE id = ? AND tab = ?',
|
||||
@@ -88,7 +88,7 @@ router.put('/:tab/:id', requireModerator, async (req, res) => {
|
||||
// Удалить блок
|
||||
router.delete('/:tab/:id', requireModerator, async (req, res) => {
|
||||
const { tab, id } = req.params;
|
||||
if (!['info', 'union'].includes(tab)) return res.status(400).json({ message: 'Некорректная вкладка' });
|
||||
if (!['info', 'union', 'events', 'help', 'housing'].includes(tab)) return res.status(400).json({ message: 'Некорректная вкладка' });
|
||||
try {
|
||||
await db.query('DELETE FROM content_blocks WHERE id = ? AND tab = ?', [id, tab]);
|
||||
res.json({ message: 'Удалено' });
|
||||
@@ -96,5 +96,51 @@ router.delete('/:tab/:id', requireModerator, async (req, res) => {
|
||||
res.status(500).json({ message: 'Ошибка сервера' });
|
||||
}
|
||||
});
|
||||
router.get('/section/:section', async (req, res) => {
|
||||
const { section } = req.params;
|
||||
if (!['events', 'help', 'housing'].includes(section)) {
|
||||
return res.status(400).json({ message: 'Некорректный раздел' });
|
||||
}
|
||||
try {
|
||||
const [rows] = await db.query(
|
||||
'SELECT * FROM content_blocks WHERE tab = ? ORDER BY created_at ASC',
|
||||
[section]
|
||||
);
|
||||
// Если есть блоки — возвращаем их, иначе пустой массив
|
||||
res.json(rows);
|
||||
} catch (err) {
|
||||
console.error(err);
|
||||
res.status(500).json({ message: 'Ошибка сервера' });
|
||||
}
|
||||
});
|
||||
|
||||
// Сохранить контент раздела (очищаем старые и вставляем новый)
|
||||
router.post('/section/:section', requireModerator, async (req, res) => {
|
||||
const { section } = req.params;
|
||||
const { content } = req.body;
|
||||
|
||||
if (!['events', 'help', 'housing'].includes(section)) {
|
||||
return res.status(400).json({ message: 'Некорректный раздел' });
|
||||
}
|
||||
|
||||
try {
|
||||
// Удаляем старые блоки для этого раздела
|
||||
await db.query('DELETE FROM content_blocks WHERE tab = ?', [section]);
|
||||
|
||||
// Вставляем новый блок
|
||||
const [result] = await db.query(
|
||||
'INSERT INTO content_blocks (tab, type, content, created_by, created_at, updated_at) VALUES (?, ?, ?, ?, NOW(), NOW())',
|
||||
[section, 'text', content, req.session.userId]
|
||||
);
|
||||
|
||||
res.json({
|
||||
success: true,
|
||||
id: result.insertId,
|
||||
message: 'Раздел сохранён'
|
||||
});
|
||||
} catch (err) {
|
||||
console.error(err);
|
||||
res.status(500).json({ message: 'Ошибка сервера' });
|
||||
}
|
||||
});
|
||||
module.exports = router;
|
||||
Reference in New Issue
Block a user