]>
Commit | Line | Data |
---|---|---|
f8bf8952 NL |
1 | <?php |
2 | ||
3 | namespace Wallabag\CoreBundle\Controller; | |
4 | ||
5 | use Nelmio\ApiDocBundle\Annotation\ApiDoc; | |
f8bf8952 | 6 | use Symfony\Bundle\FrameworkBundle\Controller\Controller; |
27f15aa4 | 7 | use Symfony\Component\HttpFoundation\Request; |
42a90646 | 8 | use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; |
f8bf8952 NL |
9 | use Wallabag\CoreBundle\Entity\Entries; |
10 | use Wallabag\CoreBundle\Entity\Tags; | |
843dbe51 | 11 | use Wallabag\CoreBundle\Service\Extractor; |
f8bf8952 | 12 | |
a8c90c5c | 13 | class WallabagRestController extends Controller |
f8bf8952 | 14 | { |
f8bf8952 | 15 | /** |
a8c90c5c | 16 | * Retrieve all entries. It could be filtered by many options. |
f8bf8952 NL |
17 | * |
18 | * @ApiDoc( | |
a8c90c5c | 19 | * parameters={ |
2c093b03 NL |
20 | * {"name"="archive", "dataType"="boolean", "required"=false, "format"="true or false, all entries by default", "description"="filter by archived status."}, |
21 | * {"name"="star", "dataType"="boolean", "required"=false, "format"="true or false, all entries by default", "description"="filter by starred status."}, | |
22 | * {"name"="delete", "dataType"="boolean", "required"=false, "format"="true or false, default '0'", "description"="filter by deleted status."}, | |
a8c90c5c NL |
23 | * {"name"="sort", "dataType"="string", "required"=false, "format"="'created' or 'updated', default 'created'", "description"="sort entries by date."}, |
24 | * {"name"="order", "dataType"="string", "required"=false, "format"="'asc' or 'desc', default 'desc'", "description"="order of sort."}, | |
25 | * {"name"="page", "dataType"="integer", "required"=false, "format"="default '1'", "description"="what page you want."}, | |
26 | * {"name"="perPage", "dataType"="integer", "required"=false, "format"="default'30'", "description"="results per page."}, | |
27 | * {"name"="tags", "dataType"="string", "required"=false, "format"="api%2Crest", "description"="a list of tags url encoded. Will returns entries that matches ALL tags."}, | |
28 | * } | |
f8bf8952 NL |
29 | * ) |
30 | * @return Entries | |
31 | */ | |
27f15aa4 | 32 | public function getEntriesAction(Request $request) |
f8bf8952 | 33 | { |
6e334aba NL |
34 | $isArchived = $request->query->get('archive'); |
35 | $isStarred = $request->query->get('star'); | |
6079aaa3 | 36 | $isDeleted = $request->query->get('delete', 0); |
a8c90c5c NL |
37 | $sort = $request->query->get('sort', 'created'); |
38 | $order = $request->query->get('order', 'desc'); | |
39 | $page = $request->query->get('page', 1); | |
40 | $perPage = $request->query->get('perPage', 30); | |
41 | $tags = $request->query->get('tags', array()); | |
42 | ||
43 | $entries = $this | |
44 | ->getDoctrine() | |
45 | ->getRepository('WallabagCoreBundle:Entries') | |
6e334aba | 46 | ->findEntries(1, $isArchived, $isStarred, $isDeleted, $sort, $order); |
a8c90c5c | 47 | |
7df80cb3 | 48 | if (!is_array($entries)) { |
a8c90c5c NL |
49 | throw $this->createNotFoundException(); |
50 | } | |
51 | ||
52 | return $entries; | |
f8bf8952 NL |
53 | } |
54 | ||
55 | /** | |
a8c90c5c | 56 | * Retrieve a single entry |
f8bf8952 NL |
57 | * |
58 | * @ApiDoc( | |
59 | * requirements={ | |
60 | * {"name"="entry", "dataType"="integer", "requirement"="\w+", "description"="The entry ID"} | |
61 | * } | |
62 | * ) | |
63 | * @return Entries | |
64 | */ | |
65 | public function getEntryAction(Entries $entry) | |
66 | { | |
67 | return $entry; | |
68 | } | |
69 | ||
70 | /** | |
a8c90c5c | 71 | * Create an entry |
f8bf8952 NL |
72 | * |
73 | * @ApiDoc( | |
a8c90c5c NL |
74 | * parameters={ |
75 | * {"name"="url", "dataType"="string", "required"=true, "format"="http://www.test.com/article.html", "description"="Url for the entry."}, | |
76 | * {"name"="title", "dataType"="string", "required"=false, "description"="Optional, we'll get the title from the page."}, | |
77 | * {"name"="tags", "dataType"="string", "required"=false, "format"="tag1,tag2,tag3", "description"="a comma-separated list of tags."}, | |
78 | * } | |
f8bf8952 NL |
79 | * ) |
80 | */ | |
843dbe51 | 81 | public function postEntriesAction(Request $request) |
f8bf8952 | 82 | { |
843dbe51 | 83 | //TODO gérer si on passe les tags |
c3235553 NL |
84 | $url = $request->request->get('url'); |
85 | ||
86 | $content = Extractor::extract($url); | |
843dbe51 NL |
87 | $entry = new Entries(); |
88 | $entry->setUserId(1); | |
c3235553 | 89 | $entry->setUrl($url); |
9e0fff7c | 90 | $entry->setTitle($request->request->get('title') ?: $content->getTitle()); |
843dbe51 | 91 | $entry->setContent($content->getBody()); |
843dbe51 NL |
92 | $em = $this->getDoctrine()->getManager(); |
93 | $em->persist($entry); | |
94 | $em->flush(); | |
95 | ||
96 | return $entry; | |
f8bf8952 NL |
97 | } |
98 | ||
99 | /** | |
a8c90c5c | 100 | * Change several properties of an entry |
f8bf8952 NL |
101 | * |
102 | * @ApiDoc( | |
103 | * requirements={ | |
104 | * {"name"="entry", "dataType"="integer", "requirement"="\w+", "description"="The entry ID"} | |
a8c90c5c NL |
105 | * }, |
106 | * parameters={ | |
107 | * {"name"="title", "dataType"="string", "required"=false}, | |
108 | * {"name"="tags", "dataType"="string", "required"=false, "format"="tag1,tag2,tag3", "description"="a comma-separated list of tags."}, | |
2c093b03 NL |
109 | * {"name"="archive", "dataType"="boolean", "required"=false, "format"="true or false", "description"="archived the entry."}, |
110 | * {"name"="star", "dataType"="boolean", "required"=false, "format"="true or false", "description"="starred the entry."}, | |
111 | * {"name"="delete", "dataType"="boolean", "required"=false, "format"="true or false", "description"="flag as deleted. Default false. In case that you don't want to *really* remove it.."}, | |
a8c90c5c | 112 | * } |
f8bf8952 NL |
113 | * ) |
114 | */ | |
2c093b03 | 115 | public function patchEntriesAction(Entries $entry, Request $request) |
f8bf8952 | 116 | { |
2c093b03 NL |
117 | $title = $request->request->get("title"); |
118 | $tags = $request->request->get("tags", array()); | |
119 | $isArchived = $request->request->get("archive"); | |
120 | $isDeleted = $request->request->get("delete"); | |
121 | $isStarred = $request->request->get("star"); | |
122 | ||
123 | if (!is_null($title)) { | |
124 | $entry->setTitle($title); | |
125 | } | |
126 | ||
127 | if (!is_null($isArchived)) { | |
905ae369 | 128 | $entry->setArchived($isArchived); |
2c093b03 NL |
129 | } |
130 | ||
131 | if (!is_null($isDeleted)) { | |
132 | $entry->setDeleted($isDeleted); | |
133 | } | |
134 | ||
135 | if (!is_null($isStarred)) { | |
905ae369 | 136 | $entry->setStarred($isStarred); |
2c093b03 NL |
137 | } |
138 | ||
139 | $em = $this->getDoctrine()->getManager(); | |
2c093b03 NL |
140 | $em->flush(); |
141 | ||
142 | return $entry; | |
f8bf8952 NL |
143 | } |
144 | ||
145 | /** | |
a8c90c5c | 146 | * Delete **permanently** an entry |
f8bf8952 NL |
147 | * |
148 | * @ApiDoc( | |
a8c90c5c NL |
149 | * requirements={ |
150 | * {"name"="entry", "dataType"="integer", "requirement"="\w+", "description"="The entry ID"} | |
151 | * } | |
f8bf8952 NL |
152 | * ) |
153 | */ | |
a8c90c5c | 154 | public function deleteEntriesAction(Entries $entry) |
f8bf8952 | 155 | { |
42a90646 NL |
156 | if ($entry->isDeleted()) { |
157 | throw new NotFoundHttpException('This entry is already deleted'); | |
158 | } | |
159 | ||
160 | $em = $this->getDoctrine()->getManager(); | |
161 | $entry->setDeleted(1); | |
42a90646 NL |
162 | $em->flush(); |
163 | ||
164 | return $entry; | |
f8bf8952 NL |
165 | } |
166 | ||
167 | /** | |
a8c90c5c | 168 | * Retrieve all tags for an entry |
f8bf8952 NL |
169 | * |
170 | * @ApiDoc( | |
171 | * requirements={ | |
172 | * {"name"="entry", "dataType"="integer", "requirement"="\w+", "description"="The entry ID"} | |
173 | * } | |
174 | * ) | |
175 | */ | |
7df80cb3 J |
176 | public function getEntriesTagsAction(Entries $entry) |
177 | { | |
f8bf8952 NL |
178 | } |
179 | ||
180 | /** | |
a8c90c5c | 181 | * Add one or more tags to an entry |
f8bf8952 NL |
182 | * |
183 | * @ApiDoc( | |
184 | * requirements={ | |
185 | * {"name"="entry", "dataType"="integer", "requirement"="\w+", "description"="The entry ID"} | |
a8c90c5c NL |
186 | * }, |
187 | * parameters={ | |
188 | * {"name"="tags", "dataType"="string", "required"=false, "format"="tag1,tag2,tag3", "description"="a comma-separated list of tags."}, | |
189 | * } | |
f8bf8952 NL |
190 | * ) |
191 | */ | |
7df80cb3 J |
192 | public function postEntriesTagsAction(Entries $entry) |
193 | { | |
f8bf8952 NL |
194 | } |
195 | ||
196 | /** | |
a8c90c5c | 197 | * Permanently remove one tag for an entry |
f8bf8952 NL |
198 | * |
199 | * @ApiDoc( | |
200 | * requirements={ | |
201 | * {"name"="tag", "dataType"="string", "requirement"="\w+", "description"="The tag"}, | |
202 | * {"name"="entry", "dataType"="integer", "requirement"="\w+", "description"="The entry ID"} | |
203 | * } | |
204 | * ) | |
205 | */ | |
206 | public function deleteEntriesTagsAction(Entries $entry, Tags $tag) | |
207 | { | |
f8bf8952 NL |
208 | } |
209 | ||
210 | /** | |
a8c90c5c | 211 | * Retrieve all tags |
f8bf8952 NL |
212 | * |
213 | * @ApiDoc( | |
214 | * ) | |
215 | */ | |
7df80cb3 J |
216 | public function getTagsAction() |
217 | { | |
f8bf8952 NL |
218 | } |
219 | ||
220 | /** | |
a8c90c5c | 221 | * Retrieve a single tag |
f8bf8952 NL |
222 | * |
223 | * @ApiDoc( | |
a8c90c5c | 224 | * requirements={ |
f8bf8952 | 225 | * {"name"="tag", "dataType"="string", "requirement"="\w+", "description"="The tag"} |
a8c90c5c | 226 | * } |
f8bf8952 NL |
227 | * ) |
228 | */ | |
7df80cb3 J |
229 | public function getTagAction(Tags $tag) |
230 | { | |
f8bf8952 NL |
231 | } |
232 | ||
233 | /** | |
a8c90c5c | 234 | * Permanently remove one tag from **every** entry |
f8bf8952 NL |
235 | * |
236 | * @ApiDoc( | |
237 | * requirements={ | |
238 | * {"name"="tag", "dataType"="string", "requirement"="\w+", "description"="The tag"} | |
239 | * } | |
240 | * ) | |
241 | */ | |
242 | public function deleteTagAction(Tags $tag) | |
243 | { | |
f8bf8952 | 244 | } |
7df80cb3 | 245 | } |