X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=src%2FWallabag%2FApiBundle%2FController%2FEntryRestController.php;h=fc46e782efb6cb29c5a6843c455e2d99b65ab3aa;hb=d1fc590211b8dc7360bf5b7ee01c67ccff0577ea;hp=1a726b6edbcc776aa0ba2cb0f57ea862574732f1;hpb=944b8d61752bedd126e1cdaeb3205177e414cf6b;p=github%2Fwallabag%2Fwallabag.git diff --git a/src/Wallabag/ApiBundle/Controller/EntryRestController.php b/src/Wallabag/ApiBundle/Controller/EntryRestController.php index 1a726b6e..fc46e782 100644 --- a/src/Wallabag/ApiBundle/Controller/EntryRestController.php +++ b/src/Wallabag/ApiBundle/Controller/EntryRestController.php @@ -41,7 +41,7 @@ class EntryRestController extends WallabagRestController ->getRepository('WallabagCoreBundle:Entry') ->findByUrlAndUserId($url, $this->getUser()->getId()); - $results[$url] = false === $res ? false : true; + $results[$url] = $res instanceof Entry ? $res->getId() : false; } $json = $this->get('serializer')->serialize($results, 'json'); @@ -60,7 +60,7 @@ class EntryRestController extends WallabagRestController ->getRepository('WallabagCoreBundle:Entry') ->findByUrlAndUserId($url, $this->getUser()->getId()); - $exists = false === $res ? false : true; + $exists = $res instanceof Entry ? $res->getId() : false; $json = $this->get('serializer')->serialize(['exists' => $exists], 'json'); @@ -287,7 +287,7 @@ class EntryRestController extends WallabagRestController /** * Reload an entry. - * A response with HTTP Status 400 will be return if we weren't able to update the content (because it hasn't changed or we got an error). + * 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). * * @ApiDoc( * requirements={ @@ -310,12 +310,12 @@ class EntryRestController extends WallabagRestController 'entry' => $entry, ]); - return new JsonResponse(['error' => 'Error while trying to fetch content'], 400); + return new JsonResponse([], 304); } // if refreshing entry failed, don't save it if ($this->getParameter('wallabag_core.fetching_error_message') === $entry->getContent()) { - return new JsonResponse(['error' => 'Error while trying to extract content'], 400); + return new JsonResponse([], 304); } $em = $this->getDoctrine()->getManager(); @@ -438,4 +438,72 @@ class EntryRestController extends WallabagRestController return (new JsonResponse())->setJson($json); } + + /** + * Handles an entries list and add / delete to them some tags. + * + * @ApiDoc( + * parameters={ + * {"name"="list", "dataType"="string", "required"=true, "format"="A JSON array of urls [{'url': 'http://...','tags': 'tag1, tag2','action': 'delete'}, {'url': 'http://...','tags': 'tag1, tag2','action': 'add'}]", "description"="Urls (as an array) to handle."} + * } + * ) + * + * @return JsonResponse + */ + public function postEntriesTagsListAction(Request $request) + { + $this->validateAuthentication(); + + $list = json_decode($request->query->get('list', [])); + $results = []; + + // handle multiple urls + if (!empty($list)) { + $results = []; + foreach ($list as $key => $element) { + $entry = $this->get('wallabag_core.entry_repository')->findByUrlAndUserId( + $element->url, + $this->getUser()->getId() + ); + + $results[$key]['url'] = $element->url; + $results[$key]['action'] = $element->action; + $results[$key]['entry'] = $entry instanceof Entry ? $entry->getId() : false; + + $tags = $element->tags; + + if (false !== $entry && !(empty($tags))) { + switch ($element->action) { + case 'delete': + $tags = explode(',', $tags); + foreach ($tags as $label) { + $label = trim($label); + + $tag = $this->getDoctrine() + ->getRepository('WallabagCoreBundle:Tag') + ->findOneByLabel($label); + + if (false !== $tag) { + $entry->removeTag($tag); + } + } + + break; + case 'add': + $this->get('wallabag_core.content_proxy')->assignTagsToEntry($entry, $tags); + + break; + } + + $em = $this->getDoctrine()->getManager(); + $em->persist($entry); + $em->flush(); + } + } + } + + $json = $this->get('serializer')->serialize($results, 'json'); + + return (new JsonResponse())->setJson($json); + } }