This repository was archived by the owner on Aug 20, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathArticle.php
executable file
·206 lines (181 loc) · 7.07 KB
/
Article.php
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
<?php
namespace EBM\Routes;
use EBM\Database;
class Article
{
public static function getArticles(Database $db)
{
$articles = $db->query("SELECT id, title FROM np_articles");
return json_encode($articles);
}
public static function getArticleById(Database $db, int $article_id)
{
$article = $db->prepare("SELECT id, title FROM np_articles WHERE id=?", [$article_id], true);
// $article is false if the query returned nothing
if ($article) {
$paragraphs = $db->prepare(
"SELECT id, content, `order`
FROM np_paragraphs
WHERE article_id=?
ORDER BY `order`",
[$article->id]
);
$article_with_paragraphs = [
"id" => $article->id,
"title" => $article->title,
"paragraphs" => $paragraphs
];
return json_encode($article_with_paragraphs);
} else {
http_response_code(404);
$error = ["error" => "Article not found"];
return json_encode($error);
}
}
public static function getParagraphById(Database $db, int $paragraph_id)
{
$paragraph = $db->prepare("SELECT id, content, `order` FROM np_paragraphs WHERE id=?", [$paragraph_id], true);
// $paragraph is false if the query returned nothing
if ($paragraph) {
return json_encode($paragraph);
} else {
http_response_code(404);
$error = ["error" => "Paragraph not found"];
return json_encode($error);
}
}
public static function createArticle(Database $db, string $title)
{
$article = $db->prepare("INSERT INTO np_articles SET title=?", [$title]);
// $article is false if the query failed
if ($article) {
$id = $db->lastInsertId();
http_response_code(201);
return self::getArticleById($db, $id);
} else {
http_response_code(404);
$error = ["error" => "Article not created"];
return json_encode($error);
}
}
public static function createParagraph(Database $db, int $article_id, string $content)
{
$paragraph = $db->prepare(
"INSERT INTO np_paragraphs (article_id, content, `order`)
SELECT ?, ?, max(`order`) + 1
FROM np_paragraphs
WHERE article_id=?",
[$article_id, $content, $article_id]
);
// $paragraph is false if the query failed
if ($paragraph) {
$id = $db->lastInsertId();
http_response_code(201);
return self::getParagraphById($db, $id);
} else {
http_response_code(404);
$error = ["error" => "Paragraph not created"];
return json_encode($error);
}
}
public static function updateArticleTitle(Database $db, int $article_id, string $title)
{
$article = $db->prepare("UPDATE np_articles SET title=? WHERE id=?", [$title, $article_id]);
// $article is false if the query failed
if ($article) {
return self::getArticleById($db, $article_id);
} else {
http_response_code(404);
$error = ["error" => "Article not updated"];
return json_encode($error);
}
}
public static function updateParagraphContent(Database $db, int $paragraph_id, string $content)
{
$paragraph = $db->prepare(
"UPDATE np_paragraphs SET content=? WHERE id=?",
[$content, $paragraph_id]
);
// $paragraph is false if the query failed
if ($paragraph) {
return self::getParagraphById($db, $paragraph_id);
} else {
http_response_code(404);
$error = ["error" => "Paragraph not updated"];
return json_encode($error);
}
}
public static function updateParagraphOrder(Database $db, int $paragraph_id, int $new_order)
{
//TODO Requêtes à simplifier (deux en une) ?
$old_order = $db->prepare("SELECT `order` FROM np_paragraphs WHERE id=?", [$paragraph_id], true);
$article_id = $db->prepare("SELECT article_id FROM np_paragraphs WHERE id=?", [$paragraph_id], true);
//$old_order and $article_id are false if the query failed
if ($old_order && $article_id) {
$old_order = (int)$old_order->order;
$article_id = (int)$article_id->article_id;
if ($new_order > $old_order) {
$paragraphs_to_move = $db->prepare(
"UPDATE np_paragraphs
SET `order` = `order`-1
WHERE article_id=? AND `order`>? AND `order`<=?",
[$article_id, $old_order, $new_order]
);
} elseif ($new_order < $old_order) {
$paragraphs_to_move = $db->prepare(
"UPDATE np_paragraphs
SET `order` = `order`+1
WHERE article_id=? AND `order`>=? AND `order`<?",
[$article_id, $new_order, $old_order]
);
} else {
http_response_code(200);
$message = ["message" => "Paragraph already at the correct order"];
return json_encode($message);
}
$paragraph = $db->prepare(
"UPDATE np_paragraphs SET `order`=? WHERE id=?",
[$new_order, $paragraph_id]
);
//$paragraph and $paragraphs_to_move are false if the query failed
if ($paragraph && $paragraphs_to_move) {
return self::getArticleById($db, $article_id);
} else {
http_response_code(404);
$error = ["error" => "Paragraph not updated"];
return json_encode($error);
}
} else {
http_response_code(404);
$error = ["error" => "Paragraph not updated"];
return json_encode($error);
}
}
public static function deleteParagraph(Database $db, int $paragraph_id)
{
$paragraph = $db->prepare("DELETE FROM np_paragraphs WHERE id=?", [$paragraph_id]);
// $paragraph is false if the query failed
if ($paragraph) {
http_response_code(204);
return "";
} else {
http_response_code(404);
$error = ["error" => "Paragraph not deleted"];
return json_encode($error);
}
}
public static function deleteArticle(Database $db, int $article_id)
{
$paragraphs = $db->prepare("DELETE FROM np_paragraphs WHERE article_id=?", [$article_id]);
$article = $db->prepare("DELETE FROM np_articles WHERE id=?", [$article_id]);
// $paragraphs and $article are false if the queries failed
if ($paragraphs && $article) {
http_response_code(204);
return "";
} else {
http_response_code(404);
$error = ["error" => "Article not deleted"];
return json_encode($error);
}
}
}