# Review package: c3267c9..5ce10da ## Commits 5ce10da feat(content): guid in resolve/componenti + GET /api/admin/content/[tag] ## Files changed src/components/content/T.astro | 4 ++-- src/components/content/TImg.astro | 10 ++++++---- src/lib/content.ts | 16 ++++++++-------- src/pages/api/admin/content/[tag].ts | 15 +++++++++++++++ tests/content-api.test.ts | 13 +++++++++++++ tests/content.test.ts | 8 +++++++- 6 files changed, 51 insertions(+), 15 deletions(-) ## Diff diff --git a/src/components/content/T.astro b/src/components/content/T.astro index 41cefd3..b11d23e 100644 --- a/src/components/content/T.astro +++ b/src/components/content/T.astro @@ -1,11 +1,11 @@ --- import { resolveContent } from '../../lib/content'; interface Props { tag: string; as?: string; class?: string } const { tag, as = 'span', class: cls } = Astro.props; const Tag = as; const c = resolveContent(tag); const classes = [cls, c.classes].filter(Boolean).join(' ') || undefined; --- {c.type === 'html' - ? - : {c.value}} + ? + : {c.value}} diff --git a/src/components/content/TImg.astro b/src/components/content/TImg.astro index 3fcb915..b1f6ac3 100644 --- a/src/components/content/TImg.astro +++ b/src/components/content/TImg.astro @@ -1,14 +1,16 @@ --- import { Image } from 'astro:assets'; import type { ImageMetadata } from 'astro'; -import { getImageOverride } from '../../lib/content'; +import { resolveContent } from '../../lib/content'; interface Props { tag: string; src: ImageMetadata; alt: string; class?: string; loading?: 'lazy' | 'eager'; widths?: number[]; sizes?: string; height?: number; } const { tag, src, alt, class: cls, ...rest } = Astro.props; -const override = getImageOverride(tag); +const c = resolveContent(tag); +const override = c.type === 'image' && c.value ? `/uploads/${c.value}` : null; +const guid = c.guid || undefined; --- {override - ? {alt} - : {alt}} + ? {alt} + : {alt}} diff --git a/src/lib/content.ts b/src/lib/content.ts index dbdafee..db2a539 100644 --- a/src/lib/content.ts +++ b/src/lib/content.ts @@ -1,48 +1,48 @@ import type Database from 'better-sqlite3'; import { randomUUID } from 'node:crypto'; import { getDb } from './db'; import { contentSeed, seedByTag, type ContentType } from '../data/content-seed'; import { stylesToClasses } from './style-presets'; -interface Entry { value: string; type: ContentType; styles: Record } +interface Entry { value: string; type: ContentType; styles: Record; guid: string } export function syncSeed(db: Database.Database): void { const ins = db.prepare('INSERT OR IGNORE INTO content_blocks (tag, type, value, guid) VALUES (?, ?, ?, ?)'); const tx = db.transaction(() => { for (const e of contentSeed) ins.run(e.tag, e.type, e.value, randomUUID()); }); tx(); } export function makeContentStore(dbGetter: () => Database.Database) { let cache: Map | null = null; const load = (): Map => { if (cache) return cache; - const rows = dbGetter().prepare('SELECT tag, type, value, styles FROM content_blocks').all() as - { tag: string; type: ContentType; value: string; styles: string }[]; + const rows = dbGetter().prepare('SELECT tag, type, value, styles, guid FROM content_blocks').all() as + { tag: string; type: ContentType; value: string; styles: string; guid: string | null }[]; cache = new Map(rows.map((r) => { let styles: Record = {}; try { styles = JSON.parse(r.styles); } catch { /* styles corrotto: si ignora */ } if (typeof styles !== 'object' || styles === null) styles = {}; - return [r.tag, { value: r.value, type: r.type, styles }]; + return [r.tag, { value: r.value, type: r.type, styles, guid: r.guid ?? '' }]; })); return cache; }; return { - resolve(tag: string): { value: string; type: ContentType; classes: string } { + resolve(tag: string): { value: string; type: ContentType; classes: string; guid: string } { const hit = load().get(tag); - if (hit) return { value: hit.value, type: hit.type, classes: stylesToClasses(hit.styles) }; + if (hit) return { value: hit.value, type: hit.type, classes: stylesToClasses(hit.styles), guid: hit.guid }; const seed = seedByTag.get(tag); - if (seed) return { value: seed.value, type: seed.type, classes: '' }; + if (seed) return { value: seed.value, type: seed.type, classes: '', guid: '' }; console.warn(`[content] tag sconosciuto: ${tag}`); - return { value: '', type: 'text', classes: '' }; + return { value: '', type: 'text', classes: '', guid: '' }; }, invalidate(): void { cache = null; }, }; } const store = makeContentStore(getDb); export const resolveContent = (tag: string) => store.resolve(tag); export const t = (tag: string): string => store.resolve(tag).value; export const invalidateContentCache = (): void => store.invalidate(); diff --git a/src/pages/api/admin/content/[tag].ts b/src/pages/api/admin/content/[tag].ts index 1fd7c0b..8ad2488 100644 --- a/src/pages/api/admin/content/[tag].ts +++ b/src/pages/api/admin/content/[tag].ts @@ -1,19 +1,34 @@ import type { APIRoute } from 'astro'; import { getDb } from '../../../../lib/db'; import { applyContentUpdate } from '../../../../lib/content-update'; import { invalidateContentCache } from '../../../../lib/content'; +import { seedByTag } from '../../../../data/content-seed'; export const prerender = false; const json = (status: number, body: object) => new Response(JSON.stringify(body), { status, headers: { 'Content-Type': 'application/json' } }); +export const GET: APIRoute = ({ params }) => { + const tag = params.tag ?? ''; + const row = getDb().prepare('SELECT tag, type, value, guid FROM content_blocks WHERE tag = ?').get(tag) as + { tag: string; type: string; value: string; guid: string | null } | undefined; + if (!row) return json(404, { error: 'Tag inesistente.' }); + return json(200, { + tag: row.tag, + guid: row.guid ?? '', + type: row.type, + value: row.value, + styleOptions: seedByTag.get(row.tag)?.styleOptions ?? [], + }); +}; + export const PUT: APIRoute = async ({ params, request, locals }) => { let data: Record; try { data = await request.json(); } catch { return json(400, { error: 'Dati non validi.' }); } if (typeof data !== 'object' || data === null) return json(400, { error: 'Dati non validi.' }); const r = applyContentUpdate(getDb(), params.tag ?? '', data, locals.user!.username); if (!r.ok) return json(r.status, { error: r.error }); invalidateContentCache(); // Il value salvato può differire da quello inviato (sanitizeInline sui tag html): // lo restituiamo così il pannello mostra il dato reale. const row = getDb().prepare('SELECT value FROM content_blocks WHERE tag = ?').get(params.tag) as { value: string }; diff --git a/tests/content-api.test.ts b/tests/content-api.test.ts index 6d61853..66075dc 100644 --- a/tests/content-api.test.ts +++ b/tests/content-api.test.ts @@ -28,10 +28,23 @@ describe('applyContentUpdate', () => { if (htmlTag) { applyContentUpdate(db, htmlTag, { value: 'ciao ok' }, 'a'); const row = db.prepare('SELECT value FROM content_blocks WHERE tag = ?').get(htmlTag) as any; expect(row.value).not.toContain('