55 lines
1.6 KiB
TypeScript
55 lines
1.6 KiB
TypeScript
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 reports = await new Promise<any[]>((resolve, reject) => {
|
|
const offset = (parseInt(page as string) - 1) * parseInt(limit as string)
|
|
|
|
db.all(
|
|
`SELECT *,
|
|
CASE
|
|
WHEN category_suggestions IS NOT NULL
|
|
THEN json_extract(category_suggestions, '$')
|
|
ELSE '[]'
|
|
END as category_suggestions
|
|
FROM reports
|
|
WHERE status = ?
|
|
ORDER BY created_at DESC
|
|
LIMIT ? OFFSET ?`,
|
|
[status, parseInt(limit as string), offset],
|
|
(err, rows: any[]) => {
|
|
if (err) reject(err)
|
|
else {
|
|
const processedRows = rows.map(row => ({
|
|
...row,
|
|
category_suggestions: row.category_suggestions ? JSON.parse(row.category_suggestions) : []
|
|
}))
|
|
resolve(processedRows)
|
|
}
|
|
}
|
|
)
|
|
})
|
|
|
|
return res.status(200).json(reports)
|
|
|
|
} catch (error) {
|
|
console.error('Database error:', error)
|
|
return res.status(500).json({ error: 'Internal server error' })
|
|
} finally {
|
|
db.close()
|
|
}
|
|
} |