From d178abdc31fa476a2d1176a40de51e42746df3b8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicolas=20L=C5=93uillet?= Date: Wed, 24 Sep 2014 13:54:26 +0200 Subject: [PATCH] DELETE method --- .../ApiBundle/Controller/EntryController.php | 40 +++++++++++++++---- .../Bundle/CoreBundle/Document/Entry.php | 6 +++ .../CoreBundle/Service/EntryService.php | 19 ++++++--- 3 files changed, 52 insertions(+), 13 deletions(-) diff --git a/src/Wallabag/Bundle/ApiBundle/Controller/EntryController.php b/src/Wallabag/Bundle/ApiBundle/Controller/EntryController.php index 83d9a07..8145924 100644 --- a/src/Wallabag/Bundle/ApiBundle/Controller/EntryController.php +++ b/src/Wallabag/Bundle/ApiBundle/Controller/EntryController.php @@ -1,9 +1,8 @@ logger->info("User {username} wants to show entry {url}", array("username" => $user->getUsername(), "url" => $entry->getUrl())); + return $entry; } @@ -70,10 +71,12 @@ class EntryController { * ) */ public function patchAction(User $user, Entry $entry, Request $request) { - $request->request->get("tags", array()); - $request->request->get("archived"); - $request->request->get("deleted"); - $request->request->get("starred"); + $tags = $request->request->get("tags", array()); + $archived = $request->request->get("archived"); + $deleted = $request->request->get("deleted"); + $starred = $request->request->get("starred"); + + $this->entryService->updateEntry($entry, $tags, $archived, $starred, $deleted); $view = \FOS\RestBundle\View\View::create(); $view->setStatusCode(Response::HTTP_NO_CONTENT); @@ -81,6 +84,29 @@ class EntryController { "user" => $user->getUsername(), "entry" => $entry->getId() ))); + return $view; } -} \ No newline at end of file + + /** + * @Delete("/u/{user}/entry/{entry}") + * @ParamConverter("user", options={"mapping": {"user": "username"}}) + * @ParamConverter("entry", options={"id"="entry"}) + * @View(statusCode=200) + * @ApiDoc( + * requirements={ + * {"name"="user", "dataType"="string", "requirement"="\w+", "description"="The username"} + * } + * ) + */ + public function deleteAction(User $user, Entry $entry) + { + if ($entry->getDeleted()) { + throw new NotFoundHttpException(); + } + + $this->entryService->deleteEntry($entry); + + return $entry; + } +} diff --git a/src/Wallabag/Bundle/CoreBundle/Document/Entry.php b/src/Wallabag/Bundle/CoreBundle/Document/Entry.php index a654fbd..9a25303 100644 --- a/src/Wallabag/Bundle/CoreBundle/Document/Entry.php +++ b/src/Wallabag/Bundle/CoreBundle/Document/Entry.php @@ -79,18 +79,24 @@ class Entry /** * @var boolean * @MongoDB\Boolean + * @Expose + * @Groups({"entries"}) */ private $archived = false; /** * @var boolean * @MongoDB\Boolean + * @Expose + * @Groups({"entries"}) */ private $deleted = false; /** * @var boolean * @MongoDB\Boolean + * @Expose + * @Groups({"entries"}) */ private $starred = false; diff --git a/src/Wallabag/Bundle/CoreBundle/Service/EntryService.php b/src/Wallabag/Bundle/CoreBundle/Service/EntryService.php index ee99cb9..52e98f9 100644 --- a/src/Wallabag/Bundle/CoreBundle/Service/EntryService.php +++ b/src/Wallabag/Bundle/CoreBundle/Service/EntryService.php @@ -123,19 +123,17 @@ class EntryService { /** * Change updatable fields of an entry an persists it * - * @param Entry $entry the entry + * @param Entry $entry * @param string[] $tags the new tags * @param boolean $archived the new archived status * @param boolean $starred the new starred status * @param boolean $deleted the new deleted status */ public function updateEntry(Entry $entry, $tags, $archived, $starred, $deleted) { - $managed = $this->dm->merge($entry); - if(is_array($tags)) { - $managed->flushTags(); + $entry->flushTags(); foreach ($tags as $tag) { - $managed->addTag(new Tag($tag)); + $entry->addTag(new Tag($tag)); } } if($archived != null) { @@ -150,4 +148,13 @@ class EntryService { $this->dm->flush(); } -} \ No newline at end of file + + /** + * Delete an entry + * @param Entry $entry + */ + public function deleteEntry(Entry $entry) { + $entry->setDeleted(true); + $this->dm->flush(); + } +}