import type { NextApiRequest, NextApiResponse } from 'next' import sqlite3 from 'sqlite3' import path from 'path' export default async function handler( req: NextApiRequest, res: NextApiResponse ) { if (req.method !== 'GET') { return res.status(405).json({ error: 'Method not allowed' }) } const { status = 'pending', page = '1', limit = '20' } = req.query const dbPath = path.join(process.cwd(), 'database', 'antihoax.db') const db = new sqlite3.Database(dbPath) try { const sources = await new Promise((resolve, reject) => { const offset = (parseInt(page as string) - 1) * parseInt(limit as string) db.all( `SELECT * FROM sources WHERE status = ? ORDER BY created_at DESC LIMIT ? OFFSET ?`, [status, parseInt(limit as string), offset], (err, rows) => { if (err) reject(err) else resolve(rows) } ) }) return res.status(200).json(sources) } catch (error) { console.error('Database error:', error) return res.status(500).json({ error: 'Internal server error' }) } finally { db.close() } }