implemented step 04 by Gemini,
- added AJAX actions initSession, createProject, listProjects and getProjectStatus
This commit is contained in:
2
.gitignore
vendored
Normal file
2
.gitignore
vendored
Normal file
@ -0,0 +1,2 @@
|
||||
/data/users/*.json
|
||||
/data/projects/*.json
|
||||
@ -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;
|
||||
|
||||
123
src/Actions/ProjectActions.php
Normal file
123
src/Actions/ProjectActions.php
Normal file
@ -0,0 +1,123 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Actions;
|
||||
|
||||
use App\Services\FileStorage;
|
||||
use Exception;
|
||||
|
||||
class ProjectActions
|
||||
{
|
||||
private FileStorage $storage;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user