57 lines
2.4 KiB
TypeScript
57 lines
2.4 KiB
TypeScript
import type { APIRoute } from 'astro';
|
|
import { getDb } from '../../../db/index';
|
|
import { generateDraftConfig } from '../../../services/file-generator';
|
|
|
|
export const prerender = false;
|
|
|
|
export const POST: APIRoute = async ({ request }) => {
|
|
try {
|
|
const { page_id, section_id, settings } = await request.json();
|
|
|
|
if (!page_id || !section_id || !settings) {
|
|
return new Response(JSON.stringify({ error: 'page_id, section_id und settings erforderlich' }), { status: 400 });
|
|
}
|
|
|
|
const db = getDb();
|
|
const existingRow = db.prepare('SELECT content_draft_json, styles_json FROM page_sections WHERE id = ?').get(section_id) as any;
|
|
|
|
if (!existingRow) {
|
|
return new Response(JSON.stringify({ error: 'Sektion nicht gefunden' }), { status: 404 });
|
|
}
|
|
|
|
const currentDraft = JSON.parse(existingRow.content_draft_json || '{}');
|
|
const currentStyles = JSON.parse(existingRow.styles_json || '{}');
|
|
|
|
// Trenne Styles (z. B. bg_color, accent_color) von typischem Inhalt
|
|
const { bg_color, accent_color, animation, backdrop_blur, liquid_edge, padding_top, padding_right, padding_bottom, padding_left, ...contentFields } = settings;
|
|
|
|
const updatedDraft = { ...currentDraft, ...contentFields };
|
|
const updatedStyles = {
|
|
...currentStyles,
|
|
...(bg_color !== undefined ? { bg_color } : {}),
|
|
...(accent_color !== undefined ? { accent_color } : {}),
|
|
...(animation !== undefined ? { animation } : {}),
|
|
...(backdrop_blur !== undefined ? { backdrop_blur } : {}),
|
|
...(liquid_edge !== undefined ? { liquid_edge } : {}),
|
|
...(padding_top !== undefined ? { padding_top } : {}),
|
|
...(padding_right !== undefined ? { padding_right } : {}),
|
|
...(padding_bottom !== undefined ? { padding_bottom } : {}),
|
|
...(padding_left !== undefined ? { padding_left } : {})
|
|
};
|
|
|
|
db.prepare('UPDATE page_sections SET content_draft_json = ?, styles_json = ? WHERE id = ?').run(
|
|
JSON.stringify(updatedDraft),
|
|
JSON.stringify(updatedStyles),
|
|
section_id
|
|
);
|
|
|
|
// Draft-Config neu kompilieren
|
|
await generateDraftConfig();
|
|
|
|
return new Response(JSON.stringify({ success: true, settings: { ...updatedDraft, ...updatedStyles } }), { status: 200 });
|
|
} catch (error) {
|
|
console.error('[API /api/editor/update-section-settings] Exception:', error);
|
|
return new Response(JSON.stringify({ error: 'Fehler beim Aktualisieren der Sektions-Einstellungen' }), { status: 500 });
|
|
}
|
|
};
|