use Nelmio\ApiDocBundle\Annotation\ApiDoc;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
+use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Response;
use Wallabag\CoreBundle\Entity\Entry;
use Wallabag\CoreBundle\Entity\Tag;
class WallabagRestController extends Controller
{
+ /**
+ * @param Entry $entry
+ * @param string $tags
+ */
+ private function assignTagsToEntry(Entry $entry, $tags)
+ {
+ foreach (explode(',', $tags) as $label) {
+ $label = trim($label);
+ $tagEntity = $this
+ ->getDoctrine()
+ ->getRepository('WallabagCoreBundle:Tag')
+ ->findOneByLabel($label);
+
+ if (is_null($tagEntity)) {
+ $tagEntity = new Tag($this->getUser());
+ $tagEntity->setLabel($label);
+ }
+
+ // only add the tag on the entry if the relation doesn't exist
+ if (!$entry->getTags()->contains($tagEntity)) {
+ $entry->addTag($tagEntity);
+ }
+ }
+ }
+
/**
* Retrieve salt for a giver user.
*
- * @ApiDoc(
- * parameters={
- * {"name"="username", "dataType"="string", "required"=true, "description"="username"}
- * }
- * )
+ * @ApiDoc()
* @return array
*/
public function getSaltAction($username)
$json = $this->get('serializer')->serialize($entries, 'json');
- return new Response($json, 200, array('application/json'));
+ return new JsonResponse($json, 200);
}
/**
*/
public function getEntryAction(Entry $entry)
{
+ if ($entry->getUser()->getId() != $this->getUser()->getId()) {
+ throw $this->createAccessDeniedException();
+ }
+
$json = $this->get('serializer')->serialize($entry, 'json');
- return new Response($json, 200, array('application/json'));
+ return new JsonResponse($json, 200);
}
/**
*/
public function postEntriesAction(Request $request)
{
- //TODO gérer si on passe les tags
$url = $request->request->get('url');
$content = Extractor::extract($url);
$entry->setUrl($url);
$entry->setTitle($request->request->get('title') ?: $content->getTitle());
$entry->setContent($content->getBody());
+
+ $tags = $request->request->get('tags', '');
+ if (!empty($tags)) {
+ $this->assignTagsToEntry($entry, $tags);
+ }
+
$em = $this->getDoctrine()->getManager();
$em->persist($entry);
$em->flush();
$json = $this->get('serializer')->serialize($entry, 'json');
- return new Response($json, 200, array('application/json'));
+ return new JsonResponse($json, 200);
}
/**
*/
public function patchEntriesAction(Entry $entry, Request $request)
{
+ if ($entry->getUser()->getId() != $this->getUser()->getId()) {
+ throw $this->createAccessDeniedException();
+ }
+
$title = $request->request->get("title");
- $tags = $request->request->get("tags", array());
$isArchived = $request->request->get("archive");
$isStarred = $request->request->get("star");
$entry->setStarred($isStarred);
}
+ $tags = $request->request->get('tags', '');
+ if (!empty($tags)) {
+ $this->assignTagsToEntry($entry, $tags);
+ }
+
$em = $this->getDoctrine()->getManager();
$em->flush();
- return $entry;
+ $json = $this->get('serializer')->serialize($entry, 'json');
+
+ return new JsonResponse($json, 200);
}
/**
*/
public function deleteEntriesAction(Entry $entry)
{
+ if ($entry->getUser()->getId() != $this->getUser()->getId()) {
+ throw $this->createAccessDeniedException();
+ }
+
$em = $this->getDoctrine()->getManager();
$em->remove($entry);
$em->flush();
$json = $this->get('serializer')->serialize($entry, 'json');
- return new Response($json, 200, array('application/json'));
+ return new JsonResponse($json, 200);
}
/**
*/
public function getEntriesTagsAction(Entry $entry)
{
- $json = $this->get('serializer')->serialize($entry, 'json');
+ if ($entry->getUser()->getId() != $this->getUser()->getId()) {
+ throw $this->createAccessDeniedException();
+ }
+
+ $json = $this->get('serializer')->serialize($entry->getTags(), 'json');
return new Response($json, 200, array('application/json'));
}
* }
* )
*/
- public function postEntriesTagsAction(Entry $entry)
+ public function postEntriesTagsAction(Request $request, Entry $entry)
{
+ if ($entry->getUser()->getId() != $this->getUser()->getId()) {
+ throw $this->createAccessDeniedException();
+ }
+
+ $tags = $request->request->get('tags', '');
+ if (!empty($tags)) {
+ $this->assignTagsToEntry($entry, $tags);
+ }
+
+ $em = $this->getDoctrine()->getManager();
+ $em->persist($entry);
+ $em->flush();
+
+ $json = $this->get('serializer')->serialize($entry, 'json');
+
+ return new JsonResponse($json, 200);
}
/**
*/
public function deleteEntriesTagsAction(Entry $entry, Tag $tag)
{
+ if ($entry->getUser()->getId() != $this->getUser()->getId()) {
+ throw $this->createAccessDeniedException();
+ }
+
+ $entry->removeTag($tag);
+ $em = $this->getDoctrine()->getManager();
+ $em->persist($entry);
+ $em->flush();
+
+ $json = $this->get('serializer')->serialize($entry, 'json');
+
+ return new JsonResponse($json, 200);
}
/**
* Retrieve all tags
*
- * @ApiDoc(
- * {"name"="user", "dataType"="integer", "requirement"="\w+", "description"="The user ID"}
- * )
+ * @ApiDoc()
*/
- public function getTagsUserAction()
+ public function getTagsAction()
{
+ $json = $this->get('serializer')->serialize($this->getUser()->getTags(), 'json');
+
+ return new JsonResponse($json, 200);
}
/**
*/
public function deleteTagAction(Tag $tag)
{
+ if ($tag->getUser()->getId() != $this->getUser()->getId()) {
+ throw $this->createAccessDeniedException();
+ }
+
+ $em = $this->getDoctrine()->getManager();
+ $em->remove($tag);
+ $em->flush();
+
+ $json = $this->get('serializer')->serialize($tag, 'json');
+
+ return new JsonResponse($json, 200);
}
}