- 后端添加获取学生课程列表的API路由 - 实现Course模型中的findByStudentId方法查询学生课程 - 新增学生控制器的getCourses方法处理课程请求 - 前端添加课程表格展示及刷新功能
30 lines
854 B
JavaScript
30 lines
854 B
JavaScript
const db = require('../config/database');
|
|
|
|
class Course {
|
|
static async findByTeacherId(teacherId) {
|
|
return await db.query(
|
|
'SELECT * FROM courses WHERE teacher_id = ? ORDER BY course_code',
|
|
[teacherId]
|
|
);
|
|
}
|
|
|
|
static async findById(id) {
|
|
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; |