import type { RowDataPacket, ResultSetHeader } from 'mysql2' import { db, DB } from './db' export interface NewsItem { id: number titulo: string tituloEn: string contenido: string contenidoEn: string fecha: string | null enlace: string | null } export async function listNews(): Promise { const [rows] = await db(DB.default).query( 'SELECT id, titulo, titulo_en, contenido, contenido_en, fecha_publicacion, enlace FROM home_noticia ORDER BY fecha_publicacion DESC', ) return rows.map((r) => ({ id: r.id, titulo: r.titulo, tituloEn: r.titulo_en || '', contenido: r.contenido, contenidoEn: r.contenido_en || '', fecha: r.fecha_publicacion ? new Date(r.fecha_publicacion).toISOString() : null, enlace: r.enlace || null, })) } export async function createNews( titulo: string, contenido: string, enlace: string | null, tituloEn: string | null, contenidoEn: string | null, ): Promise { const [res] = await db(DB.default).query( 'INSERT INTO home_noticia (titulo, titulo_en, contenido, contenido_en, fecha_publicacion, enlace) VALUES (?, ?, ?, ?, NOW(), ?)', [titulo, tituloEn || null, contenido, contenidoEn || null, enlace || null], ) return res.insertId } /** Actualiza una noticia (incluye la traducción al inglés). */ export async function updateNews( id: number, titulo: string, contenido: string, enlace: string | null, tituloEn: string | null, contenidoEn: string | null, ): Promise { await db(DB.default).query( 'UPDATE home_noticia SET titulo = ?, titulo_en = ?, contenido = ?, contenido_en = ?, enlace = ? WHERE id = ?', [titulo, tituloEn || null, contenido, contenidoEn || null, enlace || null, id], ) } export async function deleteNews(id: number): Promise { await db(DB.default).query('DELETE FROM home_noticia WHERE id = ?', [id]) }