feat: add filter, limit, and X-Total-Count to session list endpoint

Made-with: Cursor
This commit is contained in:
cottongin
2026-03-23 01:42:51 -04:00
parent 35474e5df4
commit 68045afbbc
2 changed files with 123 additions and 0 deletions

View File

@@ -21,18 +21,45 @@ function createMessageHash(username, message, timestamp) {
// Get all sessions
router.get('/', (req, res) => {
try {
const filter = req.query.filter || 'default';
const limitParam = req.query.limit || 'all';
let whereClause = '';
if (filter === 'default') {
whereClause = 'WHERE s.archived = 0';
} else if (filter === 'archived') {
whereClause = 'WHERE s.archived = 1';
}
const countRow = db.prepare(`
SELECT COUNT(DISTINCT s.id) as total
FROM sessions s
${whereClause}
`).get();
let limitClause = '';
if (limitParam !== 'all') {
const limitNum = parseInt(limitParam, 10);
if (!isNaN(limitNum) && limitNum > 0) {
limitClause = `LIMIT ${limitNum}`;
}
}
const sessions = db.prepare(`
SELECT
s.id,
s.created_at,
s.closed_at,
s.is_active,
s.archived,
s.notes,
COUNT(sg.id) as games_played
FROM sessions s
LEFT JOIN session_games sg ON s.id = sg.session_id
${whereClause}
GROUP BY s.id
ORDER BY s.created_at DESC
${limitClause}
`).all();
const result = sessions.map(({ notes, ...session }) => {
@@ -40,6 +67,7 @@ router.get('/', (req, res) => {
return { ...session, has_notes, notes_preview };
});
res.set('X-Total-Count', String(countRow.total));
res.json(result);
} catch (error) {
res.status(500).json({ error: error.message });