diff --git a/router.js b/router.js index 7ee77e8..56742c8 100644 --- a/router.js +++ b/router.js @@ -1,24 +1,50 @@ -const express = require('express'); -const router = express.Router(); -const multer = require('multer'); -const randomSelect = require('./services/randomSelect'); -const ranking = require('./services/ranking'); -const classManager = require('./services/classManager'); -const scoreManager = require('./services/scoreManager'); - -// 文件上传配置 -const upload = multer({ dest: 'uploads/' }); - -// 路由 - 添加班级 -router.post('/upload', upload.single('file'), classManager.addClass); - -// 路由 - 随机点名 -router.get('/random-call', randomSelect.selectStudent); - -// 路由 - 获取排名 -router.get('/ranking', ranking.getRanking); - -// 路由 - 更新分数 -router.post('/update-score', scoreManager.updateScore); - -module.exports = router; +const express = require('express'); +const router = express.Router(); +const multer = require('multer'); +const randomSelect = require('./services/randomSelect'); +const ranking = require('./services/ranking'); +const classManager = require('./services/classManager'); +const scoreManager = require('./services/scoreManager'); +const TableManager = require('./services/delete-table'); +const pool = require('./db'); +// 文件上传配置 +const upload = multer({ dest: 'uploads/' }); + +// 路由 - 添加班级 +router.post('/upload', upload.single('file'), classManager.addClass); + +// 路由 - 随机点名 +router.get('/random-call', randomSelect.selectStudent); + +// 路由 - 获取排名 +router.get('/ascend_ranking', ranking.get_ascend_Ranking); +router.get('/descend_ranking', ranking.get_descend_Ranking); + +// 路由 - 更新分数 +router.post('/update-score', scoreManager.updateScore); + +//路由 - 获取某个id的分数 +router.post('/get-score', scoreManager.getStudentScore); + +//路由 - 删除班级 +router.post('/delete-table', TableManager.deleteTable); + + +// 获取全部学生名单 +router.get('/get-students', async (req, res) => { + let connection; // 声明连接变量 + try { + connection = await pool.getConnection(); // 从连接池获取连接 + const [students] = await connection.query('SELECT student_name FROM students'); // 执行查询 + const studentNames = students.map(student => ({ student_name: student.student_name })); // 只保留 student_name 字段 + res.json(studentNames); // 返回只包含 student_name 的结果 + } catch (error) { + console.error('Error fetching students:', error); // 记录错误信息 + res.status(500).send({ error: 'Failed to fetch students' }); // 返回500错误响应 + } finally { + if (connection) connection.release(); // 确保连接被释放 + } +}); + + +module.exports = router;