DELETE method

This commit is contained in:
Nicolas Lœuillet 2014-09-24 13:54:26 +02:00 committed by Nicolas Lœuillet
parent 8fb1d90fb2
commit d178abdc31
3 changed files with 52 additions and 13 deletions

View File

@ -1,9 +1,8 @@
<?php <?php
namespace Wallabag\Bundle\ApiBundle\Controller; namespace Wallabag\Bundle\ApiBundle\Controller;
use FOS\RestBundle\Controller\Annotations\Delete;
use FOS\RestBundle\Controller\Annotations\Get; use FOS\RestBundle\Controller\Annotations\Get;
use FOS\RestBundle\Controller\Annotations\Patch; use FOS\RestBundle\Controller\Annotations\Patch;
use FOS\RestBundle\Controller\Annotations\View; use FOS\RestBundle\Controller\Annotations\View;
@ -13,6 +12,7 @@ use Psr\Log\LoggerInterface;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter; use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Wallabag\Bundle\CoreBundle\Document\Entry; use Wallabag\Bundle\CoreBundle\Document\Entry;
use Wallabag\Bundle\CoreBundle\Document\User; use Wallabag\Bundle\CoreBundle\Document\User;
use Wallabag\Bundle\CoreBundle\Service\EntryService; use Wallabag\Bundle\CoreBundle\Service\EntryService;
@ -53,6 +53,7 @@ class EntryController {
public function getAction(User $user, Entry $entry) { public function getAction(User $user, Entry $entry) {
$this->logger->info("User {username} wants to show entry {url}", $this->logger->info("User {username} wants to show entry {url}",
array("username" => $user->getUsername(), "url" => $entry->getUrl())); array("username" => $user->getUsername(), "url" => $entry->getUrl()));
return $entry; return $entry;
} }
@ -70,10 +71,12 @@ class EntryController {
* ) * )
*/ */
public function patchAction(User $user, Entry $entry, Request $request) { public function patchAction(User $user, Entry $entry, Request $request) {
$request->request->get("tags", array()); $tags = $request->request->get("tags", array());
$request->request->get("archived"); $archived = $request->request->get("archived");
$request->request->get("deleted"); $deleted = $request->request->get("deleted");
$request->request->get("starred"); $starred = $request->request->get("starred");
$this->entryService->updateEntry($entry, $tags, $archived, $starred, $deleted);
$view = \FOS\RestBundle\View\View::create(); $view = \FOS\RestBundle\View\View::create();
$view->setStatusCode(Response::HTTP_NO_CONTENT); $view->setStatusCode(Response::HTTP_NO_CONTENT);
@ -81,6 +84,29 @@ class EntryController {
"user" => $user->getUsername(), "user" => $user->getUsername(),
"entry" => $entry->getId() "entry" => $entry->getId()
))); )));
return $view; return $view;
} }
}
/**
* @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;
}
}

View File

@ -79,18 +79,24 @@ class Entry
/** /**
* @var boolean * @var boolean
* @MongoDB\Boolean * @MongoDB\Boolean
* @Expose
* @Groups({"entries"})
*/ */
private $archived = false; private $archived = false;
/** /**
* @var boolean * @var boolean
* @MongoDB\Boolean * @MongoDB\Boolean
* @Expose
* @Groups({"entries"})
*/ */
private $deleted = false; private $deleted = false;
/** /**
* @var boolean * @var boolean
* @MongoDB\Boolean * @MongoDB\Boolean
* @Expose
* @Groups({"entries"})
*/ */
private $starred = false; private $starred = false;

View File

@ -123,19 +123,17 @@ class EntryService {
/** /**
* Change updatable fields of an entry an persists it * Change updatable fields of an entry an persists it
* *
* @param Entry $entry the entry * @param Entry $entry
* @param string[] $tags the new tags * @param string[] $tags the new tags
* @param boolean $archived the new archived status * @param boolean $archived the new archived status
* @param boolean $starred the new starred status * @param boolean $starred the new starred status
* @param boolean $deleted the new deleted status * @param boolean $deleted the new deleted status
*/ */
public function updateEntry(Entry $entry, $tags, $archived, $starred, $deleted) { public function updateEntry(Entry $entry, $tags, $archived, $starred, $deleted) {
$managed = $this->dm->merge($entry);
if(is_array($tags)) { if(is_array($tags)) {
$managed->flushTags(); $entry->flushTags();
foreach ($tags as $tag) { foreach ($tags as $tag) {
$managed->addTag(new Tag($tag)); $entry->addTag(new Tag($tag));
} }
} }
if($archived != null) { if($archived != null) {
@ -150,4 +148,13 @@ class EntryService {
$this->dm->flush(); $this->dm->flush();
} }
}
/**
* Delete an entry
* @param Entry $entry
*/
public function deleteEntry(Entry $entry) {
$entry->setDeleted(true);
$this->dm->flush();
}
}