From 20ff641811a0ea7d3927de7688d7662151e03707 Mon Sep 17 00:00:00 2001 From: igor Date: Fri, 12 Jun 2026 16:31:42 +0200 Subject: [PATCH] implemented step 04 by Gemini, - added AJAX actions initSession, createProject, listProjects and getProjectStatus --- .gitignore | 2 + public/ajax.php | 22 ++++++ src/Actions/ProjectActions.php | 123 +++++++++++++++++++++++++++++++++ 3 files changed, 147 insertions(+) create mode 100644 .gitignore create mode 100644 src/Actions/ProjectActions.php diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..7f83026 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +/data/users/*.json +/data/projects/*.json diff --git a/public/ajax.php b/public/ajax.php index 5a31b39..8dbc05f 100644 --- a/public/ajax.php +++ b/public/ajax.php @@ -51,11 +51,33 @@ try { } // Router + $projectActions = new \App\Actions\ProjectActions(); + switch ($action) { case 'ping': sendResponse(true, ['message' => 'pong', 'timestamp' => time()]); break; + case 'initSession': + sendResponse(true, $projectActions->initSession()); + break; + + case 'createProject': + sendResponse(true, $projectActions->createProject($userId)); + break; + + case 'listProjects': + sendResponse(true, $projectActions->listProjects($userId)); + break; + + case 'getProjectStatus': + $projectId = $data['project_id'] ?? null; + if (!$projectId) { + sendResponse(false, ['code' => 'MISSING_PROJECT_ID', 'message' => 'Project ID is required.'], 400); + } + sendResponse(true, $projectActions->getProjectStatus($userId, $projectId)); + break; + default: sendResponse(false, ['code' => 'UNKNOWN_ACTION', 'message' => "Action '$action' is not defined."], 404); break; diff --git a/src/Actions/ProjectActions.php b/src/Actions/ProjectActions.php new file mode 100644 index 0000000..9a14711 --- /dev/null +++ b/src/Actions/ProjectActions.php @@ -0,0 +1,123 @@ +storage = new FileStorage(); + } + + /** + * Initializes a new user session. + */ + public function initSession(): array + { + $userId = 'u_' . bin2hex(random_bytes(8)); + $userData = [ + 'user_id' => $userId, + 'created_at' => date('c'), + 'projects' => [] + ]; + + $this->storage->put("users/{$userId}.json", $userData); + + return ['user_id' => $userId]; + } + + /** + * Creates a new project for a user. + */ + public function createProject(string $userId): array + { + $userPath = "users/{$userId}.json"; + $userData = $this->storage->get($userPath); + + if (!$userData) { + throw new Exception("User not found.", 404); + } + + $projectId = 'p_' . bin2hex(random_bytes(8)); + $projectData = [ + 'project_id' => $projectId, + 'user_id' => $userId, + 'status' => 'draft', + 'current_step' => 1, + 'wizard_data' => [ + 'identity' => [], + 'contact' => [], + 'services' => [], + 'visuals' => [], + 'modules' => [], + 'assets' => [], + 'language' => [], + 'business_category' => [], + 'smart_answers' => [] + ], + 'created_at' => date('c'), + 'updated_at' => date('c') + ]; + + $this->storage->put("projects/{$projectId}.json", $projectData); + + // Update user's project list + $userData['projects'][] = $projectId; + $this->storage->put($userPath, $userData); + + return $projectData; + } + + /** + * Lists all projects for a user. + */ + public function listProjects(string $userId): array + { + $userData = $this->storage->get("users/{$userId}.json"); + + if (!$userData) { + throw new Exception("User not found.", 404); + } + + $projects = []; + foreach ($userData['projects'] as $projectId) { + $projectData = $this->storage->get("projects/{$projectId}.json"); + if ($projectData) { + $projects[] = [ + 'project_id' => $projectData['project_id'], + 'status' => $projectData['status'], + 'business_name' => $projectData['wizard_data']['identity']['business_name'] ?? 'Unnamed Project', + 'created_at' => $projectData['created_at'], + 'updated_at' => $projectData['updated_at'] + ]; + } + } + + return $projects; + } + + /** + * Returns full project data. + */ + public function getProjectStatus(string $userId, string $projectId): array + { + $projectData = $this->storage->get("projects/{$projectId}.json"); + + if (!$projectData) { + throw new Exception("Project not found.", 404); + } + + if ($projectData['user_id'] !== $userId) { + throw new Exception("Unauthorized access to project.", 403); + } + + return $projectData; + } +}