mirror of
https://github.com/xtr-dev/rondevu-server.git
synced 2025-12-12 03:43:23 +00:00
Add startsWith filter to topics endpoint
Added optional startsWith parameter to GET /topics endpoint: - Filters topics by prefix using SQL LIKE operator - Updated storage interface and implementations (SQLite & D1) - Added query param documentation - Returns startsWith in response when used Version bumped to 0.1.1 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
@@ -296,32 +296,51 @@ export class D1Storage implements Storage {
|
||||
return candidates;
|
||||
}
|
||||
|
||||
async getTopics(limit: number, offset: number): Promise<{
|
||||
async getTopics(limit: number, offset: number, startsWith?: string): Promise<{
|
||||
topics: TopicInfo[];
|
||||
total: number;
|
||||
}> {
|
||||
const now = Date.now();
|
||||
|
||||
// Build WHERE clause for startsWith filter
|
||||
const whereClause = startsWith
|
||||
? 'o.expires_at > ? AND ot.topic LIKE ?'
|
||||
: 'o.expires_at > ?';
|
||||
|
||||
const startsWithPattern = startsWith ? `${startsWith}%` : null;
|
||||
|
||||
// Get total count of topics with active offers
|
||||
const countResult = await this.db.prepare(`
|
||||
const countQuery = `
|
||||
SELECT COUNT(DISTINCT ot.topic) as count
|
||||
FROM offer_topics ot
|
||||
INNER JOIN offers o ON ot.offer_id = o.id
|
||||
WHERE o.expires_at > ?
|
||||
`).bind(Date.now()).first();
|
||||
WHERE ${whereClause}
|
||||
`;
|
||||
|
||||
const countStmt = this.db.prepare(countQuery);
|
||||
const countResult = startsWith
|
||||
? await countStmt.bind(now, startsWithPattern).first()
|
||||
: await countStmt.bind(now).first();
|
||||
|
||||
const total = (countResult as any)?.count || 0;
|
||||
|
||||
// Get topics with peer counts (paginated)
|
||||
const topicsResult = await this.db.prepare(`
|
||||
const topicsQuery = `
|
||||
SELECT
|
||||
ot.topic,
|
||||
COUNT(DISTINCT o.peer_id) as active_peers
|
||||
FROM offer_topics ot
|
||||
INNER JOIN offers o ON ot.offer_id = o.id
|
||||
WHERE o.expires_at > ?
|
||||
WHERE ${whereClause}
|
||||
GROUP BY ot.topic
|
||||
ORDER BY active_peers DESC, ot.topic ASC
|
||||
LIMIT ? OFFSET ?
|
||||
`).bind(Date.now(), limit, offset).all();
|
||||
`;
|
||||
|
||||
const topicsStmt = this.db.prepare(topicsQuery);
|
||||
const topicsResult = startsWith
|
||||
? await topicsStmt.bind(now, startsWithPattern, limit, offset).all()
|
||||
: await topicsStmt.bind(now, limit, offset).all();
|
||||
|
||||
const topics = (topicsResult.results || []).map((row: any) => ({
|
||||
topic: row.topic,
|
||||
|
||||
@@ -305,35 +305,50 @@ export class SQLiteStorage implements Storage {
|
||||
}));
|
||||
}
|
||||
|
||||
async getTopics(limit: number, offset: number): Promise<{
|
||||
async getTopics(limit: number, offset: number, startsWith?: string): Promise<{
|
||||
topics: TopicInfo[];
|
||||
total: number;
|
||||
}> {
|
||||
const now = Date.now();
|
||||
|
||||
// Build WHERE clause for startsWith filter
|
||||
const whereClause = startsWith
|
||||
? 'o.expires_at > ? AND ot.topic LIKE ?'
|
||||
: 'o.expires_at > ?';
|
||||
|
||||
const startsWithPattern = startsWith ? `${startsWith}%` : null;
|
||||
|
||||
// Get total count of topics with active offers
|
||||
const countStmt = this.db.prepare(`
|
||||
const countQuery = `
|
||||
SELECT COUNT(DISTINCT ot.topic) as count
|
||||
FROM offer_topics ot
|
||||
INNER JOIN offers o ON ot.offer_id = o.id
|
||||
WHERE o.expires_at > ?
|
||||
`);
|
||||
WHERE ${whereClause}
|
||||
`;
|
||||
|
||||
const countRow = countStmt.get(Date.now()) as any;
|
||||
const countStmt = this.db.prepare(countQuery);
|
||||
const countParams = startsWith ? [now, startsWithPattern] : [now];
|
||||
const countRow = countStmt.get(...countParams) as any;
|
||||
const total = countRow.count;
|
||||
|
||||
// Get topics with peer counts (paginated)
|
||||
const topicsStmt = this.db.prepare(`
|
||||
const topicsQuery = `
|
||||
SELECT
|
||||
ot.topic,
|
||||
COUNT(DISTINCT o.peer_id) as active_peers
|
||||
FROM offer_topics ot
|
||||
INNER JOIN offers o ON ot.offer_id = o.id
|
||||
WHERE o.expires_at > ?
|
||||
WHERE ${whereClause}
|
||||
GROUP BY ot.topic
|
||||
ORDER BY active_peers DESC, ot.topic ASC
|
||||
LIMIT ? OFFSET ?
|
||||
`);
|
||||
`;
|
||||
|
||||
const rows = topicsStmt.all(Date.now(), limit, offset) as any[];
|
||||
const topicsStmt = this.db.prepare(topicsQuery);
|
||||
const topicsParams = startsWith
|
||||
? [now, startsWithPattern, limit, offset]
|
||||
: [now, limit, offset];
|
||||
const rows = topicsStmt.all(...topicsParams) as any[];
|
||||
|
||||
const topics = rows.map(row => ({
|
||||
topic: row.topic,
|
||||
|
||||
@@ -146,9 +146,10 @@ export interface Storage {
|
||||
* Retrieves topics with active peer counts (paginated)
|
||||
* @param limit Maximum number of topics to return
|
||||
* @param offset Number of topics to skip
|
||||
* @param startsWith Optional prefix filter - only return topics starting with this string
|
||||
* @returns Object with topics array and total count
|
||||
*/
|
||||
getTopics(limit: number, offset: number): Promise<{
|
||||
getTopics(limit: number, offset: number, startsWith?: string): Promise<{
|
||||
topics: TopicInfo[];
|
||||
total: number;
|
||||
}>;
|
||||
|
||||
Reference in New Issue
Block a user