import type { APIRoute } from 'astro'; import { getDb } from '../../../db/index'; import { generateDraftConfig } from '../../../services/file-generator'; export const prerender = false; // Standard-Templates const SECTION_TEMPLATES: Record = { HeroSection: { title: 'Neue Überschrift', subtitle: 'Beschreibungstext hier eingeben.', ctaPrimaryText: 'Jetzt anfragen', ctaPrimaryLink: '#contact' }, BentoGrid: { sectionTitle: 'Unsere Highlights', sectionSubtitle: 'Modernste Features im Überblick' }, ContactSection: { title: 'Kontaktieren Sie uns', subtitle: 'Wir antworten innerhalb von 24 Stunden.' } }; export const POST: APIRoute = async ({ request }) => { try { const { page_id, section_type } = await request.json(); if (!page_id || !section_type) { return new Response(JSON.stringify({ error: 'Gültige page_id und section_type erforderlich' }), { status: 400 }); } const db = getDb(); const secId = `sec_${section_type.toLowerCase()}_${Date.now()}`; const defaultContent = JSON.stringify(SECTION_TEMPLATES[section_type] || {}); // Aktuellen max order_index ermitteln const maxOrderRow = db.prepare('SELECT MAX(order_index) as max_idx FROM page_sections WHERE page_id = ?').get(page_id) as { max_idx: number | null }; const nextIdx = (maxOrderRow?.max_idx ?? -1) + 1; db.prepare('INSERT INTO page_sections (id, page_id, section_type, order_index, content_draft_json, content_published_json, styles_json) VALUES (?, ?, ?, ?, ?, ?, ?)').run( secId, page_id, section_type, nextIdx, defaultContent, defaultContent, JSON.stringify({}) ); // Draft-Config via File-Generator neu kompilieren await generateDraftConfig(); return new Response(JSON.stringify({ success: true, section_id: secId }), { status: 201 }); } catch (error) { return new Response(JSON.stringify({ error: 'Fehler beim Hinzufügen der Sektion' }), { status: 500 }); } };