Admin: gestión de foros (categorías y foros)

- lib/admin-forum.ts: listCategoriesWithForums, createCategory/deleteCategory
  (solo si vacía), createForum/deleteForum (borrado lógico), setForumVisibility.
- API: /api/admin/forum(+/[id] DELETE/PATCH visibilidad) y
  /api/admin/forum/category(+/[id]) (403 sin admin).
- UI: /admin/forum (AdminForumManager) — alta de categorías, alta/borrado de foros
  por categoría, mostrar/ocultar; tarjeta "Gestionar foros" en el índice del admin.
- i18n es/en (Admin): 13 claves nuevas.

Verificado: build OK, /admin/forum 307->login, APIs 403 sin admin.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-07-13 01:49:52 +00:00
parent af2438399d
commit 8bb18838ff
10 changed files with 339 additions and 2 deletions
@@ -0,0 +1,21 @@
import { getSession } from '@/lib/session'
import { isAdmin } from '@/lib/admin'
import { deleteForum, setForumVisibility } from '@/lib/admin-forum'
export async function DELETE(_request: Request, { params }: { params: Promise<{ id: string }> }) {
const session = await getSession()
if (!(await isAdmin(session))) return Response.json({ success: false, error: 'forbidden' }, { status: 403 })
const { id } = await params
await deleteForum(Number(id))
return Response.json({ success: true })
}
export async function PATCH(request: Request, { params }: { params: Promise<{ id: string }> }) {
const session = await getSession()
if (!(await isAdmin(session))) return Response.json({ success: false, error: 'forbidden' }, { status: 403 })
const { id } = await params
let b: Record<string, unknown> = {}
try { b = await request.json() } catch { return Response.json({ success: false, error: 'invalidRequest' }, { status: 400 }) }
await setForumVisibility(Number(id), b.visibility !== false)
return Response.json({ success: true })
}
@@ -0,0 +1,11 @@
import { getSession } from '@/lib/session'
import { isAdmin } from '@/lib/admin'
import { deleteCategory } from '@/lib/admin-forum'
export async function DELETE(_request: Request, { params }: { params: Promise<{ id: string }> }) {
const session = await getSession()
if (!(await isAdmin(session))) return Response.json({ success: false, error: 'forbidden' }, { status: 403 })
const { id } = await params
const ok = await deleteCategory(Number(id))
return Response.json({ success: ok, error: ok ? undefined : 'categoryNotEmpty' })
}
@@ -0,0 +1,15 @@
import { getSession } from '@/lib/session'
import { isAdmin } from '@/lib/admin'
import { createCategory } from '@/lib/admin-forum'
export async function POST(request: Request) {
const session = await getSession()
if (!(await isAdmin(session))) return Response.json({ success: false, error: 'forbidden' }, { status: 403 })
let b: Record<string, unknown> = {}
try { b = await request.json() } catch { return Response.json({ success: false, error: 'invalidRequest' }, { status: 400 }) }
const name = String(b.name ?? '').trim()
const order = Number(b.order) || 0
if (!name) return Response.json({ success: false, error: 'emptyError' })
const id = await createCategory(name, order)
return Response.json({ success: true, id })
}
+18
View File
@@ -0,0 +1,18 @@
import { getSession } from '@/lib/session'
import { isAdmin } from '@/lib/admin'
import { createForum } from '@/lib/admin-forum'
export async function POST(request: Request) {
const session = await getSession()
if (!(await isAdmin(session))) return Response.json({ success: false, error: 'forbidden' }, { status: 403 })
let b: Record<string, unknown> = {}
try { b = await request.json() } catch { return Response.json({ success: false, error: 'invalidRequest' }, { status: 400 }) }
const categoryId = Number(b.categoryId)
const name = String(b.name ?? '').trim()
const description = String(b.description ?? '').trim()
const order = Number(b.order) || 0
const visibility = b.visibility === false ? 0 : 1
if (!categoryId || !name) return Response.json({ success: false, error: 'emptyError' })
const id = await createForum(categoryId, name, description, order, visibility)
return Response.json({ success: true, id })
}