feat: implement DB-driven architecture with SQLite persistence and file-generator compilation service
All checks were successful
Production Build & Deploy / build-and-deploy (push) Successful in 50s

This commit is contained in:
Daniel S
2026-08-10 00:09:10 +02:00
parent 950497311c
commit db2f607e5a
12 changed files with 375 additions and 306 deletions

View File

@@ -1,11 +1,9 @@
import type { APIRoute } from 'astro';
import fs from 'node:fs/promises';
import path from 'node:path';
import { getDb } from '../../../db/index';
import { generateDraftConfig } from '../../../services/file-generator';
export const prerender = false;
const DATA_DIR = process.env.DATA_DIR || path.join(process.cwd(), 'app', 'data');
export const POST: APIRoute = async ({ request }) => {
try {
const { page_id, section_id, settings } = await request.json();
@@ -14,35 +12,43 @@ export const POST: APIRoute = async ({ request }) => {
return new Response(JSON.stringify({ error: 'page_id, section_id und settings erforderlich' }), { status: 400 });
}
const draftPath = path.join(DATA_DIR, 'site.config.draft.json');
const livePath = path.join(DATA_DIR, 'site.config.json');
const db = getDb();
const existingRow = db.prepare('SELECT content_draft_json, styles_json FROM page_sections WHERE id = ?').get(section_id) as any;
let configRaw: string;
try {
configRaw = await fs.readFile(draftPath, 'utf-8');
} catch {
configRaw = await fs.readFile(livePath, 'utf-8');
}
const config = JSON.parse(configRaw);
const targetPage = config.pages?.find((p: any) => p.id === page_id);
if (!targetPage) {
return new Response(JSON.stringify({ error: 'Seite nicht gefunden' }), { status: 404 });
}
// Sektion finden und Einstellungen aktualisieren
const targetSection = targetPage.sections?.find((sec: any) => sec.id === section_id);
if (!targetSection) {
if (!existingRow) {
return new Response(JSON.stringify({ error: 'Sektion nicht gefunden' }), { status: 404 });
}
targetSection.settings = { ...targetSection.settings, ...settings };
const currentDraft = JSON.parse(existingRow.content_draft_json || '{}');
const currentStyles = JSON.parse(existingRow.styles_json || '{}');
await fs.mkdir(DATA_DIR, { recursive: true });
await fs.writeFile(draftPath, JSON.stringify(config, null, 2), 'utf-8');
// 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 } : {})
};
return new Response(JSON.stringify({ success: true, settings: targetSection.settings }), { status: 200 });
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) {
return new Response(JSON.stringify({ error: 'Fehler beim Aktualisieren der Sektions-Einstellungen' }), { status: 500 });
}