feat: add GET /api/votes endpoint with filtering and pagination
Made-with: Cursor
This commit is contained in:
@@ -254,6 +254,40 @@ router.get('/:id/games', (req, res) => {
|
||||
}
|
||||
});
|
||||
|
||||
// Get vote breakdown for a session
|
||||
router.get('/:id/votes', (req, res) => {
|
||||
try {
|
||||
const session = db.prepare('SELECT id FROM sessions WHERE id = ?').get(req.params.id);
|
||||
|
||||
if (!session) {
|
||||
return res.status(404).json({ error: 'Session not found' });
|
||||
}
|
||||
|
||||
const votes = db.prepare(`
|
||||
SELECT
|
||||
lv.game_id,
|
||||
g.title,
|
||||
g.pack_name,
|
||||
SUM(CASE WHEN lv.vote_type = 1 THEN 1 ELSE 0 END) AS upvotes,
|
||||
SUM(CASE WHEN lv.vote_type = -1 THEN 1 ELSE 0 END) AS downvotes,
|
||||
SUM(lv.vote_type) AS net_score,
|
||||
COUNT(*) AS total_votes
|
||||
FROM live_votes lv
|
||||
JOIN games g ON lv.game_id = g.id
|
||||
WHERE lv.session_id = ?
|
||||
GROUP BY lv.game_id
|
||||
ORDER BY net_score DESC
|
||||
`).all(req.params.id);
|
||||
|
||||
res.json({
|
||||
session_id: parseInt(req.params.id),
|
||||
votes,
|
||||
});
|
||||
} catch (error) {
|
||||
res.status(500).json({ error: error.message });
|
||||
}
|
||||
});
|
||||
|
||||
// Add game to session (admin only)
|
||||
router.post('/:id/games', authenticateToken, (req, res) => {
|
||||
try {
|
||||
|
||||
@@ -4,6 +4,87 @@ const db = require('../database');
|
||||
|
||||
const router = express.Router();
|
||||
|
||||
// Get vote history with filtering and pagination
|
||||
router.get('/', (req, res) => {
|
||||
try {
|
||||
let { session_id, game_id, username, vote_type, page, limit } = req.query;
|
||||
|
||||
page = parseInt(page) || 1;
|
||||
limit = Math.min(parseInt(limit) || 50, 100);
|
||||
if (page < 1) page = 1;
|
||||
if (limit < 1) limit = 50;
|
||||
const offset = (page - 1) * limit;
|
||||
|
||||
const where = [];
|
||||
const params = [];
|
||||
|
||||
if (session_id !== undefined) {
|
||||
const sid = parseInt(session_id);
|
||||
if (isNaN(sid)) {
|
||||
return res.status(400).json({ error: 'session_id must be an integer' });
|
||||
}
|
||||
where.push('lv.session_id = ?');
|
||||
params.push(sid);
|
||||
}
|
||||
|
||||
if (game_id !== undefined) {
|
||||
const gid = parseInt(game_id);
|
||||
if (isNaN(gid)) {
|
||||
return res.status(400).json({ error: 'game_id must be an integer' });
|
||||
}
|
||||
where.push('lv.game_id = ?');
|
||||
params.push(gid);
|
||||
}
|
||||
|
||||
if (username) {
|
||||
where.push('lv.username = ?');
|
||||
params.push(username);
|
||||
}
|
||||
|
||||
if (vote_type !== undefined) {
|
||||
if (vote_type !== 'up' && vote_type !== 'down') {
|
||||
return res.status(400).json({ error: 'vote_type must be "up" or "down"' });
|
||||
}
|
||||
where.push('lv.vote_type = ?');
|
||||
params.push(vote_type === 'up' ? 1 : -1);
|
||||
}
|
||||
|
||||
const whereClause = where.length > 0 ? 'WHERE ' + where.join(' AND ') : '';
|
||||
|
||||
const countResult = db.prepare(
|
||||
`SELECT COUNT(*) as total FROM live_votes lv ${whereClause}`
|
||||
).get(...params);
|
||||
|
||||
const total = countResult.total;
|
||||
const total_pages = Math.ceil(total / limit) || 0;
|
||||
|
||||
const votes = db.prepare(`
|
||||
SELECT
|
||||
lv.id,
|
||||
lv.session_id,
|
||||
lv.game_id,
|
||||
g.title AS game_title,
|
||||
g.pack_name,
|
||||
lv.username,
|
||||
CASE WHEN lv.vote_type = 1 THEN 'up' ELSE 'down' END AS vote_type,
|
||||
lv.timestamp,
|
||||
lv.created_at
|
||||
FROM live_votes lv
|
||||
JOIN games g ON lv.game_id = g.id
|
||||
${whereClause}
|
||||
ORDER BY lv.timestamp DESC
|
||||
LIMIT ? OFFSET ?
|
||||
`).all(...params, limit, offset);
|
||||
|
||||
res.json({
|
||||
votes,
|
||||
pagination: { page, limit, total, total_pages },
|
||||
});
|
||||
} catch (error) {
|
||||
res.status(500).json({ error: error.message });
|
||||
}
|
||||
});
|
||||
|
||||
// Live vote endpoint - receives real-time votes from bot
|
||||
router.post('/live', authenticateToken, (req, res) => {
|
||||
try {
|
||||
|
||||
Reference in New Issue
Block a user