Files
infohliadka/pages/api/search/advanced.ts

94 lines
2.7 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 {
q,
category,
risk_level_min,
risk_level_max,
status = 'verified',
page = '1',
limit = '20'
} = req.query
const dbPath = path.join(process.cwd(), "database", "antihoax.db")
const db = new sqlite3.Database(dbPath)
try {
let whereConditions = ["s.status = ?"]
let params: any[] = [status]
if (q) {
whereConditions.push("(s.domain LIKE ? OR s.title LIKE ? OR s.description LIKE ?)")
params.push(`%${q}%`, `%${q}%`, `%${q}%`)
}
if (category) {
whereConditions.push("EXISTS (SELECT 1 FROM source_categories sc JOIN categories c ON sc.category_id = c.id WHERE sc.source_id = s.id AND c.name = ?)")
params.push(category)
}
if (risk_level_min) {
whereConditions.push("s.risk_level >= ?")
params.push(parseInt(risk_level_min as string))
}
if (risk_level_max) {
whereConditions.push("s.risk_level <= ?")
params.push(parseInt(risk_level_max as string))
}
const offset = (parseInt(page as string) - 1) * parseInt(limit as string)
const query = `
SELECT s.*, GROUP_CONCAT(c.name) as categories,
COUNT(*) OVER() as total_count
FROM sources s
LEFT JOIN source_categories sc ON s.id = sc.source_id
LEFT JOIN categories c ON sc.category_id = c.id
WHERE ${whereConditions.join(' AND ')}
GROUP BY s.id
ORDER BY s.risk_level DESC, s.created_at DESC
LIMIT ? OFFSET ?
`
params.push(parseInt(limit as string), offset)
const results = await new Promise<any[]>((resolve, reject) => {
db.all(query, params, (err, rows) => {
if (err) reject(err)
else resolve(rows)
})
})
const total = results.length > 0 ? results[0].total_count : 0
const totalPages = Math.ceil(total / parseInt(limit as string))
res.json({
results: results.map(row => ({
id: row.id,
domain: row.domain,
title: row.title,
risk_level: row.risk_level,
categories: row.categories ? row.categories.split(',') : [],
description: row.description,
created_at: row.created_at
})),
pagination: {
page: parseInt(page as string),
limit: parseInt(limit as string),
total,
totalPages
}
})
} catch (error) {
res.status(500).json({ error: "Search failed" })
} finally {
db.close()
}
}