import type { RowDataPacket, ResultSetHeader } from 'mysql2' import { db, DB } from './db' export interface NewsItem { id: number titulo: string fecha: string | null enlace: string | null } export async function listNews(): Promise { const [rows] = await db(DB.default).query( 'SELECT id, titulo, fecha_publicacion, enlace FROM home_noticia ORDER BY fecha_publicacion DESC', ) return rows.map((r) => ({ id: r.id, titulo: r.titulo, 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): Promise { const [res] = await db(DB.default).query( 'INSERT INTO home_noticia (titulo, contenido, fecha_publicacion, enlace) VALUES (?, ?, NOW(), ?)', [titulo, contenido, enlace || null], ) return res.insertId } export async function deleteNews(id: number): Promise { await db(DB.default).query('DELETE FROM home_noticia WHERE id = ?', [id]) }