81 lines
2.4 KiB
PHP
81 lines
2.4 KiB
PHP
<?php
|
|
session_start();
|
|
header('Content-Type: application/json');
|
|
|
|
if (!isset($_SESSION['user_id'])) {
|
|
http_response_code(401);
|
|
echo json_encode(['error' => 'User not authenticated']);
|
|
exit;
|
|
}
|
|
|
|
require_once 'db/config.php';
|
|
|
|
$project_id = filter_input(INPUT_GET, 'id', FILTER_VALIDATE_INT);
|
|
if (!$project_id) {
|
|
http_response_code(400);
|
|
echo json_encode(['error' => 'Project ID is required.']);
|
|
exit;
|
|
}
|
|
|
|
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
|
|
http_response_code(405);
|
|
echo json_encode(['error' => 'POST method required.']);
|
|
exit;
|
|
}
|
|
|
|
$request_body = file_get_contents('php://input');
|
|
$data = json_decode($request_body, true);
|
|
if (json_last_error() !== JSON_ERROR_NONE) {
|
|
http_response_code(400);
|
|
echo json_encode(['error' => 'Invalid JSON input.']);
|
|
exit;
|
|
}
|
|
|
|
try {
|
|
$pdo = db();
|
|
|
|
// Verify project ownership and status
|
|
$stmt = $pdo->prepare("SELECT status FROM projects WHERE id = ? AND user_id = ?");
|
|
$stmt->execute([$project_id, $_SESSION['user_id']]);
|
|
$project = $stmt->fetch(PDO::FETCH_ASSOC);
|
|
|
|
if (!$project) {
|
|
http_response_code(404);
|
|
echo json_encode(['error' => 'Project not found or permission denied.']);
|
|
exit;
|
|
}
|
|
|
|
if ($project['status'] !== 'draft') {
|
|
http_response_code(403);
|
|
echo json_encode(['error' => 'Project is not in draft status and cannot be edited.']);
|
|
exit;
|
|
}
|
|
|
|
// Get new data from request body
|
|
$title = trim($data['title'] ?? '');
|
|
$story_text = trim($data['story_text'] ?? '');
|
|
$style = trim($data['style'] ?? '');
|
|
$target_duration = filter_var($data['target_duration'] ?? null, FILTER_VALIDATE_INT, ['options' => ['default' => 0]]);
|
|
|
|
if (empty($title)) {
|
|
http_response_code(400);
|
|
echo json_encode(['error' => 'Title is required.']);
|
|
exit;
|
|
}
|
|
|
|
// Update the project
|
|
$stmt = $pdo->prepare("UPDATE projects SET title = ?, story_text = ?, style = ?, target_duration = ? WHERE id = ?");
|
|
$stmt->execute([$title, $story_text, $style, $target_duration, $project_id]);
|
|
|
|
// Fetch and return the updated project
|
|
$stmt = $pdo->prepare("SELECT * FROM projects WHERE id = ?");
|
|
$stmt->execute([$project_id]);
|
|
$updated_project = $stmt->fetch(PDO::FETCH_ASSOC);
|
|
|
|
echo json_encode($updated_project);
|
|
|
|
} catch (PDOException $e) {
|
|
http_response_code(500);
|
|
// error_log("Database error: " . $e->getMessage());
|
|
echo json_encode(['error' => 'Database error.']);
|
|
} |