Repository navigation
Expand file tree
/
Copy pathindex.php
More file actions
113 lines (100 loc) · 4.22 KB
/
Copy pathindex.php
File metadata and controls
113 lines (100 loc) · 4.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
<?php
ob_start(); // Démarre la mise en tampon de sortie
require_once 'config.php';
$URL = explode('/', trim($_SERVER['REQUEST_URI'], '/'));
// Récupérer la méthode, le chemin et les paramètres
$METHODE = $_SERVER['REQUEST_METHOD'];
$CHEMIN = $URL[0] ?? null;
$PARAMETRE = $URL[1] ?? null;
header('Content-Type: application/json');
// Vérifier si la ressource demandée est "task"
if ($CHEMIN === "task" || ($METHODE === 'POST' && str_contains($CHEMIN, "task"))) {
if ($METHODE === 'GET') {
if ($PARAMETRE === null) {
// Récupération de toutes les tâches
$sql = "SELECT id, description FROM tasks";
$stmt = $pdo->query($sql);
$tasks = $stmt->fetchAll(PDO::FETCH_ASSOC);
echo json_encode($tasks, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE);
exit;
} elseif (filter_var($PARAMETRE, FILTER_VALIDATE_INT)) {
// Récupération de la tâche spécifique par l'ID
$sql = "SELECT id, description FROM tasks WHERE id = :id";
$stmt = $pdo->prepare($sql);
$stmt->execute(['id' => $PARAMETRE]);
$task = $stmt->fetch(PDO::FETCH_ASSOC);
if ($task) {
echo json_encode($task, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE);
} else {
http_response_code(404);
echo json_encode(["error" => "Task not found"]);
}
exit;
} else {
http_response_code(400);
echo json_encode(["error" => "Invalid task ID"]);
exit;
}
}
elseif ($METHODE === 'POST') {
$PARAMETRE = explode('=',explode('?',$CHEMIN)[1]);
try {
$sql = 'INSERT INTO tasks(description) VALUE(:description)';
$stmt = $pdo->prepare($sql);
$stmt->execute(['description' => $PARAMETRE[1]]);
if ($stmt->rowCount() > 0) {
$sql = "SELECT id, description FROM tasks order by id desc LIMIT 1";
$stmt = $pdo->query($sql);
$tasks = $stmt->fetchAll(PDO::FETCH_ASSOC);
if ($tasks) {
// Définir l'en-tête Location avec l'ID de la tâche créée
header("Location: http://localhost/task/".$tasks[0]['id']);
echo json_encode($tasks, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE);
http_response_code(200);
} else {
http_response_code(404);
echo json_encode(["error" => "Task not found"]);
}
return;
} else {
$response = ["status" => "warning", "message" => "Task not found or already deleted"];
echo json_encode($response, flags: JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE);
return;
}
} catch (PDOException $e) {
$response = ["status" => "error", "message" => "Database error: " . $e->getMessage()];
echo json_encode($response, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE);
return;
}
}
elseif($METHODE === 'DELETE')
{
try {
$sql = "DELETE FROM tasks WHERE id = :id";
$stmt = $pdo->prepare($sql);
$stmt->execute(['id' => $PARAMETRE]);
if ($stmt->rowCount() > 0) {
http_response_code(200);
return;
} else {
$response = ["status" => "warning", "message" => "Task not found or already deleted"];
echo json_encode($response, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE);
return;
}
} catch (PDOException $e) {
$response = ["status" => "error", "message" => "Database error: " . $e->getMessage()];
echo json_encode($response, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE);
return;
}
}
else {
http_response_code(405);
echo json_encode(["error" => "Method Not Allowed"]);
exit;
}
} else {
http_response_code(404);
echo json_encode(["error" => "Resource not found"]);
exit;
}
ob_end_flush(); // Envoie la sortie tamponnée au navigateur