{
"tool_response": {
"type": "text",
"file": {
"filePath": "\/var\/www\/dev.campus.systemische-tools.de\/src\/Controller\/Api\/TaskController.php",
"content": "<?php\n\ndeclare(strict_types=1);\n\nnamespace Controller\\Api;\n\n\/\/ @responsibility: REST-API für Task-Management (CRUD, Assignments, AI-Execution)\n\nuse Domain\\Repository\\TaskRepositoryInterface;\nuse Domain\\Repository\\TaskResultRepositoryInterface;\nuse Framework\\Controller;\nuse UseCases\\Task\\AssignTaskUseCase;\nuse UseCases\\Task\\CreateTaskUseCase;\nuse UseCases\\Task\\DeleteTaskUseCase;\nuse UseCases\\Task\\ExecuteAITaskUseCase;\nuse UseCases\\Task\\GetTasksUseCase;\nuse UseCases\\Task\\SaveTaskResultUseCase;\nuse UseCases\\Task\\UpdateTaskStatusUseCase;\n\nclass TaskController extends Controller\n{\n private GetTasksUseCase $getTasksUseCase;\n private CreateTaskUseCase $createTaskUseCase;\n private DeleteTaskUseCase $deleteTaskUseCase;\n private AssignTaskUseCase $assignTaskUseCase;\n private UpdateTaskStatusUseCase $updateStatusUseCase;\n private SaveTaskResultUseCase $saveResultUseCase;\n private ExecuteAITaskUseCase $executeAIUseCase;\n private TaskRepositoryInterface $taskRepository;\n private TaskResultRepositoryInterface $resultRepository;\n\n public function __construct(\n GetTasksUseCase $getTasksUseCase,\n CreateTaskUseCase $createTaskUseCase,\n DeleteTaskUseCase $deleteTaskUseCase,\n AssignTaskUseCase $assignTaskUseCase,\n UpdateTaskStatusUseCase $updateStatusUseCase,\n SaveTaskResultUseCase $saveResultUseCase,\n ExecuteAITaskUseCase $executeAIUseCase,\n TaskRepositoryInterface $taskRepository,\n TaskResultRepositoryInterface $resultRepository\n ) {\n $this->getTasksUseCase = $getTasksUseCase;\n $this->createTaskUseCase = $createTaskUseCase;\n $this->deleteTaskUseCase = $deleteTaskUseCase;\n $this->assignTaskUseCase = $assignTaskUseCase;\n $this->updateStatusUseCase = $updateStatusUseCase;\n $this->saveResultUseCase = $saveResultUseCase;\n $this->executeAIUseCase = $executeAIUseCase;\n $this->taskRepository = $taskRepository;\n $this->resultRepository = $resultRepository;\n }\n\n public function index(): void\n {\n try {\n $filters = [];\n $status = $this->getString('status');\n if ($status !== '') {\n $filters['status'] = $status;\n }\n $type = $this->getString('type');\n if ($type !== '') {\n $filters['type'] = $type;\n }\n $search = $this->getString('search');\n if ($search !== '') {\n $filters['search'] = $search;\n }\n\n $limit = $this->getInt('limit', 50);\n $offset = $this->getInt('offset');\n\n $tasks = $this->getTasksUseCase->execute($filters, $limit, $offset);\n $total = $this->getTasksUseCase->count($filters);\n\n $this->json([\n 'success' => true,\n 'data' => array_map(fn ($t) => $t->toArray(), $tasks),\n 'meta' => [\n 'total' => $total,\n 'limit' => $limit,\n 'offset' => $offset,\n ],\n ]);\n } catch (\\Exception $e) {\n $this->jsonError($e->getMessage());\n }\n }\n\n public function show(string $id): void\n {\n try {\n $details = $this->getTasksUseCase->getTaskWithDetails((int) $id);\n\n if ($details === null) {\n $this->json(['success' => false, 'error' => 'Task not found'], 404);\n\n return;\n }\n\n $this->json([\n 'success' => true,\n 'data' => $details,\n ]);\n } catch (\\Exception $e) {\n $this->jsonError($e->getMessage());\n }\n }\n\n public function store(): void\n {\n $this->requireCsrf();\n\n try {\n $input = $this->getJsonInput();\n if (empty($input)) {\n $input = $_POST;\n }\n\n $task = $this->createTaskUseCase->execute($input);\n\n \/\/ HTMX: Redirect to new task\n if ($this->isHtmxRequest()) {\n $this->htmxRedirect('\/tasks\/' . $task->getId());\n\n return;\n }\n\n $this->json([\n 'success' => true,\n 'data' => $task->toArray(),\n ], 201);\n } catch (\\InvalidArgumentException $e) {\n if ($this->isHtmxRequest()) {\n $this->htmxError($e->getMessage());\n\n return;\n }\n $this->json(['success' => false, 'error' => $e->getMessage()], 400);\n } catch (\\Exception $e) {\n if ($this->isHtmxRequest()) {\n $this->htmxError($e->getMessage());\n\n return;\n }\n $this->jsonError($e->getMessage());\n }\n }\n\n public function update(string $id): void\n {\n $this->requireCsrf();\n\n try {\n $input = $this->getJsonInput();\n if (empty($input)) {\n $input = $_POST;\n }\n\n $task = $this->getTasksUseCase->findById((int) $id);\n\n if ($task === null) {\n if ($this->isHtmxRequest()) {\n $this->htmxError('Task nicht gefunden');\n\n return;\n }\n $this->json(['success' => false, 'error' => 'Task not found'], 404);\n\n return;\n }\n\n if (isset($input['title']) || isset($input['description'])) {\n $task->updateDetails(\n $input['title'] ?? $task->getTitle(),\n $input['description'] ?? $task->getDescription()\n );\n }\n if (isset($input['type'])) {\n $task->changeType(\\Domain\\ValueObject\\TaskType::from($input['type']));\n }\n if (isset($input['due_date'])) {\n $task->setDueDateTo(new \\DateTimeImmutable($input['due_date']));\n }\n\n $this->taskRepository->update($task);\n\n \/\/ HTMX: Redirect to task page\n if ($this->isHtmxRequest()) {\n $this->htmxRedirect('\/tasks\/' . $id);\n\n return;\n }\n\n $this->json([\n 'success' => true,\n 'data' => $task->toArray(),\n ]);\n } catch (\\InvalidArgumentException $e) {\n if ($this->isHtmxRequest()) {\n $this->htmxError($e->getMessage());\n\n return;\n }\n $this->json(['success' => false, 'error' => $e->getMessage()], 400);\n } catch (\\Exception $e) {\n if ($this->isHtmxRequest()) {\n $this->htmxError($e->getMessage());\n\n return;\n }\n $this->jsonError($e->getMessage());\n }\n }\n\n public function destroy(string $id): void\n {\n try {\n $this->deleteTaskUseCase->execute((int) $id);\n\n $this->json([\n 'success' => true,\n 'message' => 'Task deleted',\n ]);\n } catch (\\InvalidArgumentException $e) {\n $this->json(['success' => false, 'error' => $e->getMessage()], 404);\n } catch (\\Exception $e) {\n $this->jsonError($e->getMessage());\n }\n }\n\n public function assign(string $id): void\n {\n try {\n $input = $this->getJsonInput();\n\n $assignment = $this->assignTaskUseCase->execute((int) $id, $input);\n\n $this->json([\n 'success' => true,\n 'data' => $assignment->toArray(),\n ], 201);\n } catch (\\InvalidArgumentException $e) {\n $this->json(['success' => false, 'error' => $e->getMessage()], 400);\n } catch (\\Exception $e) {\n $this->jsonError($e->getMessage());\n }\n }\n\n public function updateStatus(string $id): void\n {\n $this->requireCsrf();\n\n try {\n \/\/ Accept both JSON and form data\n $input = $this->getJsonInput();\n $status = $input['status'] ?? $_POST['status'] ?? null;\n\n if ($status === null || $status === '') {\n if ($this->isHtmxRequest()) {\n $this->htmxError('Status ist erforderlich');\n\n return;\n }\n $this->json(['success' => false, 'error' => 'Status is required'], 400);\n\n return;\n }\n\n $updatedBy = $input['updated_by'] ?? 'api';\n $updatedByType = $input['updated_by_type'] ?? 'human';\n\n $task = $this->updateStatusUseCase->execute((int) $id, $status, $updatedBy, $updatedByType);\n\n \/\/ HTMX: Return updated status select\n if ($this->isHtmxRequest()) {\n $this->partial('tasks.partials.status-select', [\n 'task' => $task->toArray(),\n ]);\n\n return;\n }\n\n $this->json([\n 'success' => true,\n 'data' => $task->toArray(),\n ]);\n } catch (\\InvalidArgumentException $e) {\n if ($this->isHtmxRequest()) {\n $this->htmxError($e->getMessage());\n\n return;\n }\n $this->json(['success' => false, 'error' => $e->getMessage()], 400);\n } catch (\\Exception $e) {\n if ($this->isHtmxRequest()) {\n $this->htmxError($e->getMessage());\n\n return;\n }\n $this->jsonError($e->getMessage());\n }\n }\n\n public function storeResult(string $id): void\n {\n try {\n $input = $this->getJsonInput();\n\n $result = $this->saveResultUseCase->execute((int) $id, $input);\n\n $this->json([\n 'success' => true,\n 'data' => $result->toArray(),\n ], 201);\n } catch (\\InvalidArgumentException $e) {\n $this->json(['success' => false, 'error' => $e->getMessage()], 400);\n } catch (\\Exce... [TRUNCATED-f1b519d9ffb407c3]",
"numLines": 403,
"startLine": 1,
"totalLines": 403
}
}
}