|
|
@ -4,7 +4,20 @@ const xlsx = require('xlsx');
|
|
|
|
const pool = require('../db');
|
|
|
|
const pool = require('../db');
|
|
|
|
|
|
|
|
|
|
|
|
const classManager = {
|
|
|
|
const classManager = {
|
|
|
|
addClass: (req, res) => {
|
|
|
|
addClass: async (req, res) => {
|
|
|
|
|
|
|
|
// 创建students表的SQL语句
|
|
|
|
|
|
|
|
const createTableQuery = `
|
|
|
|
|
|
|
|
CREATE TABLE IF NOT EXISTS students (
|
|
|
|
|
|
|
|
student_id INT AUTO_INCREMENT PRIMARY KEY,
|
|
|
|
|
|
|
|
student_name VARCHAR(255) NOT NULL,
|
|
|
|
|
|
|
|
score DECIMAL(5, 2),
|
|
|
|
|
|
|
|
call_count INT DEFAULT 0
|
|
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
`;
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
// 先创建表
|
|
|
|
|
|
|
|
await pool.query(createTableQuery);
|
|
|
|
|
|
|
|
|
|
|
|
const filePath = req.file.path;
|
|
|
|
const filePath = req.file.path;
|
|
|
|
|
|
|
|
|
|
|
|
// 读取并解析Excel文件
|
|
|
|
// 读取并解析Excel文件
|
|
|
@ -15,7 +28,7 @@ const classManager = {
|
|
|
|
const students = xlsx.utils.sheet_to_json(worksheet);
|
|
|
|
const students = xlsx.utils.sheet_to_json(worksheet);
|
|
|
|
|
|
|
|
|
|
|
|
// 定义所需的字段
|
|
|
|
// 定义所需的字段
|
|
|
|
const requiredFields = [ 'student_id', 'student_name'];
|
|
|
|
const requiredFields = ['student_id','student_name'];
|
|
|
|
let hasError = false;
|
|
|
|
let hasError = false;
|
|
|
|
|
|
|
|
|
|
|
|
students.forEach(student => {
|
|
|
|
students.forEach(student => {
|
|
|
@ -42,6 +55,10 @@ const classManager = {
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
res.status(200).json({ message: 'Class added successfully!' });
|
|
|
|
res.status(200).json({ message: 'Class added successfully!' });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} catch (err) {
|
|
|
|
|
|
|
|
console.error('Error creating students table:', err);
|
|
|
|
|
|
|
|
res.status(500).json({ message: 'Error creating table during class addition.' });
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|