/* * MC Report System * Copyright (C) 2026 Sea Network Technology Studio * Author: CangLan * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU Affero General Public License as published * by the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Affero General Public License for more details. * * You should have received a copy of the GNU Affero General Public License * along with this program. If not, see . */ const express = require('express'); const { query, getRow } = require('../db'); const { authenticate, requireRole } = require('../middleware/auth'); const router = express.Router(); // 统一校验 :id 路径参数(必须是正整数) router.param('id', (req, res, next, id) => { if (!/^\d+$/.test(id)) return res.status(400).json({ error: '无效的ID' }); next(); }); router.get('/', authenticate, async (req, res) => { const { group_name, server_name, sort, status } = req.query; let orderClause = 'ORDER BY fi.created_at DESC'; if (sort === 'votes') orderClause = 'ORDER BY votes_count DESC, fi.created_at DESC'; const items = await query(`SELECT fi.*, (SELECT COUNT(*) FROM feature_votes WHERE item_id = fi.id) as votes_count, (SELECT COUNT(*) FROM feature_votes WHERE item_id = fi.id AND user_id = ?) as my_vote, (SELECT COUNT(*) FROM feature_comments WHERE item_id = fi.id) as comments_count FROM feature_items fi WHERE (fi.group_name = ? OR ? = '') AND (fi.server_name = ? OR ? = '') AND (fi.status = ? OR ? = '' OR ? = 'all') ${orderClause}`, [req.user.id, group_name||'', group_name||'', server_name||'', server_name||'', status||'', status||'', status||'']); res.json(items); }); router.get('/:id', authenticate, async (req, res) => { const item = await getRow(`SELECT fi.*, (SELECT COUNT(*) FROM feature_votes WHERE item_id = fi.id) as votes_count, (SELECT COUNT(*) FROM feature_votes WHERE item_id = fi.id AND user_id = ?) as my_vote FROM feature_items fi WHERE fi.id = ?`, [req.user.id, req.params.id]); if (!item) return res.status(404).json({ error: '不存在' }); item.comments = await query(`SELECT fc.*, u.username, u.role FROM feature_comments fc LEFT JOIN users u ON fc.user_id = u.id WHERE fc.item_id = ? ORDER BY fc.created_at ASC`, [req.params.id]); res.json(item); }); router.post('/', authenticate, requireRole('owner'), async (req, res) => { const { title, description, group_name, server_name } = req.body; if (!title) return res.status(400).json({ error: '标题为必填' }); const r = await query('INSERT INTO feature_items(title, description, group_name, server_name) VALUES (?,?,?,?)', [title, description||'', group_name||'', server_name||'']); res.status(201).json({ id: r.insertId }); }); router.put('/:id', authenticate, requireRole('owner'), async (req, res) => { const fields = {}; if (req.body.title) fields.title = req.body.title; if (req.body.description !== undefined) fields.description = req.body.description; if (req.body.status) fields.status = req.body.status; if (req.body.group_name) fields.group_name = req.body.group_name; if (req.body.server_name) fields.server_name = req.body.server_name; if (!Object.keys(fields).length) return res.status(400).json({ error: '无更新内容' }); const sets = Object.keys(fields).map(k => `${k} = ?`).join(', '); await query(`UPDATE feature_items SET ${sets} WHERE id = ?`, [...Object.values(fields), req.params.id]); res.json({ message: '更新成功' }); }); router.delete('/:id', authenticate, requireRole('owner'), async (req, res) => { await query('DELETE FROM feature_comments WHERE item_id = ?', [req.params.id]); await query('DELETE FROM feature_votes WHERE item_id = ?', [req.params.id]); await query('DELETE FROM feature_items WHERE id = ?', [req.params.id]); res.json({ message: '已删除' }); }); router.post('/:id/vote', authenticate, async (req, res) => { const item = await getRow('SELECT id FROM feature_items WHERE id = ?', [req.params.id]); if (!item) return res.status(404).json({ error: '不存在' }); const voted = await getRow('SELECT id FROM feature_votes WHERE item_id = ? AND user_id = ?', [item.id, req.user.id]); if (voted) { await query('DELETE FROM feature_votes WHERE item_id = ? AND user_id = ?', [item.id, req.user.id]); return res.json({ message: '已取消投票' }); } await query('INSERT INTO feature_votes(item_id, user_id) VALUES (?,?)', [item.id, req.user.id]); res.json({ message: '投票成功' }); }); router.post('/:id/comment', authenticate, async (req, res) => { const { content } = req.body; if (!content) return res.status(400).json({ error: '内容不能为空' }); const item = await getRow('SELECT id FROM feature_items WHERE id = ?', [req.params.id]); if (!item) return res.status(404).json({ error: '不存在' }); await query('INSERT INTO feature_comments(item_id, user_id, content) VALUES (?,?,?)', [req.params.id, req.user.id, content]); res.status(201).json({ message: '评论成功' }); }); module.exports = router;