use Hateoas\Representation\Factory\PagerfantaFactory;
use Nelmio\ApiDocBundle\Annotation\ApiDoc;
use Symfony\Component\HttpFoundation\Request;
-use Symfony\Component\HttpFoundation\Response;
+use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
use Wallabag\CoreBundle\Entity\Entry;
* {"name"="page", "dataType"="integer", "required"=false, "format"="default '1'", "description"="what page you want."},
* {"name"="perPage", "dataType"="integer", "required"=false, "format"="default'30'", "description"="results per page."},
* {"name"="tags", "dataType"="string", "required"=false, "format"="api,rest", "description"="a list of tags url encoded. Will returns entries that matches ALL tags."},
+ * {"name"="since", "dataType"="integer", "required"=false, "format"="default '0'", "description"="The timestamp since when you want entries updated."},
* }
* )
*
- * @return Response
+ * @return JsonResponse
*/
public function getEntriesAction(Request $request)
{
$this->validateAuthentication();
- $isArchived = (int) $request->query->get('archive');
- $isStarred = (int) $request->query->get('starred');
+ $isArchived = (null === $request->query->get('archive')) ? null : (bool) $request->query->get('archive');
+ $isStarred = (null === $request->query->get('starred')) ? null : (bool) $request->query->get('starred');
$sort = $request->query->get('sort', 'created');
$order = $request->query->get('order', 'desc');
$page = (int) $request->query->get('page', 1);
$perPage = (int) $request->query->get('perPage', 30);
+ $since = $request->query->get('since', 0);
+ $tags = $request->query->get('tags', '');
$pager = $this->getDoctrine()
->getRepository('WallabagCoreBundle:Entry')
- ->findEntries($this->getUser()->getId(), (bool) $isArchived, (bool) $isStarred, $sort, $order);
+ ->findEntries($this->getUser()->getId(), $isArchived, $isStarred, $sort, $order, $since, $tags);
$pager->setCurrentPage($page);
$pager->setMaxPerPage($perPage);
$json = $this->get('serializer')->serialize($paginatedCollection, 'json');
- return $this->renderJsonResponse($json);
+ return (new JsonResponse())->setJson($json);
}
/**
* }
* )
*
- * @return Response
+ * @return JsonResponse
*/
public function getEntryAction(Entry $entry)
{
$json = $this->get('serializer')->serialize($entry, 'json');
- return $this->renderJsonResponse($json);
+ return (new JsonResponse())->setJson($json);
}
/**
* }
* )
*
- * @return Response
+ * @return JsonResponse
*/
public function postEntriesAction(Request $request)
{
$this->validateAuthentication();
$url = $request->request->get('url');
- $isArchived = (int) $request->request->get('archive');
- $isStarred = (int) $request->request->get('starred');
+ $title = $request->request->get('title');
+ $isArchived = $request->request->get('archive');
+ $isStarred = $request->request->get('starred');
$entry = $this->get('wallabag_core.entry_repository')->findByUrlAndUserId($url, $this->getUser()->getId());
);
}
+ if (!is_null($title)) {
+ $entry->setTitle($title);
+ }
+
$tags = $request->request->get('tags', '');
if (!empty($tags)) {
$this->get('wallabag_core.content_proxy')->assignTagsToEntry($entry, $tags);
$json = $this->get('serializer')->serialize($entry, 'json');
- return $this->renderJsonResponse($json);
+ return (new JsonResponse())->setJson($json);
}
/**
* }
* )
*
- * @return Response
+ * @return JsonResponse
*/
public function patchEntriesAction(Entry $entry, Request $request)
{
$this->validateUserAccess($entry->getUser()->getId());
$title = $request->request->get('title');
- $isArchived = (int) $request->request->get('archive');
- $isStarred = (int) $request->request->get('starred');
+ $isArchived = $request->request->get('archive');
+ $isStarred = $request->request->get('starred');
if (!is_null($title)) {
$entry->setTitle($title);
$json = $this->get('serializer')->serialize($entry, 'json');
- return $this->renderJsonResponse($json);
+ return (new JsonResponse())->setJson($json);
}
/**
* }
* )
*
- * @return Response
+ * @return JsonResponse
*/
public function deleteEntriesAction(Entry $entry)
{
$json = $this->get('serializer')->serialize($entry, 'json');
- return $this->renderJsonResponse($json);
+ return (new JsonResponse())->setJson($json);
}
/**
* }
* )
*
- * @return Response
+ * @return JsonResponse
*/
public function getEntriesTagsAction(Entry $entry)
{
$json = $this->get('serializer')->serialize($entry->getTags(), 'json');
- return $this->renderJsonResponse($json);
+ return (new JsonResponse())->setJson($json);
}
/**
* }
* )
*
- * @return Response
+ * @return JsonResponse
*/
public function postEntriesTagsAction(Request $request, Entry $entry)
{
$json = $this->get('serializer')->serialize($entry, 'json');
- return $this->renderJsonResponse($json);
+ return (new JsonResponse())->setJson($json);
}
/**
* }
* )
*
- * @return Response
+ * @return JsonResponse
*/
public function deleteEntriesTagsAction(Entry $entry, Tag $tag)
{
$json = $this->get('serializer')->serialize($entry, 'json');
- return $this->renderJsonResponse($json);
+ return (new JsonResponse())->setJson($json);
}
/**
*
* @ApiDoc()
*
- * @return Response
+ * @return JsonResponse
*/
public function getTagsAction()
{
$tags = $this->getDoctrine()
->getRepository('WallabagCoreBundle:Tag')
- ->findAllTags($this->getUser()->getId());
+ ->findAllTags($this->getUser()->getId())
+ ->getQuery()
+ ->getResult();
$json = $this->get('serializer')->serialize($tags, 'json');
- return $this->renderJsonResponse($json);
+ return (new JsonResponse())->setJson($json);
+ }
+
+ /**
+ * Permanently remove one tag from **every** entry.
+ *
+ * @ApiDoc(
+ * requirements={
+ * {"name"="tag", "dataType"="string", "required"=true, "requirement"="\w+", "description"="Tag as a string"}
+ * }
+ * )
+ *
+ * @return JsonResponse
+ */
+ public function deleteTagLabelAction(Request $request)
+ {
+ $this->validateAuthentication();
+ $label = $request->request->get('tag', '');
+
+ $tag = $this->getDoctrine()->getRepository('WallabagCoreBundle:Tag')->findOneByLabel($label);
+
+ if (empty($tag)) {
+ throw $this->createNotFoundException('Tag not found');
+ }
+
+ $this->getDoctrine()
+ ->getRepository('WallabagCoreBundle:Entry')
+ ->removeTag($this->getUser()->getId(), $tag);
+
+ $json = $this->get('serializer')->serialize($tag, 'json');
+
+ return (new JsonResponse())->setJson($json);
+ }
+
+ /**
+ * Permanently remove some tags from **every** entry.
+ *
+ * @ApiDoc(
+ * requirements={
+ * {"name"="tags", "dataType"="string", "required"=true, "format"="tag1,tag2", "description"="Tags as strings (comma splitted)"}
+ * }
+ * )
+ *
+ * @return JsonResponse
+ */
+ public function deleteTagsLabelAction(Request $request)
+ {
+ $this->validateAuthentication();
+
+ $tagsLabels = $request->request->get('tags', '');
+
+ $tags = [];
+
+ foreach (explode(',', $tagsLabels) as $tagLabel) {
+ $tagEntity = $this->getDoctrine()->getRepository('WallabagCoreBundle:Tag')->findOneByLabel($tagLabel);
+
+ if (!empty($tagEntity)) {
+ $tags[] = $tagEntity;
+ }
+ }
+
+ if (empty($tags)) {
+ throw $this->createNotFoundException('Tags not found');
+ }
+
+ $this->getDoctrine()
+ ->getRepository('WallabagCoreBundle:Entry')
+ ->removeTags($this->getUser()->getId(), $tags);
+
+ $json = $this->get('serializer')->serialize($tags, 'json');
+
+ return (new JsonResponse())->setJson($json);
}
/**
* }
* )
*
- * @return Response
+ * @return JsonResponse
*/
public function deleteTagAction(Tag $tag)
{
$json = $this->get('serializer')->serialize($tag, 'json');
- return $this->renderJsonResponse($json);
+ return (new JsonResponse())->setJson($json);
}
+
/**
* Retrieve version number.
*
* @ApiDoc()
*
- * @return Response
+ * @return JsonResponse
*/
public function getVersionAction()
{
$json = $this->get('serializer')->serialize($version, 'json');
- return $this->renderJsonResponse($json);
+ return (new JsonResponse())->setJson($json);
}
/**
throw $this->createAccessDeniedException('Access forbidden. Entry user id: '.$requestUserId.', logged user id: '.$user->getId());
}
}
-
- /**
- * Send a JSON Response.
- * We don't use the Symfony JsonRespone, because it takes an array as parameter instead of a JSON string.
- *
- * @param string $json
- *
- * @return Response
- */
- private function renderJsonResponse($json)
- {
- return new Response($json, 200, array('application/json'));
- }
}