import type { RowDataPacket, ResultSetHeader } from 'mysql2' import { db, DB } from './db' // Gestión del foro para el admin, sobre el esquema FusionCMS (ver sql/forum_fusion.sql): // forum_categories(id, order, name) // forum_forums(id, category, order, name, description, icon, colortitle, type) // No hay columnas _en / visibility / deleted: las categorías y foros son datos del // seed, editables aquí. Borrar un foro es un DELETE real (se rechaza si tiene temas). export interface AdminCategory { id: number name: string order: number } export interface AdminForum { id: number category: number name: string description: string icon: string | null colortitle: string | null type: number order: number } export async function listCategoriesWithForums(): Promise< { category: AdminCategory; forums: AdminForum[] }[] > { const [cats] = await db(DB.web).query( 'SELECT id, name, `order` FROM forum_categories ORDER BY `order`, id', ) const [forums] = await db(DB.web).query( 'SELECT id, category, name, description, icon, colortitle, type, `order` FROM forum_forums ORDER BY `order`, id', ) return cats.map((c) => ({ category: { id: c.id, name: c.name, order: c.order }, forums: forums .filter((f) => f.category === c.id) .map((f) => ({ id: f.id, category: f.category, name: f.name, description: f.description, icon: f.icon, colortitle: f.colortitle, type: f.type, order: f.order, })), })) } export async function createCategory(name: string, order: number): Promise { const [res] = await db(DB.web).query( 'INSERT INTO forum_categories (name, `order`) VALUES (?, ?)', [name.slice(0, 45), order], ) return res.insertId } export async function updateCategory(id: number, name: string, order: number): Promise { await db(DB.web).query('UPDATE forum_categories SET name = ?, `order` = ? WHERE id = ?', [ name.slice(0, 45), order, id, ]) } /** Borra una categoría solo si no tiene foros. */ export async function deleteCategory(id: number): Promise { const [f] = await db(DB.web).query( 'SELECT COUNT(*) AS n FROM forum_forums WHERE category = ?', [id], ) if (Number(f[0]?.n ?? 0) > 0) return false await db(DB.web).query('DELETE FROM forum_categories WHERE id = ?', [id]) return true } export interface ForumInput { category: number name: string description: string icon: string | null colortitle: string | null type: number order: number } export async function createForum(f: ForumInput): Promise { const [res] = await db(DB.web).query( 'INSERT INTO forum_forums (category, name, description, icon, colortitle, type, `order`) VALUES (?, ?, ?, ?, ?, ?, ?)', [f.category, f.name.slice(0, 45), f.description, f.icon || null, f.colortitle || null, f.type, f.order], ) return res.insertId } export async function updateForum(id: number, f: ForumInput): Promise { await db(DB.web).query( 'UPDATE forum_forums SET category = ?, name = ?, description = ?, icon = ?, colortitle = ?, type = ?, `order` = ? WHERE id = ?', [f.category, f.name.slice(0, 45), f.description, f.icon || null, f.colortitle || null, f.type, f.order, id], ) } /** Borra un foro solo si no tiene temas. */ export async function deleteForum(id: number): Promise { const [t] = await db(DB.web).query('SELECT COUNT(*) AS n FROM forum_topics WHERE forum = ?', [id]) if (Number(t[0]?.n ?? 0) > 0) return false await db(DB.web).query('DELETE FROM forum_forums WHERE id = ?', [id]) return true }