diff options
author | Jeremy Benoist <j0k3r@users.noreply.github.com> | 2017-01-27 09:34:32 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-01-27 09:34:32 +0100 |
commit | 6fb06904ecde15b1b07d0a2af945338b416cf0e2 (patch) | |
tree | e76f3e8142399316ec5660fab8c646b2c34b8336 /src/Wallabag/ApiBundle/Controller/EntryRestController.php | |
parent | 05fa529bcfde01be5d320cb532900d72cf4b0830 (diff) | |
parent | 78295b99dd1721c613f1ce52e2debbe6f6db7753 (diff) | |
download | wallabag-6fb06904ecde15b1b07d0a2af945338b416cf0e2.tar.gz wallabag-6fb06904ecde15b1b07d0a2af945338b416cf0e2.tar.zst wallabag-6fb06904ecde15b1b07d0a2af945338b416cf0e2.zip |
Merge pull request #2416 from wallabag/2.2
wallabag 2.2.0
Diffstat (limited to 'src/Wallabag/ApiBundle/Controller/EntryRestController.php')
-rw-r--r-- | src/Wallabag/ApiBundle/Controller/EntryRestController.php | 76 |
1 files changed, 75 insertions, 1 deletions
diff --git a/src/Wallabag/ApiBundle/Controller/EntryRestController.php b/src/Wallabag/ApiBundle/Controller/EntryRestController.php index 24fa7b3b..2c2ec0c1 100644 --- a/src/Wallabag/ApiBundle/Controller/EntryRestController.php +++ b/src/Wallabag/ApiBundle/Controller/EntryRestController.php | |||
@@ -10,6 +10,8 @@ use Symfony\Component\HttpFoundation\JsonResponse; | |||
10 | use Symfony\Component\Routing\Generator\UrlGeneratorInterface; | 10 | use Symfony\Component\Routing\Generator\UrlGeneratorInterface; |
11 | use Wallabag\CoreBundle\Entity\Entry; | 11 | use Wallabag\CoreBundle\Entity\Entry; |
12 | use Wallabag\CoreBundle\Entity\Tag; | 12 | use Wallabag\CoreBundle\Entity\Tag; |
13 | use Wallabag\CoreBundle\Event\EntrySavedEvent; | ||
14 | use Wallabag\CoreBundle\Event\EntryDeletedEvent; | ||
13 | 15 | ||
14 | class EntryRestController extends WallabagRestController | 16 | class EntryRestController extends WallabagRestController |
15 | { | 17 | { |
@@ -149,6 +151,28 @@ class EntryRestController extends WallabagRestController | |||
149 | } | 151 | } |
150 | 152 | ||
151 | /** | 153 | /** |
154 | * Retrieve a single entry as a predefined format. | ||
155 | * | ||
156 | * @ApiDoc( | ||
157 | * requirements={ | ||
158 | * {"name"="entry", "dataType"="integer", "requirement"="\w+", "description"="The entry ID"} | ||
159 | * } | ||
160 | * ) | ||
161 | * | ||
162 | * @return Response | ||
163 | */ | ||
164 | public function getEntryExportAction(Entry $entry, Request $request) | ||
165 | { | ||
166 | $this->validateAuthentication(); | ||
167 | $this->validateUserAccess($entry->getUser()->getId()); | ||
168 | |||
169 | return $this->get('wallabag_core.helper.entries_export') | ||
170 | ->setEntries($entry) | ||
171 | ->updateTitle('entry') | ||
172 | ->exportAs($request->attributes->get('_format')); | ||
173 | } | ||
174 | |||
175 | /** | ||
152 | * Create an entry. | 176 | * Create an entry. |
153 | * | 177 | * |
154 | * @ApiDoc( | 178 | * @ApiDoc( |
@@ -200,9 +224,11 @@ class EntryRestController extends WallabagRestController | |||
200 | 224 | ||
201 | $em = $this->getDoctrine()->getManager(); | 225 | $em = $this->getDoctrine()->getManager(); |
202 | $em->persist($entry); | 226 | $em->persist($entry); |
203 | |||
204 | $em->flush(); | 227 | $em->flush(); |
205 | 228 | ||
229 | // entry saved, dispatch event about it! | ||
230 | $this->get('event_dispatcher')->dispatch(EntrySavedEvent::NAME, new EntrySavedEvent($entry)); | ||
231 | |||
206 | $json = $this->get('serializer')->serialize($entry, 'json'); | 232 | $json = $this->get('serializer')->serialize($entry, 'json'); |
207 | 233 | ||
208 | return (new JsonResponse())->setJson($json); | 234 | return (new JsonResponse())->setJson($json); |
@@ -260,6 +286,51 @@ class EntryRestController extends WallabagRestController | |||
260 | } | 286 | } |
261 | 287 | ||
262 | /** | 288 | /** |
289 | * Reload an entry. | ||
290 | * An empty response with HTTP Status 304 will be send if we weren't able to update the content (because it hasn't changed or we got an error). | ||
291 | * | ||
292 | * @ApiDoc( | ||
293 | * requirements={ | ||
294 | * {"name"="entry", "dataType"="integer", "requirement"="\w+", "description"="The entry ID"} | ||
295 | * } | ||
296 | * ) | ||
297 | * | ||
298 | * @return JsonResponse | ||
299 | */ | ||
300 | public function patchEntriesReloadAction(Entry $entry) | ||
301 | { | ||
302 | $this->validateAuthentication(); | ||
303 | $this->validateUserAccess($entry->getUser()->getId()); | ||
304 | |||
305 | try { | ||
306 | $entry = $this->get('wallabag_core.content_proxy')->updateEntry($entry, $entry->getUrl()); | ||
307 | } catch (\Exception $e) { | ||
308 | $this->get('logger')->error('Error while saving an entry', [ | ||
309 | 'exception' => $e, | ||
310 | 'entry' => $entry, | ||
311 | ]); | ||
312 | |||
313 | return new JsonResponse([], 304); | ||
314 | } | ||
315 | |||
316 | // if refreshing entry failed, don't save it | ||
317 | if ($this->getParameter('wallabag_core.fetching_error_message') === $entry->getContent()) { | ||
318 | return new JsonResponse([], 304); | ||
319 | } | ||
320 | |||
321 | $em = $this->getDoctrine()->getManager(); | ||
322 | $em->persist($entry); | ||
323 | $em->flush(); | ||
324 | |||
325 | // entry saved, dispatch event about it! | ||
326 | $this->get('event_dispatcher')->dispatch(EntrySavedEvent::NAME, new EntrySavedEvent($entry)); | ||
327 | |||
328 | $json = $this->get('serializer')->serialize($entry, 'json'); | ||
329 | |||
330 | return (new JsonResponse())->setJson($json); | ||
331 | } | ||
332 | |||
333 | /** | ||
263 | * Delete **permanently** an entry. | 334 | * Delete **permanently** an entry. |
264 | * | 335 | * |
265 | * @ApiDoc( | 336 | * @ApiDoc( |
@@ -279,6 +350,9 @@ class EntryRestController extends WallabagRestController | |||
279 | $em->remove($entry); | 350 | $em->remove($entry); |
280 | $em->flush(); | 351 | $em->flush(); |
281 | 352 | ||
353 | // entry deleted, dispatch event about it! | ||
354 | $this->get('event_dispatcher')->dispatch(EntryDeletedEvent::NAME, new EntryDeletedEvent($entry)); | ||
355 | |||
282 | $json = $this->get('serializer')->serialize($entry, 'json'); | 356 | $json = $this->get('serializer')->serialize($entry, 'json'); |
283 | 357 | ||
284 | return (new JsonResponse())->setJson($json); | 358 | return (new JsonResponse())->setJson($json); |