categories management

This commit is contained in:
2024-04-25 14:18:47 +02:00
parent 3af6013dbb
commit 54d91baecd
3 changed files with 203 additions and 0 deletions

View File

@@ -0,0 +1,39 @@
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 !== 'PATCH') {
return res.status(405).json({ error: 'Method not allowed' })
}
const { id } = req.query
const { is_active } = req.body
const dbPath = path.join(process.cwd(), 'database', 'antihoax.db')
const db = new sqlite3.Database(dbPath)
try {
await new Promise<void>((resolve, reject) => {
db.run(
'UPDATE categories SET is_active = ?, updated_at = CURRENT_TIMESTAMP WHERE id = ?',
[is_active, id],
function(err) {
if (err) reject(err)
else resolve()
}
)
})
return res.status(200).json({ success: true })
} catch (error) {
console.error('Database error:', error)
return res.status(500).json({ error: 'Internal server error' })
} finally {
db.close()
}
}

View File

@@ -0,0 +1,35 @@
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 dbPath = path.join(process.cwd(), 'database', 'antihoax.db')
const db = new sqlite3.Database(dbPath)
try {
const categories = await new Promise<any[]>((resolve, reject) => {
db.all(
'SELECT * FROM categories ORDER BY priority DESC, name ASC',
(err, rows) => {
if (err) reject(err)
else resolve(rows)
}
)
})
return res.status(200).json(categories)
} catch (error) {
console.error('Database error:', error)
return res.status(500).json({ error: 'Internal server error' })
} finally {
db.close()
}
}