feat(学生): 添加学生课程查询功能

- 后端添加获取学生课程列表的API路由
- 实现Course模型中的findByStudentId方法查询学生课程
- 新增学生控制器的getCourses方法处理课程请求
- 前端添加课程表格展示及刷新功能
This commit is contained in:
2025-12-21 22:55:43 +08:00
parent 00e2f6ac88
commit f360194efd
6 changed files with 129 additions and 6 deletions

View File

@@ -30,6 +30,20 @@ class StudentController {
error(res, '服务器错误');
}
}
static async getCourses(req, res) {
try {
const userId = req.session.user.id;
const data = await StudentService.getStudentCourses(userId);
success(res, data);
} catch (err) {
if (err.message === '学生信息不存在') {
return error(res, err.message, 404);
}
console.error('Get Courses Error:', err);
error(res, '服务器错误');
}
}
}
module.exports = StudentController;

View File

@@ -12,6 +12,19 @@ class Course {
const rows = await db.query('SELECT * FROM courses WHERE id = ?', [id]);
return rows[0];
}
static async findByStudentId(studentId) {
const sql = `
SELECT c.*, u.name as teacher_name
FROM courses c
JOIN classes cl ON c.class_id = cl.id
JOIN students s ON cl.class_name = s.class
JOIN users u ON c.teacher_id = u.id
WHERE s.id = ?
ORDER BY c.course_code
`;
return await db.query(sql, [studentId]);
}
}
module.exports = Course;

View File

@@ -5,5 +5,6 @@ const { requireAuth, requireRole } = require('../middleware/auth');
router.get('/grades', requireAuth, requireRole(['student']), StudentController.getGrades);
router.get('/grades/:id', requireAuth, requireRole(['student']), StudentController.getGradeDetails);
router.get('/courses', requireAuth, requireRole(['student']), StudentController.getCourses);
module.exports = router;

View File

@@ -1,12 +1,9 @@
const Score = require('../models/Score');
const Student = require('../models/Student');
const Course = require('../models/Course');
class StudentService {
static async getStudentGrades(userId) {
// 先通过 userId 获取 studentId
// 假设 users.id = students.id或者通过 user_id 关联
// 根据之前的 authService我们假设 users.id 就是 students.id
// 确认学生是否存在
const student = await Student.findById(userId);
if (!student) {
@@ -50,6 +47,14 @@ class StudentService {
}
return grade;
}
static async getStudentCourses(userId) {
const student = await Student.findById(userId);
if (!student) {
throw new Error('学生信息不存在');
}
return await Course.findByStudentId(userId);
}
}
module.exports = StudentService;
module.exports = StudentService;