feat: add offset pagination and X-Prev-Last-Date header to GET /sessions
Made-with: Cursor
This commit is contained in:
@@ -23,6 +23,9 @@ router.get('/', (req, res) => {
|
||||
try {
|
||||
const filter = req.query.filter || 'default';
|
||||
const limitParam = req.query.limit || 'all';
|
||||
const offsetParam = req.query.offset || '0';
|
||||
let offset = parseInt(offsetParam, 10);
|
||||
if (isNaN(offset) || offset < 0) offset = 0;
|
||||
|
||||
let whereClause = '';
|
||||
if (filter === 'default') {
|
||||
@@ -45,6 +48,11 @@ router.get('/', (req, res) => {
|
||||
}
|
||||
}
|
||||
|
||||
let offsetClause = '';
|
||||
if (offset > 0) {
|
||||
offsetClause = `OFFSET ${offset}`;
|
||||
}
|
||||
|
||||
const sessions = db.prepare(`
|
||||
SELECT
|
||||
s.id,
|
||||
@@ -60,6 +68,7 @@ router.get('/', (req, res) => {
|
||||
GROUP BY s.id
|
||||
ORDER BY s.created_at DESC
|
||||
${limitClause}
|
||||
${offsetClause}
|
||||
`).all();
|
||||
|
||||
const result = sessions.map(({ notes, ...session }) => {
|
||||
@@ -69,6 +78,19 @@ router.get('/', (req, res) => {
|
||||
|
||||
const absoluteTotal = db.prepare('SELECT COUNT(*) as total FROM sessions').get();
|
||||
|
||||
if (offset > 0) {
|
||||
const prevRow = db.prepare(`
|
||||
SELECT s.created_at
|
||||
FROM sessions s
|
||||
${whereClause}
|
||||
ORDER BY s.created_at DESC
|
||||
LIMIT 1 OFFSET ${offset - 1}
|
||||
`).get();
|
||||
if (prevRow) {
|
||||
res.set('X-Prev-Last-Date', prevRow.created_at);
|
||||
}
|
||||
}
|
||||
|
||||
res.set('X-Total-Count', String(countRow.total));
|
||||
res.set('X-Absolute-Total', String(absoluteTotal.total));
|
||||
res.json(result);
|
||||
|
||||
Reference in New Issue
Block a user