Skip to content

Commit cace4ef

Browse files
committed
feat: add pagination
1 parent dc33ea7 commit cace4ef

2 files changed

Lines changed: 225 additions & 211 deletions

File tree

app.js

Lines changed: 49 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -568,25 +568,69 @@ app.get('/settings', isAuthenticated, async (req, res) => {
568568
app.get('/history', isAuthenticated, async (req, res) => {
569569
try {
570570
const db = require('./db/database').db;
571+
const page = parseInt(req.query.page) || 1;
572+
const limit = parseInt(req.query.limit) || 10;
573+
const sort = req.query.sort === 'oldest' ? 'ASC' : 'DESC';
574+
const platform = req.query.platform || 'all';
575+
const search = req.query.search || '';
576+
const offset = (page - 1) * limit;
577+
578+
let whereClause = 'WHERE h.user_id = ?';
579+
const params = [req.session.userId];
580+
581+
if (platform !== 'all') {
582+
whereClause += ' AND h.platform = ?';
583+
params.push(platform);
584+
}
585+
586+
if (search) {
587+
whereClause += ' AND h.title LIKE ?';
588+
params.push(`%${search}%`);
589+
}
590+
591+
const totalCount = await new Promise((resolve, reject) => {
592+
db.get(
593+
`SELECT COUNT(*) as count FROM stream_history h ${whereClause}`,
594+
params,
595+
(err, row) => {
596+
if (err) reject(err);
597+
else resolve(row.count);
598+
}
599+
);
600+
});
601+
571602
const history = await new Promise((resolve, reject) => {
572603
db.all(
573604
`SELECT h.*, v.thumbnail_path
574605
FROM stream_history h
575606
LEFT JOIN videos v ON h.video_id = v.id
576-
WHERE h.user_id = ?
577-
ORDER BY h.start_time DESC`,
578-
[req.session.userId],
607+
${whereClause}
608+
ORDER BY h.start_time ${sort}
609+
LIMIT ? OFFSET ?`,
610+
[...params, limit, offset],
579611
(err, rows) => {
580612
if (err) reject(err);
581613
else resolve(rows);
582614
}
583615
);
584616
});
617+
618+
const totalPages = Math.ceil(totalCount / limit);
619+
585620
res.render('history', {
586621
active: 'history',
587622
title: 'Stream History',
588623
history: history,
589-
helpers: app.locals.helpers
624+
helpers: app.locals.helpers,
625+
pagination: {
626+
page,
627+
limit,
628+
totalCount,
629+
totalPages,
630+
sort: req.query.sort || 'newest',
631+
platform,
632+
search
633+
}
590634
});
591635
} catch (error) {
592636
console.error('Error fetching stream history:', error);
@@ -2290,4 +2334,4 @@ const server = app.listen(port, '0.0.0.0', async () => {
22902334

22912335
server.timeout = 30 * 60 * 1000;
22922336
server.keepAliveTimeout = 30 * 60 * 1000;
2293-
server.headersTimeout = 30 * 60 * 1000;
2337+
server.headersTimeout = 30 * 60 * 1000;

0 commit comments

Comments
 (0)