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:
@@ -0,0 +1,24 @@
|
||||
import { getTranslations, setRequestLocale } from 'next-intl/server'
|
||||
import { redirect } from '@/i18n/navigation'
|
||||
import { getSession } from '@/lib/session'
|
||||
import { isAdmin } from '@/lib/admin'
|
||||
import { listCategoriesWithForums } from '@/lib/admin-forum'
|
||||
import { AdminForumManager } from '@/components/AdminForumManager'
|
||||
|
||||
export const dynamic = 'force-dynamic'
|
||||
|
||||
export default async function AdminForumPage({ params }: { params: Promise<{ locale: string }> }) {
|
||||
const { locale } = await params
|
||||
setRequestLocale(locale)
|
||||
const t = await getTranslations('Admin')
|
||||
const session = await getSession()
|
||||
if (!session.bnetId) redirect({ href: '/login', locale })
|
||||
if (!(await isAdmin(session))) redirect({ href: '/', locale })
|
||||
const groups = await listCategoriesWithForums()
|
||||
return (
|
||||
<main className="mx-auto max-w-3xl px-4 py-8">
|
||||
<h1 className="mb-6 text-2xl font-bold text-amber-500">{t('forum')}</h1>
|
||||
<AdminForumManager initial={groups} />
|
||||
</main>
|
||||
)
|
||||
}
|
||||
@@ -20,6 +20,7 @@ export default async function AdminPage({ params }: { params: Promise<{ locale:
|
||||
{ href: '/admin/gold', label: t('manageGold'), icon: '🪙' },
|
||||
{ href: '/admin/users', label: t('manageUsers'), icon: '👤' },
|
||||
{ href: '/admin/recruit', label: t('manageRecruit'), icon: '🎁' },
|
||||
{ href: '/admin/forum', label: t('manageForum'), icon: '💬' },
|
||||
]
|
||||
|
||||
return (
|
||||
|
||||
@@ -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 })
|
||||
}
|
||||
@@ -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 })
|
||||
}
|
||||
Reference in New Issue
Block a user