/
ruscreator
/
Webed
Обзор
Документация
Войти
/
ruscreator
/
Webed
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
main
backend/src/controllers/topicController.js
41 строка
1 KB
Алексей Слепов
Initial commit
17 фев 2025, 15:15
17 фев 2025, 15:15
63a31d6
Код
Авторство
О чём код?
const { Topic, sequelize } = require('../models'); const topicController = { async getAll(req, res) { try { const topics = await Topic.findAll({ include: [ { association: 'articles', attributes: ['id'] } ], attributes: { include: [ [ sequelize.literal('(SELECT COUNT(*) FROM articles WHERE articles.topic_id = Topic.id)'), 'articlesCount' ] ] } }); res.json(topics); } catch (error) { console.error('Ошибка при получении тем:', error); res.status(500).json({ message: 'Ошибка сервера' }); } }, async create(req, res) { try { const topic = await Topic.create(req.body); res.status(201).json(topic); } catch (error) { console.error('Ошибка при создании темы:', error); res.status(500).json({ message: 'Ошибка сервера' }); } } }; module.exports = topicController;