refactor: remove polls+features, move to standalone mc-vote repo
This commit is contained in:
@@ -1,51 +0,0 @@
|
||||
const express = require('express');
|
||||
const { query, getRow } = require('../db');
|
||||
const { authenticate, requireRole } = require('../middleware/auth');
|
||||
|
||||
const router = express.Router();
|
||||
|
||||
router.get('/', authenticate, async (req, res) => {
|
||||
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
|
||||
FROM feature_items fi ORDER BY votes_count DESC, fi.created_at DESC`, [req.user.id]);
|
||||
res.json(items);
|
||||
});
|
||||
|
||||
router.post('/', authenticate, requireRole('owner'), async (req, res) => {
|
||||
const { title, description } = req.body;
|
||||
if (!title) return res.status(400).json({ error: '标题为必填' });
|
||||
const r = await query('INSERT INTO feature_items(title, description) VALUES (?,?)', [title, description||'']);
|
||||
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 (!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_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: '投票成功' });
|
||||
});
|
||||
|
||||
module.exports = router;
|
||||
@@ -1,72 +0,0 @@
|
||||
const express = require('express');
|
||||
const { query, getRow } = require('../db');
|
||||
const { authenticate, requireRole } = require('../middleware/auth');
|
||||
|
||||
const router = express.Router();
|
||||
|
||||
router.get('/active', authenticate, async (req, res) => {
|
||||
const polls = await query('SELECT id, title, description, active, created_at FROM polls WHERE active = 1 ORDER BY created_at DESC');
|
||||
for (const p of polls) {
|
||||
p.options = JSON.parse(p.options || '[]');
|
||||
const rows = await query(`SELECT pv.option_index,
|
||||
SUM(CASE WHEN u.role IN ('owner','admin') THEN 1.5 ELSE 1 END) as total
|
||||
FROM poll_votes pv JOIN users u ON pv.user_id = u.id
|
||||
WHERE pv.poll_id = ? GROUP BY pv.option_index`, [p.id]);
|
||||
p.votes = {};
|
||||
let total = 0;
|
||||
for (const r of rows) { p.votes[r.option_index] = Math.round(r.total * 10) / 10; total += r.total; }
|
||||
p.total = Math.round(total * 10) / 10;
|
||||
const myVote = await getRow('SELECT option_index FROM poll_votes WHERE poll_id = ? AND user_id = ?', [p.id, req.user.id]);
|
||||
p.my_vote = myVote ? myVote.option_index : null;
|
||||
p.voted = !!myVote;
|
||||
}
|
||||
res.json(polls);
|
||||
});
|
||||
|
||||
router.get('/:id', authenticate, requireRole('owner'), async (req, res) => {
|
||||
const p = await getRow('SELECT * FROM polls WHERE id = ?', [req.params.id]);
|
||||
if (!p) return res.status(404).json({ error: '不存在' });
|
||||
res.json(p);
|
||||
});
|
||||
|
||||
router.post('/', authenticate, requireRole('owner'), async (req, res) => {
|
||||
const { title, description, options } = req.body;
|
||||
if (!title || !options || !Array.isArray(options) || options.length < 2) return res.status(400).json({ error: '标题和至少2个选项为必填' });
|
||||
const r = await query('INSERT INTO polls(title, description, options, created_by) VALUES (?,?,?,?)', [title, description||'', JSON.stringify(options), req.user.id]);
|
||||
res.status(201).json({ id: r.insertId, message: '创建成功' });
|
||||
});
|
||||
|
||||
router.put('/:id', authenticate, requireRole('owner'), async (req, res) => {
|
||||
const p = await getRow('SELECT * FROM polls WHERE id = ?', [req.params.id]);
|
||||
if (!p) return res.status(404).json({ error: '投票不存在' });
|
||||
const fields = {};
|
||||
if (req.body.title) fields.title = req.body.title;
|
||||
if (req.body.description !== undefined) fields.description = req.body.description;
|
||||
if (req.body.active !== undefined) fields.active = req.body.active ? 1 : 0;
|
||||
if (req.body.options) fields.options = JSON.stringify(req.body.options);
|
||||
if (!Object.keys(fields).length) return res.status(400).json({ error: '无更新内容' });
|
||||
const sets = Object.keys(fields).map(k => `${k} = ?`).join(', ');
|
||||
await query(`UPDATE polls 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 poll_votes WHERE poll_id = ?', [req.params.id]);
|
||||
await query('DELETE FROM polls WHERE id = ?', [req.params.id]);
|
||||
res.json({ message: '已删除' });
|
||||
});
|
||||
|
||||
router.post('/:id/vote', authenticate, async (req, res) => {
|
||||
const p = await getRow('SELECT * FROM polls WHERE id = ? AND active = 1', [req.params.id]);
|
||||
if (!p) return res.status(404).json({ error: '投票不存在或已关闭' });
|
||||
const voted = await getRow('SELECT id FROM poll_votes WHERE poll_id = ? AND user_id = ?', [p.id, req.user.id]);
|
||||
if (voted) return res.status(400).json({ error: '您已投过票' });
|
||||
const { option_index } = req.body;
|
||||
if (option_index === undefined || option_index === null) return res.status(400).json({ error: '请选择选项' });
|
||||
const opts = JSON.parse(p.options || '[]');
|
||||
if (option_index < 0 || option_index >= opts.length) return res.status(400).json({ error: '无效选项' });
|
||||
await query('INSERT INTO poll_votes(poll_id, user_id, option_index) VALUES (?,?,?)', [req.params.id, req.user.id, option_index]);
|
||||
res.json({ message: '投票成功' });
|
||||
});
|
||||
|
||||
module.exports = router;
|
||||
@@ -104,8 +104,6 @@ if (isInstalled()) {
|
||||
app.use('/api/export', methodGuard(['GET']), generalLimiter, require('./routes/export'));
|
||||
app.use('/api/uploads', methodGuard(['GET']), generalLimiter, require('./routes/uploads'));
|
||||
app.use('/api/notifications', methodGuard(['GET','POST','PUT','DELETE']), generalLimiter, require('./routes/notifications'));
|
||||
app.use('/api/polls', methodGuard(['GET','POST','PUT','DELETE']), generalLimiter, require('./routes/polls'));
|
||||
app.use('/api/features', methodGuard(['GET','POST','PUT','DELETE']), generalLimiter, require('./routes/features'));
|
||||
app.use('/api/external', require('./routes/external'));
|
||||
|
||||
app.get('/api/verify', (req, res) => res.redirect(`/#/verify?token=${req.query.token}`));
|
||||
|
||||
Reference in New Issue
Block a user