feat: feature voting (PCL-style), poll owner edit/delete, blind polls for all

This commit is contained in:
2026-07-14 01:17:56 +08:00
parent 74974864cd
commit ee2f33c61f
8 changed files with 195 additions and 8 deletions

View File

@@ -0,0 +1,51 @@
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;

View File

@@ -4,9 +4,10 @@ const { authenticate, requireRole } = require('../middleware/auth');
const router = express.Router();
router.get('/', authenticate, async (req, res) => {
const polls = await query('SELECT id, title, description, active, created_at FROM polls ORDER BY created_at DESC');
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.get('/active', authenticate, async (req, res) => {