Add pagination to feed list
This commit is contained in:
@ -339,6 +339,65 @@ export async function fetchActiveFeeds(): Promise<Feed[]> {
|
||||
return getAllFeeds();
|
||||
}
|
||||
|
||||
// Get paginated active feeds with total count
|
||||
export async function fetchActiveFeedsPaginated(
|
||||
page: number = 1,
|
||||
limit: number = 10,
|
||||
category?: string
|
||||
): Promise<{ feeds: Feed[]; total: number; page: number; limit: number; totalPages: number }> {
|
||||
try {
|
||||
const offset = (page - 1) * limit;
|
||||
|
||||
// Build query conditions
|
||||
let whereCondition = "WHERE active = 1";
|
||||
const params: any[] = [];
|
||||
|
||||
if (category) {
|
||||
whereCondition += " AND category = ?";
|
||||
params.push(category);
|
||||
}
|
||||
|
||||
// Get total count
|
||||
const countStmt = db.prepare(`SELECT COUNT(*) as count FROM feeds ${whereCondition}`);
|
||||
const countResult = countStmt.get(...params) as { count: number };
|
||||
const total = countResult.count;
|
||||
|
||||
// Get paginated feeds
|
||||
const feedsStmt = db.prepare(`
|
||||
SELECT * FROM feeds
|
||||
${whereCondition}
|
||||
ORDER BY created_at DESC
|
||||
LIMIT ? OFFSET ?
|
||||
`);
|
||||
|
||||
const rows = feedsStmt.all(...params, limit, offset) as any[];
|
||||
|
||||
const feeds = rows.map((row) => ({
|
||||
id: row.id,
|
||||
url: row.url,
|
||||
title: row.title,
|
||||
description: row.description,
|
||||
category: row.category,
|
||||
lastUpdated: row.last_updated,
|
||||
createdAt: row.created_at,
|
||||
active: Boolean(row.active),
|
||||
}));
|
||||
|
||||
const totalPages = Math.ceil(total / limit);
|
||||
|
||||
return {
|
||||
feeds,
|
||||
total,
|
||||
page,
|
||||
limit,
|
||||
totalPages
|
||||
};
|
||||
} catch (error) {
|
||||
console.error("Error getting paginated feeds:", error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
// Get episodes with feed information for enhanced display
|
||||
export async function fetchEpisodesWithFeedInfo(): Promise<
|
||||
EpisodeWithFeedInfo[]
|
||||
|
Reference in New Issue
Block a user