{
"tool_response": {
"filePath": "\/var\/www\/dev.campus.systemische-tools.de\/src\/Infrastructure\/Persistence\/ContentRepository.php",
"oldString": "<?php\n\ndeclare(strict_types=1);\n\nnamespace Infrastructure\\Persistence;\n\n\/\/ @responsibility: Persistenz für Content-Orders, -Versions und -Configs\n\nuse Domain\\Repository\\ContentRepositoryInterface;\nuse Infrastructure\\Traits\\JsonDecodeTrait;\n\nclass ContentRepository implements ContentRepositoryInterface\n{\n use JsonDecodeTrait;\n\n private \\PDO $pdo;\n\n public function __construct(\\PDO $pdo)\n {\n $this->pdo = $pdo;\n }\n\n \/\/ ==================== Orders ====================\n\n public function findAllOrders(array $filters = [], int $limit = 50, int $offset = 0): array\n {\n $sql = 'SELECT co.*,\n ap.name as profile_name,\n cc.name as contract_name,\n cs.name as structure_name,\n (SELECT COUNT(*) FROM content_versions WHERE order_id = co.id) as version_count\n FROM content_orders co\n LEFT JOIN content_config ap ON co.author_profile_id = ap.id AND ap.type = \"author_profile\"\n LEFT JOIN content_config cc ON co.contract_id = cc.id AND cc.type = \"contract\"\n LEFT JOIN content_config cs ON co.structure_id = cs.id AND cs.type = \"structure\"\n WHERE 1=1';\n\n $params = [];\n\n if (isset($filters['status']) && $filters['status'] !== '') {\n $sql .= ' AND co.status = :status';\n $params['status'] = $filters['status'];\n }\n\n if (isset($filters['profile_id']) && $filters['profile_id'] !== '') {\n $sql .= ' AND co.author_profile_id = :profile_id';\n $params['profile_id'] = $filters['profile_id'];\n }\n\n $sql .= ' ORDER BY co.updated_at DESC LIMIT :limit OFFSET :offset';\n\n $stmt = $this->pdo->prepare($sql);\n foreach ($params as $key => $value) {\n $stmt->bindValue(':' . $key, $value);\n }\n $stmt->bindValue(':limit', $limit, \\PDO::PARAM_INT);\n $stmt->bindValue(':offset', $offset, \\PDO::PARAM_INT);\n $stmt->execute();\n\n return $stmt->fetchAll();\n }\n\n public function findOrder(int $id): ?array\n {\n $stmt = $this->pdo->prepare('\n SELECT co.*,\n ap.name as profile_name, ap.content as profile_config,\n cc.name as contract_name, cc.content as contract_config,\n cs.name as structure_name, cs.content as structure_config\n FROM content_orders co\n LEFT JOIN content_config ap ON co.author_profile_id = ap.id AND ap.type = \"author_profile\"\n LEFT JOIN content_config cc ON co.contract_id = cc.id AND cc.type = \"contract\"\n LEFT JOIN content_config cs ON co.structure_id = cs.id AND cs.type = \"structure\"\n WHERE co.id = :id\n ');\n $stmt->execute(['id' => $id]);\n $result = $stmt->fetch();\n\n return $result !== false ? $result : null;\n }\n\n \/**\n * Get settings from the last created order as defaults for new orders.\n *\n * @return array{model: string, collections: array<string>, context_limit: int, author_profile_id: int|null, contract_id: int|null, structure_id: int|null}\n *\/\n public function getLastOrderSettings(): array\n {\n $stmt = $this->pdo->query('\n SELECT model, collections, context_limit, author_profile_id, contract_id, structure_id\n FROM content_orders\n ORDER BY id DESC\n LIMIT 1\n ');\n $row = $stmt->fetch(\\PDO::FETCH_ASSOC);\n\n if (!$row) {\n return [\n 'model' => 'claude-sonnet-4-20250514',\n 'collections' => ['documents'],\n 'context_limit' => 5,\n 'author_profile_id' => null,\n 'contract_id' => null,\n 'structure_id' => null,\n ];\n }\n\n return [\n 'model' => $row['model'] ?? 'claude-sonnet-4-20250514',\n 'collections' => $this->decodeJsonArray($row['collections'] ?? null) ?: ['documents'],\n 'context_limit' => (int) ($row['context_limit'] ?? 5),\n 'author_profile_id' => $row['author_profile_id'] !== null ? (int) $row['author_profile_id'] : null,\n 'contract_id' => $row['contract_id'] !== null ? (int) $row['contract_id'] : null,\n 'structure_id' => $row['structure_id'] !== null ? (int) $row['structure_id'] : null,\n ];\n }\n\n public function createOrder(array $data): int\n {\n $stmt = $this->pdo->prepare(\"\n INSERT INTO content_orders\n (title, briefing, author_profile_id, contract_id, structure_id, model, collections, context_limit, status)\n VALUES (:title, :briefing, :profile_id, :contract_id, :structure_id, :model, :collections, :context_limit, 'draft')\n \");\n\n $stmt->execute([\n 'title' => $data['title'],\n 'briefing' => $data['briefing'],\n 'profile_id' => ($data['author_profile_id'] !== '' && $data['author_profile_id'] !== null) ? $data['author_profile_id'] : null,\n 'contract_id' => ($data['contract_id'] !== '' && $data['contract_id'] !== null) ? $data['contract_id'] : null,\n 'structure_id' => ($data['structure_id'] !== '' && $data['structure_id'] !== null) ? $data['structure_id'] : null,\n 'model' => $data['model'] ?? 'claude-sonnet-4-20250514',\n 'collections' => $data['collections'] ?? '[\"documents\"]',\n 'context_limit' => $data['context_limit'] ?? 5,\n ]);\n\n return (int) $this->pdo->lastInsertId();\n }\n\n public function updateOrderStatus(int $id, string $status): void\n {\n $stmt = $this->pdo->prepare('\n UPDATE content_orders SET status = :status, updated_at = NOW() WHERE id = :id\n ');\n $stmt->execute(['id' => $id, 'status' => $status]);\n }\n\n public function updateGenerationStatus(int $id, string $status, ?string $error = null): void\n {\n $sql = 'UPDATE content_orders SET generation_status = :status, updated_at = NOW()';\n $params = ['id' => $id, 'status' => $status];\n\n if ($status === 'generating') {\n $sql .= ', generation_started_at = NOW()';\n }\n if ($error !== null) {\n $sql .= ', generation_error = :error';\n $params['error'] = $error;\n }\n if ($status === 'completed' || $status === 'idle') {\n $sql .= ', generation_error = NULL';\n }\n\n $sql .= ' WHERE id = :id';\n $stmt = $this->pdo->prepare($sql);\n $stmt->execute($params);\n }\n\n public function updateCritiqueStatus(int $id, string $status, ?string $error = null): void\n {\n $sql = 'UPDATE content_orders SET critique_status = :status, updated_at = NOW()';\n $params = ['id' => $id, 'status' => $status];\n\n if ($status === 'critiquing') {\n $sql .= ', critique_started_at = NOW(), critique_log = NULL';\n }\n if ($error !== null) {\n $sql .= ', critique_error = :error';\n $params['error'] = $error;\n }\n if ($status === 'completed' || $status === 'idle') {\n $sql .= ', critique_error = NULL';\n }\n\n $sql .= ' WHERE id = :id';\n $stmt = $this->pdo->prepare($sql);\n $stmt->execute($params);\n }\n\n public function updateOrder(int $id, array $data): bool\n {\n $stmt = $this->pdo->prepare('\n UPDATE content_orders SET\n title = :title,\n briefing = :briefing,\n author_profile_id = :profile_id,\n contract_id = :contract_id,\n structure_id = :structure_id,\n updated_at = NOW()\n WHERE id = :id\n ');\n\n return $stmt->execute([\n 'id' => $id,\n 'title' => $data['title'],\n 'briefing' => $data['briefing'],\n 'profile_id' => ($data['author_profile_id'] !== '' && $data['author_profile_id'] !== null) ? $data['author_profile_id'] : null,\n 'contract_id' => ($data['contract_id'] !== '' && $data['contract_id'] !== null) ? $data['contract_id'] : null,\n 'structure_id' => ($data['structure_id'] !== '' && $data['structure_id'] !== null) ? $data['structure_id'] : null,\n ]);\n }\n\n \/\/ ==================== Versions ====================\n\n public function findVersionsByOrder(int $orderId): array\n {\n $stmt = $this->pdo->prepare('\n SELECT * FROM content_versions\n WHERE order_id = :order_id\n ORDER BY version_number DESC\n ');\n $stmt->execute(['order_id' => $orderId]);\n\n return $stmt->fetchAll();\n }\n\n public function findLatestVersion(int $orderId): ?array\n {\n $stmt = $this->pdo->prepare('\n SELECT * FROM content_versions\n WHERE order_id = :order_id\n ORDER BY version_number DESC\n LIMIT 1\n ');\n $stmt->execute(['order_id' => $orderId]);\n $result = $stmt->fetch();\n\n return $result !== false ? $result : null;\n }\n\n public function findVersion(int $id): ?array\n {\n $stmt = $this->pdo->prepare('SELECT * FROM content_versions WHERE id = :id');\n $stmt->execute(['id' => $id]);\n $result = $stmt->fetch();\n\n return $result !== false ? $result : null;\n }\n\n \/\/ ==================== Critiques ====================\n\n public function findCritiquesByVersion(int $versionId): array\n {\n $stmt = $this->pdo->prepare('\n SELECT cc.*, c.name as critic_name\n FROM content_critiques cc\n JOIN critics c ON cc.critic_id = c.id\n WHERE cc.version_id = :version_id\n ORDER BY cc.round DESC, c.sort_order ASC\n ');\n $stmt->execute(['version_id' => $versionId]);\n\n $results = $stmt->fetchAll();\n\n \/\/ Parse feedback JSON and merge with row\n foreach ($results as &$row) {\n if (isset($row['feedback']) && $... [TRUNCATED-2b17e97ac2c44b62]",
"newString": "<?php\n\ndeclare(strict_types=1);\n\nnamespace Infrastructure\\Persistence;\n\n\/\/ @responsibility: Persistenz für Content-Konfigurationen\n\nuse Domain\\Repository\\ContentRepositoryInterface;\n\nclass ContentRepository implements ContentRepositoryInterface\n{\n private \\PDO $pdo;\n private ContentOrderRepositoryInterface $orderRepository;\n private ContentVersionRepositoryInterface $versionRepository;\n private ContentSourceRepositoryInterface $sourceRepository;\n\n public function __construct(\n \\PDO $pdo,\n ContentOrderRepositoryInterface $orderRepository,\n ContentVersionRepositoryInterface $versionRepository,\n ContentSourceRepositoryInterface $sourceRepository\n ) {\n $this->pdo = $pdo;\n $this->orderRepository = $orderRepository;\n $this->versionRepository = $versionRepository;\n $this->sourceRepository = $sourceRepository;\n }\n\n \/\/ ==================== Delegation to specialized repositories ====================\n\n public function findAllOrders(array $filters = [], int $limit = 50, int $offset = 0): array\n {\n return $this->orderRepository->findAllOrders($filters, $limit, $offset);\n }\n\n public function findOrder(int $id): ?array\n {\n return $this->orderRepository->findOrder($id);\n }\n\n public function getLastOrderSettings(): array\n {\n return $this->orderRepository->getLastOrderSettings();\n }\n\n public function createOrder(array $data): int\n {\n return $this->orderRepository->createOrder($data);\n }\n\n public function updateOrderStatus(int $id, string $status): void\n {\n $this->orderRepository->updateOrderStatus($id, $status);\n }\n\n public function updateGenerationStatus(int $id, string $status, ?string $error = null): void\n {\n $this->orderRepository->updateGenerationStatus($id, $status, $error);\n }\n\n public function updateCritiqueStatus(int $id, string $status, ?string $error = null): void\n {\n $this->orderRepository->updateCritiqueStatus($id, $status, $error);\n }\n\n public function updateOrder(int $id, array $data): bool\n {\n return $this->orderRepository->updateOrder($id, $data);\n }\n\n public function findVersionsByOrder(int $orderId): array\n {\n return $this->versionRepository->findVersionsByOrder($orderId);\n }\n\n public function findLatestVersion(int $orderId): ?array\n {\n return $this->versionRepository->findLatestVersion($orderId);\n }\n\n public function findVersion(int $id): ?array\n {\n return $this->versionRepository->findVersion($id);\n }\n\n public function findCritiquesByVersion(int $versionId): array\n {\n return $this->versionRepository->findCritiquesByVersion($versionId);\n }\n\n public function findSourcesByOrder(int $orderId): array\n {\n return $this->sourceRepository->findSourcesByOrder($orderId);\n }",
"originalFile": "<?php\n\ndeclare(strict_types=1);\n\nnamespace Infrastructure\\Persistence;\n\n\/\/ @responsibility: Persistenz für Content-Orders, -Versions und -Configs\n\nuse Domain\\Repository\\ContentRepositoryInterface;\nuse Infrastructure\\Traits\\JsonDecodeTrait;\n\nclass ContentRepository implements ContentRepositoryInterface\n{\n use JsonDecodeTrait;\n\n private \\PDO $pdo;\n\n public function __construct(\\PDO $pdo)\n {\n $this->pdo = $pdo;\n }\n\n \/\/ ==================== Orders ====================\n\n public function findAllOrders(array $filters = [], int $limit = 50, int $offset = 0): array\n {\n $sql = 'SELECT co.*,\n ap.name as profile_name,\n cc.name as contract_name,\n cs.name as structure_name,\n (SELECT COUNT(*) FROM content_versions WHERE order_id = co.id) as version_count\n FROM content_orders co\n LEFT JOIN content_config ap ON co.author_profile_id = ap.id AND ap.type = \"author_profile\"\n LEFT JOIN content_config cc ON co.contract_id = cc.id AND cc.type = \"contract\"\n LEFT JOIN content_config cs ON co.structure_id = cs.id AND cs.type = \"structure\"\n WHERE 1=1';\n\n $params = [];\n\n if (isset($filters['status']) && $filters['status'] !== '') {\n $sql .= ' AND co.status = :status';\n $params['status'] = $filters['status'];\n }\n\n if (isset($filters['profile_id']) && $filters['profile_id'] !== '') {\n $sql .= ' AND co.author_profile_id = :profile_id';\n $params['profile_id'] = $filters['profile_id'];\n }\n\n $sql .= ' ORDER BY co.updated_at DESC LIMIT :limit OFFSET :offset';\n\n $stmt = $this->pdo->prepare($sql);\n foreach ($params as $key => $value) {\n $stmt->bindValue(':' . $key, $value);\n }\n $stmt->bindValue(':limit', $limit, \\PDO::PARAM_INT);\n $stmt->bindValue(':offset', $offset, \\PDO::PARAM_INT);\n $stmt->execute();\n\n return $stmt->fetchAll();\n }\n\n public function findOrder(int $id): ?array\n {\n $stmt = $this->pdo->prepare('\n SELECT co.*,\n ap.name as profile_name, ap.content as profile_config,\n cc.name as contract_name, cc.content as contract_config,\n cs.name as structure_name, cs.content as structure_config\n FROM content_orders co\n LEFT JOIN content_config ap ON co.author_profile_id = ap.id AND ap.type = \"author_profile\"\n LEFT JOIN content_config cc ON co.contract_id = cc.id AND cc.type = \"contract\"\n LEFT JOIN content_config cs ON co.structure_id = cs.id AND cs.type = \"structure\"\n WHERE co.id = :id\n ');\n $stmt->execute(['id' => $id]);\n $result = $stmt->fetch();\n\n return $result !== false ? $result : null;\n }\n\n \/**\n * Get settings from the last created order as defaults for new orders.\n *\n * @return array{model: string, collections: array<string>, context_limit: int, author_profile_id: int|null, contract_id: int|null, structure_id: int|null}\n *\/\n public function getLastOrderSettings(): array\n {\n $stmt = $this->pdo->query('\n SELECT model, collections, context_limit, author_profile_id, contract_id, structure_id\n FROM content_orders\n ORDER BY id DESC\n LIMIT 1\n ');\n $row = $stmt->fetch(\\PDO::FETCH_ASSOC);\n\n if (!$row) {\n return [\n 'model' => 'claude-sonnet-4-20250514',\n 'collections' => ['documents'],\n 'context_limit' => 5,\n 'author_profile_id' => null,\n 'contract_id' => null,\n 'structure_id' => null,\n ];\n }\n\n return [\n 'model' => $row['model'] ?? 'claude-sonnet-4-20250514',\n 'collections' => $this->decodeJsonArray($row['collections'] ?? null) ?: ['documents'],\n 'context_limit' => (int) ($row['context_limit'] ?? 5),\n 'author_profile_id' => $row['author_profile_id'] !== null ? (int) $row['author_profile_id'] : null,\n 'contract_id' => $row['contract_id'] !== null ? (int) $row['contract_id'] : null,\n 'structure_id' => $row['structure_id'] !== null ? (int) $row['structure_id'] : null,\n ];\n }\n\n public function createOrder(array $data): int\n {\n $stmt = $this->pdo->prepare(\"\n INSERT INTO content_orders\n (title, briefing, author_profile_id, contract_id, structure_id, model, collections, context_limit, status)\n VALUES (:title, :briefing, :profile_id, :contract_id, :structure_id, :model, :collections, :context_limit, 'draft')\n \");\n\n $stmt->execute([\n 'title' => $data['title'],\n 'briefing' => $data['briefing'],\n 'profile_id' => ($data['author_profile_id'] !== '' && $data['author_profile_id'] !== null) ? $data['author_profile_id'] : null,\n 'contract_id' => ($data['contract_id'] !== '' && $data['contract_id'] !== null) ? $data['contract_id'] : null,\n 'structure_id' => ($data['structure_id'] !== '' && $data['structure_id'] !== null) ? $data['structure_id'] : null,\n 'model' => $data['model'] ?? 'claude-sonnet-4-20250514',\n 'collections' => $data['collections'] ?? '[\"documents\"]',\n 'context_limit' => $data['context_limit'] ?? 5,\n ]);\n\n return (int) $this->pdo->lastInsertId();\n }\n\n public function updateOrderStatus(int $id, string $status): void\n {\n $stmt = $this->pdo->prepare('\n UPDATE content_orders SET status = :status, updated_at = NOW() WHERE id = :id\n ');\n $stmt->execute(['id' => $id, 'status' => $status]);\n }\n\n public function updateGenerationStatus(int $id, string $status, ?string $error = null): void\n {\n $sql = 'UPDATE content_orders SET generation_status = :status, updated_at = NOW()';\n $params = ['id' => $id, 'status' => $status];\n\n if ($status === 'generating') {\n $sql .= ', generation_started_at = NOW()';\n }\n if ($error !== null) {\n $sql .= ', generation_error = :error';\n $params['error'] = $error;\n }\n if ($status === 'completed' || $status === 'idle') {\n $sql .= ', generation_error = NULL';\n }\n\n $sql .= ' WHERE id = :id';\n $stmt = $this->pdo->prepare($sql);\n $stmt->execute($params);\n }\n\n public function updateCritiqueStatus(int $id, string $status, ?string $error = null): void\n {\n $sql = 'UPDATE content_orders SET critique_status = :status, updated_at = NOW()';\n $params = ['id' => $id, 'status' => $status];\n\n if ($status === 'critiquing') {\n $sql .= ', critique_started_at = NOW(), critique_log = NULL';\n }\n if ($error !== null) {\n $sql .= ', critique_error = :error';\n $params['error'] = $error;\n }\n if ($status === 'completed' || $status === 'idle') {\n $sql .= ', critique_error = NULL';\n }\n\n $sql .= ' WHERE id = :id';\n $stmt = $this->pdo->prepare($sql);\n $stmt->execute($params);\n }\n\n public function updateOrder(int $id, array $data): bool\n {\n $stmt = $this->pdo->prepare('\n UPDATE content_orders SET\n title = :title,\n briefing = :briefing,\n author_profile_id = :profile_id,\n contract_id = :contract_id,\n structure_id = :structure_id,\n updated_at = NOW()\n WHERE id = :id\n ');\n\n return $stmt->execute([\n 'id' => $id,\n 'title' => $data['title'],\n 'briefing' => $data['briefing'],\n 'profile_id' => ($data['author_profile_id'] !== '' && $data['author_profile_id'] !== null) ? $data['author_profile_id'] : null,\n 'contract_id' => ($data['contract_id'] !== '' && $data['contract_id'] !== null) ? $data['contract_id'] : null,\n 'structure_id' => ($data['structure_id'] !== '' && $data['structure_id'] !== null) ? $data['structure_id'] : null,\n ]);\n }\n\n \/\/ ==================== Versions ====================\n\n public function findVersionsByOrder(int $orderId): array\n {\n $stmt = $this->pdo->prepare('\n SELECT * FROM content_versions\n WHERE order_id = :order_id\n ORDER BY version_number DESC\n ');\n $stmt->execute(['order_id' => $orderId]);\n\n return $stmt->fetchAll();\n }\n\n public function findLatestVersion(int $orderId): ?array\n {\n $stmt = $this->pdo->prepare('\n SELECT * FROM content_versions\n WHERE order_id = :order_id\n ORDER BY version_number DESC\n LIMIT 1\n ');\n $stmt->execute(['order_id' => $orderId]);\n $result = $stmt->fetch();\n\n return $result !== false ? $result : null;\n }\n\n public function findVersion(int $id): ?array\n {\n $stmt = $this->pdo->prepare('SELECT * FROM content_versions WHERE id = :id');\n $stmt->execute(['id' => $id]);\n $result = $stmt->fetch();\n\n return $result !== false ? $result : null;\n }\n\n \/\/ ==================== Critiques ====================\n\n public function findCritiquesByVersion(int $versionId): array\n {\n $stmt = $this->pdo->prepare('\n SELECT cc.*, c.name as critic_name\n FROM content_critiques cc\n JOIN critics c ON cc.critic_id = c.id\n WHERE cc.version_id = :version_id\n ORDER BY cc.round DESC, c.sort_order ASC\n ');\n $stmt->execute(['version_id' => $versionId]);\n\n $results = $stmt->fetchAll();\n\n \/\/ Parse feedback JSON and merge with row\n foreach ($results as &$row) {\n if (isset($row['feedback']) && $... [TRUNCATED-c2849c02c9d65e58]",
"structuredPatch": [
{
"oldStart": 4,
"oldLines": 292,
"newStart": 4,
"newLines": 94,
"lines": [
" ",
" namespace Infrastructure\\Persistence;",
" ",
"-\/\/ @responsibility: Persistenz für Content-Orders, -Versions und -Configs",
"+\/\/ @responsibility: Persistenz für Content-Konfigurationen",
" ",
" use Domain\\Repository\\ContentRepositoryInterface;",
"-use Infrastructure\\Traits\\JsonDecodeTrait;",
" ",
" class ContentRepository implements ContentRepositoryInterface",
" {",
"- use JsonDecodeTrait;",
"-",
" private \\PDO $pdo;",
"+ private ContentOrderRepositoryInterface $orderRepository;",
"+ private ContentVersionRepositoryInterface $versionRepository;",
"+ private ContentSourceRepositoryInterface $sourceRepository;",
" ",
"- public function __construct(\\PDO $pdo)",
"- {",
"+ public function __construct(",
"+ \\PDO $pdo,",
"+ ContentOrderRepositoryInterface $orderRepository,",
"+ ContentVersionRepositoryInterface $versionRepository,",
"+ ContentSourceRepositoryInterface $sourceRepository",
"+ ) {",
" $this->pdo = $pdo;",
"+ $this->orderRepository = $orderRepository;",
"+ $this->versionRepository = $versionRepository;",
"+ $this->sourceRepository = $sourceRepository;",
" }",
" ",
"- \/\/ ==================== Orders ====================",
"+ \/\/ ==================== Delegation to specialized repositories ====================",
" ",
" public function findAllOrders(array $filters = [], int $limit = 50, int $offset = 0): array",
" {",
"- $sql = 'SELECT co.*,",
"- ap.name as profile_name,",
"- cc.name as contract_name,",
"- cs.name as structure_name,",
"- (SELECT COUNT(*) FROM content_versions WHERE order_id = co.id) as version_count",
"- FROM content_orders co",
"- LEFT JOIN content_config ap ON co.author_profile_id = ap.id AND ap.type = \"author_profile\"",
"- LEFT JOIN content_config cc ON co.contract_id = cc.id AND cc.type = \"contract\"",
"- LEFT JOIN content_config cs ON co.structure_id = cs.id AND cs.type = \"structure\"",
"- WHERE 1=1';",
"-",
"- $params = [];",
"-",
"- if (isset($filters['status']) && $filters['status'] !== '') {",
"- $sql .= ' AND co.status = :status';",
"- $params['status'] = $filters['status'];",
"- }",
"-",
"- if (isset($filters['profile_id']) && $filters['profile_id'] !== '') {",
"- $sql .= ' AND co.author_profile_id = :profile_id';",
"- $params['profile_id'] = $filters['profile_id'];",
"- }",
"-",
"- $sql .= ' ORDER BY co.updated_at DESC LIMIT :limit OFFSET :offset';",
"-",
"- $stmt = $this->pdo->prepare($sql);",
"- foreach ($params as $key => $value) {",
"- $stmt->bindValue(':' . $key, $value);",
"- }",
"- $stmt->bindValue(':limit', $limit, \\PDO::PARAM_INT);",
"- $stmt->bindValue(':offset', $offset, \\PDO::PARAM_INT);",
"- $stmt->execute();",
"-",
"- return $stmt->fetchAll();",
"+ return $this->orderRepository->findAllOrders($filters, $limit, $offset);",
" }",
" ",
" public function findOrder(int $id): ?array",
" {",
"- $stmt = $this->pdo->prepare('",
"- SELECT co.*,",
"- ap.name as profile_name, ap.content as profile_config,",
"- cc.name as contract_name, cc.content as contract_config,",
"- cs.name as structure_name, cs.content as structure_config",
"- FROM content_orders co",
"- LEFT JOIN content_config ap ON co.author_profile_id = ap.id AND ap.type = \"author_profile\"",
"- LEFT JOIN content_config cc ON co.contract_id = cc.id AND cc.type = \"contract\"",
"- LEFT JOIN content_config cs ON co.structure_id = cs.id AND cs.type = \"structure\"",
"- WHERE co.id = :id",
"- ');",
"- $stmt->execute(['id' => $id]);",
"- $result = $stmt->fetch();",
"-",
"- return $result !== false ? $result : null;",
"+ return $this->orderRepository->findOrder($id);",
" }",
" ",
"- \/**",
"- * Get settings from the last created order as defaults for new orders.",
"- *",
"- * @return array{model: string, collections: array<string>, context_limit: int, author_profile_id: int|null, contract_id: int|null, structure_id: int|null}",
"- *\/",
" public function getLastOrderSettings(): array",
" {",
"- $stmt = $this->pdo->query('",
"- SELECT model, collections, context_limit, author_profile_id, contract_id, structure_id",
"- FROM content_orders",
"- ORDER BY id DESC",
"- LIMIT 1",
"- ');",
"- $row = $stmt->fetch(\\PDO::FETCH_ASSOC);",
"-",
"- if (!$row) {",
"- return [",
"- 'model' => 'claude-sonnet-4-20250514',",
"- 'collections' => ['documents'],",
"- 'context_limit' => 5,",
"- 'author_profile_id' => null,",
"- 'contract_id' => null,",
"- 'structure_id' => null,",
"- ];",
"- }",
"-",
"- return [",
"- 'model' => $row['model'] ?? 'claude-sonnet-4-20250514',",
"- 'collections' => $this->decodeJsonArray($row['collections'] ?? null) ?: ['documents'],",
"- 'context_limit' => (int) ($row['context_limit'] ?? 5),",
"- 'author_profile_id' => $row['author_profile_id'] !== null ? (int) $row['author_profile_id'] : null,",
"- 'contract_id' => $row['contract_id'] !== null ? (int) $row['contract_id'] : null,",
"- 'structure_id' => $row['structure_id'] !== null ? (int) $row['structure_id'] : null,",
"- ];",
"+ return $this->orderRepository->getLastOrderSettings();",
" }",
" ",
" public function createOrder(array $data): int",
" {",
"- $stmt = $this->pdo->prepare(\"",
"- INSERT INTO content_orders",
"- (title, briefing, author_profile_id, contract_id, structure_id, model, collections, context_limit, status)",
"- VALUES (:title, :briefing, :profile_id, :contract_id, :structure_id, :model, :collections, :context_limit, 'draft')",
"- \");",
"-",
"- $stmt->execute([",
"- 'title' => $data['title'],",
"- 'briefing' => $data['briefing'],",
"- 'profile_id' => ($data['author_profile_id'] !== '' && $data['author_profile_id'] !== null) ? $data['author_profile_id'] : null,",
"- 'contract_id' => ($data['contract_id'] !== '' && $data['contract_id'] !== null) ? $data['contract_id'] : null,",
"- 'structure_id' => ($data['structure_id'] !== '' && $data['structure_id'] !== null) ? $data['structure_id'] : null,",
"- 'model' => $data['model'] ?? 'claude-sonnet-4-20250514',",
"- 'collections' => $data['collections'] ?? '[\"documents\"]',",
"- 'context_limit' => $data['context_limit'] ?? 5,",
"- ]);",
"-",
"- return (int) $this->pdo->lastInsertId();",
"+ return $this->orderRepository->createOrder($data);",
" }",
" ",
" public function updateOrderStatus(int $id, string $status): void",
" {",
"- $stmt = $this->pdo->prepare('",
"- UPDATE content_orders SET status = :status, updated_at = NOW() WHERE id = :id",
"- ');",
"- $stmt->execute(['id' => $id, 'status' => $status]);",
"+ $this->orderRepository->updateOrderStatus($id, $status);",
" }",
" ",
" public function updateGenerationStatus(int $id, string $status, ?string $error = null): void",
" {",
"- $sql = 'UPDATE content_orders SET generation_status = :status, updated_at = NOW()';",
"- $params = ['id' => $id, 'status' => $status];",
"-",
"- if ($status === 'generating') {",
"- $sql .= ', generation_started_at = NOW()';",
"- }",
"- if ($error !== null) {",
"- $sql .= ', generation_error = :error';",
"- $params['error'] = $error;",
"- }",
"- if ($status === 'completed' || $status === 'idle') {",
"- $sql .= ', generation_error = NULL';",
"- }",
"-",
"- $sql .= ' WHERE id = :id';",
"- $stmt = $this->pdo->prepare($sql);",
"- $stmt->execute($params);",
"+ $this->orderRepository->updateGenerationStatus($id, $status, $error);",
" }",
" ",
" public function updateCritiqueStatus(int $id, string $status, ?string $error = null): void",
" {",
"- $sql = 'UPDATE content_orders SET critique_status = :status, updated_at = NOW()';",
"- $params = ['id' => $id, 'status' => $status];",
"-",
"- if ($status === 'critiquing') {",
"- $sql .= ', critique_started_at = NOW(), critique_log = NULL';",
"- }",
"- if ($error !== null) {",
"- $sql .= ', critique_error = :error';",
"- $params['error'] = $error;",
"- }",
"- if ($status === 'completed' || $status === 'idle') {",
"- $sql .= ', critique_error = NULL';",
"- }",
"-",
"- $sql .= ' WHERE id = :id';",
"- $stmt = $this->pdo->prepare($sql);",
"- $stmt->execute($params);",
"+ $this->orderRepository->updateCritiqueStatus($id, $status, $error);",
" }",
" ",
" public function updateOrder(int $id, array $data): bool",
" {",
"- $stmt = $this->pdo->prepare('",
"- UPDATE content_orders SET",
"- title = :title,",
"- briefing = :briefing,",
"- author_profile_id = :profile_id,",
"- contract_id = :contract_id,",
"- structure_id = :structure_id,",
"- updated_at = NOW()",
"- WHERE id = :id",
"- ');",
"-",
"- return $stmt->execute([",
"- 'id' => $id,",
"- 'title' => $data['title'],",
"- 'briefing' => $data['briefing'],",
"- 'profile_id' => ($data['author_profile_id'] !== '' && $data['author_profile_id'] !== null) ? $data['author_profile_id'] : null,",
"- 'contract_id' => ($data['contract_id'] !== '' && $data['contract_id'] !== null) ? $data['contract_id'] : null,",
"- 'structure_id' => ($data['structure_id'] !== '' && $data['structure_id'] !== null) ? $data['structure_id'] : null,",
"- ]);",
"+ return $this->orderRepository->updateOrder($id, $data);",
" }",
" ",
"- \/\/ ==================== Versions ====================",
"-",
" public function findVersionsByOrder(int $orderId): array",
" {",
"- $stmt = $this->pdo->prepare('",
"- SELECT * FROM content_versions",
"- WHERE order_id = :order_id",
"- ORDER BY version_number DESC",
"- ');",
"- $stmt->execute(['order_id' => $orderId]);",
"-",
"- return $stmt->fetchAll();",
"+ return $this->versionRepository->findVersionsByOrder($orderId);",
" }",
" ",
" public function findLatestVersion(int $orderId): ?array",
" {",
"- $stmt = $this->pdo->prepare('",
"- SELECT * FROM content_versions",
"- WHERE order_id = :order_id",
"- ORDER BY version_number DESC",
"- LIMIT 1",
"- ');",
"- $stmt->execute(['order_id' => $orderId]);",
"- $result = $stmt->fetch();",
"-",
"- return $result !== false ? $result : null;",
"+ return $this->versionRepository->findLatestVersion($orderId);",
" }",
" ",
" public function findVersion(int $id): ?array",
" {",
"- $stmt = $this->pdo->prepare('SELECT * FROM content_versions WHERE id = :id');",
"- $stmt->execute(['id' => $id]);",
"- $result = $stmt->fetch();",
"-",
"- return $result !== false ? $result : null;",
"+ return $this->versionRepository->findVersion($id);",
" }",
" ",
"- \/\/ ==================== Critiques ====================",
"-",
" public function findCritiquesByVersion(int $versionId): array",
" {",
"- $stmt = $this->pdo->prepare('",
"- SELECT cc.*, c.name as critic_name",
"- FROM content_critiques cc",
"- JOIN critics c ON cc.critic_id = c.id",
"- WHERE cc.version_id = :version_id",
"- ORDER BY cc.round DESC, c.sort_order ASC",
"- ');",
"- $stmt->execute(['version_id' => $versionId]);",
"-",
"- $results = $stmt->fetchAll();",
"-",
"- \/\/ Parse feedback JSON and merge with row",
"- foreach ($results as &$row) {",
"- if (isset($row['feedback']) && $row['feedback'] !== '') {",
"- $feedback = $this->decodeJsonArray($row['feedback']);",
"- if ($feedback !== []) {",
"- $row = array_merge($row, $feedback);",
"- }",
"- }",
"- }",
"-",
"- return $results;",
"+ return $this->versionRepository->findCritiquesByVersion($versionId);",
" }",
" ",
"- \/\/ ==================== Sources ====================",
"-",
" public function findSourcesByOrder(int $orderId): array",
" {",
"- $stmt = $this->pdo->prepare('",
"- SELECT cs.*, c.content, d.filename as document_name",
"- FROM content_sources cs",
"- JOIN chunks c ON cs.chunk_id = c.id",
"- JOIN documents d ON c.document_id = d.id",
"- WHERE cs.order_id = :order_id",
"- ORDER BY cs.relevance_score DESC",
"- ');",
"- $stmt->execute(['order_id' => $orderId]);",
"-",
"- return $stmt->fetchAll();",
"+ return $this->sourceRepository->findSourcesByOrder($orderId);",
" }",
" ",
" \/\/ ==================== Profiles ===================="
]
}
],
"userModified": false,
"replaceAll": false
}
}