scheduleController.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. import pool from '../config/db.js';
  2. // @desc 获取用户的所有时间安排
  3. // @route GET /api/schedules
  4. // @access Private
  5. const getSchedules = async (req, res) => {
  6. try {
  7. const userId = req.user.id;
  8. const [schedules] = await pool.query(
  9. 'SELECT * FROM schedules WHERE user_id = ? ORDER BY day_of_week, time_slot',
  10. [userId]
  11. );
  12. res.json(schedules);
  13. } catch (error) {
  14. console.error(error);
  15. res.status(500).json({ message: '服务器错误' });
  16. }
  17. };
  18. // @desc 创建新的时间安排
  19. // @route POST /api/schedules
  20. // @access Private
  21. const createSchedule = async (req, res) => {
  22. try {
  23. const { day_of_week, time_slot, activity } = req.body;
  24. const userId = req.user.id;
  25. // 验证输入
  26. if (day_of_week < 1 || day_of_week > 5) {
  27. return res.status(400).json({ message: '无效的星期几,必须在1-5之间' });
  28. }
  29. // 检查该时间段是否已有安排
  30. const [existingSchedules] = await pool.query(
  31. 'SELECT * FROM schedules WHERE user_id = ? AND day_of_week = ? AND time_slot = ?',
  32. [userId, day_of_week, time_slot]
  33. );
  34. if (existingSchedules.length > 0) {
  35. // 更新现有安排
  36. await pool.query(
  37. 'UPDATE schedules SET activity = ? WHERE user_id = ? AND day_of_week = ? AND time_slot = ?',
  38. [activity, userId, day_of_week, time_slot]
  39. );
  40. const [updatedSchedule] = await pool.query(
  41. 'SELECT * FROM schedules WHERE user_id = ? AND day_of_week = ? AND time_slot = ?',
  42. [userId, day_of_week, time_slot]
  43. );
  44. return res.json(updatedSchedule[0]);
  45. }
  46. // 创建新安排
  47. const [result] = await pool.query(
  48. 'INSERT INTO schedules (user_id, day_of_week, time_slot, activity, completed) VALUES (?, ?, ?, ?, false)',
  49. [userId, day_of_week, time_slot, activity]
  50. );
  51. if (result.insertId) {
  52. const [newSchedule] = await pool.query('SELECT * FROM schedules WHERE id = ?', [result.insertId]);
  53. res.status(201).json(newSchedule[0]);
  54. } else {
  55. res.status(400).json({ message: '无效的时间安排数据' });
  56. }
  57. } catch (error) {
  58. console.error(error);
  59. res.status(500).json({ message: '服务器错误' });
  60. }
  61. };
  62. // @desc 更新时间安排完成状态
  63. // @route PUT /api/schedules/:id/toggle-complete
  64. // @access Private
  65. const toggleScheduleComplete = async (req, res) => {
  66. try {
  67. const scheduleId = req.params.id;
  68. const userId = req.user.id;
  69. // 检查时间安排是否存在
  70. const [existingSchedules] = await pool.query(
  71. 'SELECT * FROM schedules WHERE id = ? AND user_id = ?',
  72. [scheduleId, userId]
  73. );
  74. if (existingSchedules.length === 0) {
  75. return res.status(404).json({ message: '时间安排未找到' });
  76. }
  77. const currentCompleted = existingSchedules[0].completed;
  78. // 更新完成状态
  79. await pool.query(
  80. 'UPDATE schedules SET completed = ? WHERE id = ? AND user_id = ?',
  81. [!currentCompleted, scheduleId, userId]
  82. );
  83. // 获取更新后的时间安排
  84. const [updatedSchedules] = await pool.query('SELECT * FROM schedules WHERE id = ?', [scheduleId]);
  85. res.json(updatedSchedules[0]);
  86. } catch (error) {
  87. console.error(error);
  88. res.status(500).json({ message: '服务器错误' });
  89. }
  90. };
  91. // @desc 删除时间安排
  92. // @route DELETE /api/schedules/:id
  93. // @access Private
  94. const deleteSchedule = async (req, res) => {
  95. try {
  96. const scheduleId = req.params.id;
  97. const userId = req.user.id;
  98. // 检查时间安排是否存在
  99. const [existingSchedules] = await pool.query(
  100. 'SELECT * FROM schedules WHERE id = ? AND user_id = ?',
  101. [scheduleId, userId]
  102. );
  103. if (existingSchedules.length === 0) {
  104. return res.status(404).json({ message: '时间安排未找到' });
  105. }
  106. // 删除时间安排
  107. await pool.query('DELETE FROM schedules WHERE id = ? AND user_id = ?', [scheduleId, userId]);
  108. res.json({ message: '时间安排已删除' });
  109. } catch (error) {
  110. console.error(error);
  111. res.status(500).json({ message: '服务器错误' });
  112. }
  113. };
  114. export { getSchedules, createSchedule, toggleScheduleComplete, deleteSchedule };