ConfigDTO::fromArray($row), $this->configRepo->findAll($validType, $validStatus) ); } public function getById(int $id): ?ConfigDTO { $data = $this->configRepo->findById($id); return $data !== null ? ConfigDTO::fromArray($data) : null; } public function create( string $type, string $name, string $slug, ?string $description, string $content, string $version, string $status, ?int $parentId, ?int $promptId = null, int $sortOrder = 0 ): ConfigResult { $name = trim($name); $slug = trim($slug); $description = $description !== null ? trim($description) : null; if (!in_array($type, self::VALID_TYPES, true)) { return ConfigResult::error('Ungültiger Konfigurationstyp.'); } if ($name === '' || $slug === '') { return ConfigResult::error('Name und Slug sind erforderlich.'); } if (!$this->isValidJson($content)) { return ConfigResult::error('Ungültiges JSON-Format.'); } $id = $this->configRepo->create( $type, $name, $slug, $description !== '' ? $description : null, $content, trim($version) ?: '1.0', in_array($status, self::VALID_STATUSES, true) ? $status : 'draft', $parentId, $promptId, $sortOrder ); return ConfigResult::success($id, 'Konfiguration erfolgreich erstellt.'); } public function update( int $id, string $name, string $slug, ?string $description, string $content, string $newVersion, string $changeDescription, string $status, ?int $parentId ): ConfigResult { $name = trim($name); $slug = trim($slug); $newVersion = trim($newVersion); $description = $description !== null ? trim($description) : null; $existing = $this->configRepo->findById($id); if ($existing === null) { return ConfigResult::error('Konfiguration nicht gefunden.'); } if ($name === '' || $slug === '' || $newVersion === '') { return ConfigResult::error('Name, Slug und neue Version sind erforderlich.'); } if (!$this->isValidJson($content)) { return ConfigResult::error('Ungültiges JSON-Format.'); } $this->configRepo->saveHistory( $id, $existing['content'], $existing['version'], 'web-ui', $changeDescription !== '' ? $changeDescription : 'Update auf Version ' . $newVersion ); $this->configRepo->update( $id, $name, $slug, $description !== '' ? $description : null, $content, $newVersion, in_array($status, self::VALID_STATUSES, true) ? $status : $existing['status'], $parentId ); return ConfigResult::success($id, 'Konfiguration auf Version ' . $newVersion . ' aktualisiert.'); } public function delete(int $id): ConfigResult { $existing = $this->configRepo->findById($id); if ($existing === null) { return ConfigResult::error('Konfiguration nicht gefunden.'); } if ($this->configRepo->countChildren($id) > 0) { return ConfigResult::error('Kann nicht gelöscht werden: Es existieren verknüpfte Einträge.'); } $this->configRepo->deleteHistory($id); $this->configRepo->delete($id); return ConfigResult::success($id, 'Konfiguration gelöscht.'); } public function getChildren(int $id): array { return $this->configRepo->getChildren($id); } public function getHistory(int $id): array { return $this->configRepo->getHistory($id); } public function getParentOptions(?int $excludeId = null): array { return $this->configRepo->getParentOptions($excludeId); } public function getStatistics(): array { return $this->configRepo->getStatistics(); } private function isValidJson(string $content): bool { if ($content === 'null') { return true; } json_decode($content, true); return json_last_error() === JSON_ERROR_NONE; } }