-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnotes-ws.php
More file actions
144 lines (121 loc) · 3.78 KB
/
notes-ws.php
File metadata and controls
144 lines (121 loc) · 3.78 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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
<?php
//notes webservice
require('db_connection.php');
$method = $_SERVER['REQUEST_METHOD'];
switch ($method) {
case 'GET':
if($_GET == null){
get_all();
}
break;
case 'POST':
$data = json_decode(file_get_contents("php://input"));
if ($data->title != null){
add($data->title, $data->description, $data->categories);
}
break;
case 'PUT':
$data = json_decode(file_get_contents("php://input"));
if ($data->id != null && $data->title != null){
modify($data->id, $data->title, $data->description, $data->categories);
}
break;
case 'DELETE':
if (array_key_exists("id", $_GET)){
delete_by_id($_GET['id']);
}
break;
default:
handle_error($request);
break;
}
function modify($id, $title, $description, $categories){
if ($description == null){
$description = "";
}
try {
$dbconn = DbConnection::getConnection();
$conn = $dbconn->_pdo;
$stmt = $conn->prepare("UPDATE note SET title=:title, description=:description WHERE id=:id;");
$stmt->bindParam(':id', $id);
$stmt->bindParam(':title', $title);
$stmt->bindParam(':description', $description);
$stmt->execute();
if($categories != null){
$stmt = $conn->prepare("DELETE FROM note_categorie WHERE id_note = :id");
$stmt->bindParam(':id', $id);
$stmt->execute();
foreach ($categories as $cat) {
$stmt = $conn->prepare("INSERT INTO note_categorie (id_note, id_categorie) VALUES (:id_note, :id_categorie)");
$stmt->bindParam(':id_note', $id);
$stmt->bindParam(':id_categorie', $cat);
$stmt->execute();
}
}
$conn = null;
}catch(PDOException $e){
echo "Connection failed: " . $e->getMessage();
}
}
function add($title, $description, $categories){
if ($description == null){
$description = "";
}
try {
$dbconn = DbConnection::getConnection();
$conn = $dbconn->_pdo;
$stmt = $conn->prepare("INSERT INTO note (title, description) VALUES (:title, :description)");
$stmt->bindParam(':title', $title);
$stmt->bindParam(':description', $description);
$stmt->execute();
if($categories != null){
$id_note = $conn->lastInsertId();
foreach ($categories as $cat) {
$stmt = $conn->prepare("INSERT INTO note_categorie (id_note, id_categorie) VALUES (:id_note, :id_categorie)");
$stmt->bindParam(':id_note', $id_note);
$stmt->bindParam(':id_categorie', $cat);
$stmt->execute();
}
}
$conn = null;
}catch(PDOException $e){
echo "Connection failed: " . $e->getMessage();
}
}
function delete_by_id($id){
try {
$dbconn = DbConnection::getConnection();
$conn = $dbconn->_pdo;
$stmt = $conn->prepare("DELETE FROM note_categorie WHERE id_note = :id");
$stmt->bindParam(':id', $id);
$stmt->execute();
$stmt = $conn->prepare("DELETE FROM note WHERE id = :id");
$stmt->bindParam(':id', $id);
$stmt->execute();
$conn = null;
}catch(PDOException $e){
echo "Connection failed: " . $e->getMessage();
}
}
function get_all(){
try {
$dbconn = DbConnection::getConnection();
$conn = $dbconn->_pdo;
$stmt = $conn->prepare("SELECT * FROM note");
$stmt->execute();
$result = $stmt->fetchAll(PDO::FETCH_ASSOC);
for ($i = 0; $i < count($result); $i++) {
$stmt = $conn->prepare("SELECT categorie.id, categorie.name FROM note_categorie
INNER JOIN categorie ON note_categorie.id_categorie = categorie.id
WHERE note_categorie.id_note = :idnote");
$stmt->bindParam(':idnote', $result[$i]['id']);
$stmt->execute();
$res = $stmt->fetchAll(PDO::FETCH_ASSOC);
$result[$i]['categorie'] = $res;
}
echo json_encode($result);
}catch(PDOException $e){
echo "Connection failed: " . $e->getMessage();
}
}
?>