const Group = require('../models/Group'); // @desc 获取所有群组 // @route GET /api/groups // @access Private/Admin const getGroups = async (req, res) => { try { const groups = await Group.findAll(); res.json(groups); } catch (error) { res.status(500).json({ message: '服务器错误' }); } }; // @desc 获取单个群组 // @route GET /api/groups/:id // @access Private/Admin const getGroupById = async (req, res) => { try { const group = await Group.findById(req.params.id); if (group) { res.json(group); } else { res.status(404).json({ message: '群组不存在' }); } } catch (error) { res.status(500).json({ message: '服务器错误' }); } }; // @desc 创建群组 // @route POST /api/groups // @access Private/Admin const createGroup = async (req, res) => { try { const { groupId, groupName } = req.body; const groupIdExists = await Group.findByGroupId(groupId); if (groupIdExists) { return res.status(400).json({ message: '群组ID已存在' }); } const id = await Group.create({ groupId, groupName }); const group = await Group.findById(id); res.status(201).json(group); } catch (error) { res.status(500).json({ message: '服务器错误' }); } }; // @desc 更新群组 // @route PUT /api/groups/:id // @access Private/Admin const updateGroup = async (req, res) => { try { const { groupName, isActive } = req.body; const group = await Group.findById(req.params.id); if (!group) { return res.status(404).json({ message: '群组不存在' }); } await Group.update(req.params.id, { groupName, isActive }); const updatedGroup = await Group.findById(req.params.id); res.json(updatedGroup); } catch (error) { res.status(500).json({ message: '服务器错误' }); } }; // @desc 删除群组 // @route DELETE /api/groups/:id // @access Private/Admin const deleteGroup = async (req, res) => { try { const group = await Group.findById(req.params.id); if (!group) { return res.status(404).json({ message: '群组不存在' }); } await Group.delete(req.params.id); res.json({ message: '群组已删除' }); } catch (error) { res.status(500).json({ message: '服务器错误' }); } }; module.exports = { getGroups, getGroupById, createGroup, updateGroup, deleteGroup };