Noticias multiidioma (ES/EN) con fallback
home_noticia gana columnas titulo_en/contenido_en (sql/add_news_en.sql). getNoticias(locale) usa la versión EN si existe, si no cae al español (título y contenido independientes); la fecha también se formatea por idioma. El panel de admin permite escribir la traducción al crear y EDITAR noticias existentes (nuevo PUT /api/admin/news/[id], updateNews). Claves Admin nuevas (título/ contenido EN, editar/guardar). Probado: /en/ muestra EN, /es/ ES; PUT guardado (403 sin sesión). Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -4,30 +4,57 @@ 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<NewsItem[]> {
|
||||
const [rows] = await db(DB.default).query<RowDataPacket[]>(
|
||||
'SELECT id, titulo, fecha_publicacion, enlace FROM home_noticia ORDER BY fecha_publicacion DESC',
|
||||
'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): Promise<number> {
|
||||
export async function createNews(
|
||||
titulo: string,
|
||||
contenido: string,
|
||||
enlace: string | null,
|
||||
tituloEn: string | null,
|
||||
contenidoEn: string | null,
|
||||
): Promise<number> {
|
||||
const [res] = await db(DB.default).query<ResultSetHeader>(
|
||||
'INSERT INTO home_noticia (titulo, contenido, fecha_publicacion, enlace) VALUES (?, ?, NOW(), ?)',
|
||||
[titulo, contenido, enlace || null],
|
||||
'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<void> {
|
||||
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<void> {
|
||||
await db(DB.default).query('DELETE FROM home_noticia WHERE id = ?', [id])
|
||||
}
|
||||
|
||||
+10
-4
@@ -31,16 +31,22 @@ export function expansionFromGamebuild(gamebuild: number): string {
|
||||
return 'Unknown'
|
||||
}
|
||||
|
||||
export async function getNoticias(limit = 50): Promise<Noticia[]> {
|
||||
/**
|
||||
* Noticias localizadas. Con `locale='en'` usa `titulo_en`/`contenido_en` si
|
||||
* existen; si están vacías, cae a la versión en español (título y contenido de
|
||||
* forma independiente). El texto original (titulo/contenido) siempre es ES.
|
||||
*/
|
||||
export async function getNoticias(locale = 'es', limit = 50): Promise<Noticia[]> {
|
||||
const [rows] = await db(DB.default).query<RowDataPacket[]>(
|
||||
'SELECT id, titulo, contenido, fecha_publicacion, enlace FROM home_noticia ORDER BY fecha_publicacion DESC LIMIT ?',
|
||||
'SELECT id, titulo, titulo_en, contenido, contenido_en, fecha_publicacion, enlace FROM home_noticia ORDER BY fecha_publicacion DESC LIMIT ?',
|
||||
[limit],
|
||||
)
|
||||
const en = locale === 'en'
|
||||
return rows.map((r) => ({
|
||||
id: r.id,
|
||||
titulo: r.titulo,
|
||||
titulo: en && r.titulo_en ? r.titulo_en : r.titulo,
|
||||
fecha: r.fecha_publicacion ? new Date(r.fecha_publicacion).toISOString() : null,
|
||||
contenido: r.contenido,
|
||||
contenido: en && r.contenido_en ? r.contenido_en : r.contenido,
|
||||
enlace: r.enlace || null,
|
||||
}))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user