From a36737f4859e3acbddf5cfe90c279ba725a6d88a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicolas=20L=C5=93uillet?= Date: Tue, 24 Feb 2015 22:00:24 +0100 Subject: POST entries/tags with test --- .../Controller/WallabagRestController.php | 28 +++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) (limited to 'src/Wallabag/CoreBundle/Controller/WallabagRestController.php') diff --git a/src/Wallabag/CoreBundle/Controller/WallabagRestController.php b/src/Wallabag/CoreBundle/Controller/WallabagRestController.php index db9cf1be..e59ad4b7 100644 --- a/src/Wallabag/CoreBundle/Controller/WallabagRestController.php +++ b/src/Wallabag/CoreBundle/Controller/WallabagRestController.php @@ -213,8 +213,34 @@ class WallabagRestController extends Controller * } * ) */ - public function postEntriesTagsAction(Entry $entry) + public function postEntriesTagsAction(Request $request, Entry $entry) { + $tags = explode(',', $request->request->get('tags')); + + foreach ($tags as $label) { + $tagEntity = $this + ->getDoctrine() + ->getRepository('WallabagCoreBundle:Tag') + ->findOneByLabel($label); + + if (is_null($tagEntity)) { + $tagEntity = new Tag(); + $tagEntity->setLabel($label); + } + + // only add the tag on the entry if the relation doesn't exist + if (!$entry->getTags()->contains($tagEntity)) { + $entry->addTag($tagEntity); + } + } + + $em = $this->getDoctrine()->getManager(); + $em->persist($entry); + $em->flush(); + + $json = $this->get('serializer')->serialize($entry, 'json'); + + return new Response($json, 200, array('application/json')); } /** -- cgit v1.2.3