import pool from '../config/db.js'; // @desc 获取用户的所有时间安排 // @route GET /api/schedules // @access Private const getSchedules = async (req, res) => { try { const userId = req.user.id; const [schedules] = await pool.query( 'SELECT * FROM schedules WHERE user_id = ? ORDER BY day_of_week, time_slot', [userId] ); res.json(schedules); } catch (error) { console.error(error); res.status(500).json({ message: '服务器错误' }); } }; // @desc 创建新的时间安排 // @route POST /api/schedules // @access Private const createSchedule = async (req, res) => { try { const { day_of_week, time_slot, activity } = req.body; const userId = req.user.id; // 验证输入 if (day_of_week < 1 || day_of_week > 5) { return res.status(400).json({ message: '无效的星期几,必须在1-5之间' }); } // 检查该时间段是否已有安排 const [existingSchedules] = await pool.query( 'SELECT * FROM schedules WHERE user_id = ? AND day_of_week = ? AND time_slot = ?', [userId, day_of_week, time_slot] ); if (existingSchedules.length > 0) { // 更新现有安排 await pool.query( 'UPDATE schedules SET activity = ? WHERE user_id = ? AND day_of_week = ? AND time_slot = ?', [activity, userId, day_of_week, time_slot] ); const [updatedSchedule] = await pool.query( 'SELECT * FROM schedules WHERE user_id = ? AND day_of_week = ? AND time_slot = ?', [userId, day_of_week, time_slot] ); return res.json(updatedSchedule[0]); } // 创建新安排 const [result] = await pool.query( 'INSERT INTO schedules (user_id, day_of_week, time_slot, activity, completed) VALUES (?, ?, ?, ?, false)', [userId, day_of_week, time_slot, activity] ); if (result.insertId) { const [newSchedule] = await pool.query('SELECT * FROM schedules WHERE id = ?', [result.insertId]); res.status(201).json(newSchedule[0]); } else { res.status(400).json({ message: '无效的时间安排数据' }); } } catch (error) { console.error(error); res.status(500).json({ message: '服务器错误' }); } }; // @desc 更新时间安排完成状态 // @route PUT /api/schedules/:id/toggle-complete // @access Private const toggleScheduleComplete = async (req, res) => { try { const scheduleId = req.params.id; const userId = req.user.id; // 检查时间安排是否存在 const [existingSchedules] = await pool.query( 'SELECT * FROM schedules WHERE id = ? AND user_id = ?', [scheduleId, userId] ); if (existingSchedules.length === 0) { return res.status(404).json({ message: '时间安排未找到' }); } const currentCompleted = existingSchedules[0].completed; // 更新完成状态 await pool.query( 'UPDATE schedules SET completed = ? WHERE id = ? AND user_id = ?', [!currentCompleted, scheduleId, userId] ); // 获取更新后的时间安排 const [updatedSchedules] = await pool.query('SELECT * FROM schedules WHERE id = ?', [scheduleId]); res.json(updatedSchedules[0]); } catch (error) { console.error(error); res.status(500).json({ message: '服务器错误' }); } }; // @desc 删除时间安排 // @route DELETE /api/schedules/:id // @access Private const deleteSchedule = async (req, res) => { try { const scheduleId = req.params.id; const userId = req.user.id; // 检查时间安排是否存在 const [existingSchedules] = await pool.query( 'SELECT * FROM schedules WHERE id = ? AND user_id = ?', [scheduleId, userId] ); if (existingSchedules.length === 0) { return res.status(404).json({ message: '时间安排未找到' }); } // 删除时间安排 await pool.query('DELETE FROM schedules WHERE id = ? AND user_id = ?', [scheduleId, userId]); res.json({ message: '时间安排已删除' }); } catch (error) { console.error(error); res.status(500).json({ message: '服务器错误' }); } }; export { getSchedules, createSchedule, toggleScheduleComplete, deleteSchedule };