add admin interface with article and event management features
This commit is contained in:
+25
-28
@@ -14,54 +14,51 @@ async function eventRoutes(fastify) {
|
||||
fastify.get('/events', async (request, reply) => {
|
||||
const query = request.query || {};
|
||||
|
||||
const conditions = [];
|
||||
const params = [];
|
||||
|
||||
if (query.id) {
|
||||
const id = Number.parseInt(query.id, 10);
|
||||
if (!Number.isFinite(id)) {
|
||||
reply.code(400);
|
||||
return { error: 'id must be a number' };
|
||||
}
|
||||
|
||||
const event = db.prepare(`SELECT id, title, created_at FROM events WHERE id = ?`).get(id);
|
||||
if (!event) {
|
||||
reply.code(404);
|
||||
return { error: 'Event not found' };
|
||||
}
|
||||
|
||||
const articles = db.prepare(`
|
||||
SELECT id, title, description, content, url, normalized_title, source, pub_date, ingested_at
|
||||
FROM articles
|
||||
WHERE event_id = ?
|
||||
AND content IS NOT NULL AND content != ''
|
||||
AND is_index_page = 0
|
||||
ORDER BY pub_date_effective DESC, id DESC
|
||||
`).all(id);
|
||||
|
||||
return { ...event, articles };
|
||||
conditions.push('e.id = ?');
|
||||
params.push(id);
|
||||
}
|
||||
|
||||
const limit = parseLimit(query.limit);
|
||||
const offset = parseOffset(query.offset);
|
||||
|
||||
const SORT_COLUMNS = {
|
||||
created_at: 'e.created_at',
|
||||
pub_date: '(SELECT MIN(a.pub_date_effective) FROM articles a WHERE a.event_id = e.id AND a.content IS NOT NULL AND a.content != \'\' AND a.is_index_page = 0)',
|
||||
id: 'e.id',
|
||||
article_count: 'article_count',
|
||||
};
|
||||
|
||||
const sortBy = SORT_COLUMNS[query.sort_by] || SORT_COLUMNS.created_at;
|
||||
const sortBy = SORT_COLUMNS[query.sort_by] || SORT_COLUMNS.pub_date;
|
||||
const order = String(query.order || '').toLowerCase() === 'asc' ? 'ASC' : 'DESC';
|
||||
|
||||
return db.prepare(`
|
||||
SELECT e.id, e.title, e.created_at,
|
||||
COUNT(a.id) AS article_count
|
||||
const where = conditions.length ? `WHERE ${conditions.join(' AND ')}` : '';
|
||||
|
||||
const events = db.prepare(`
|
||||
SELECT e.id, e.title,
|
||||
(SELECT MIN(a.pub_date_effective) FROM articles a WHERE a.event_id = e.id AND a.content IS NOT NULL AND a.content != '' AND a.is_index_page = 0) AS pub_date
|
||||
FROM events e
|
||||
LEFT JOIN articles a ON a.event_id = e.id
|
||||
AND a.content IS NOT NULL AND a.content != ''
|
||||
AND a.is_index_page = 0
|
||||
GROUP BY e.id
|
||||
${where}
|
||||
ORDER BY ${sortBy} ${order}, e.id ${order}
|
||||
LIMIT ? OFFSET ?
|
||||
`).all(limit, offset);
|
||||
`).all(...params, limit, offset);
|
||||
|
||||
const getArticles = db.prepare(`
|
||||
SELECT id, title, description, content, url, normalized_title, source, pub_date, ingested_at
|
||||
FROM articles
|
||||
WHERE event_id = ?
|
||||
AND content IS NOT NULL AND content != ''
|
||||
AND is_index_page = 0
|
||||
ORDER BY pub_date_effective DESC, id DESC
|
||||
`);
|
||||
|
||||
return events.map(e => ({ ...e, articles: getArticles.all(e.id) }));
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user