3 namespace Wallabag\CoreBundle\Controller
;
5 use Nelmio\ApiDocBundle\Annotation\ApiDoc
;
6 use Symfony\Bundle\FrameworkBundle\Controller\Controller
;
7 use Symfony\Component\HttpFoundation\Request
;
8 use Symfony\Component\HttpFoundation\JsonResponse
;
9 use Wallabag\CoreBundle\Entity\Entry
;
10 use Wallabag\CoreBundle\Entity\Tag
;
11 use Wallabag\CoreBundle\Service\Extractor
;
13 class WallabagRestController
extends Controller
19 private function assignTagsToEntry(Entry
$entry, $tags)
21 foreach (explode(',', $tags) as $label) {
22 $label = trim($label);
25 ->getRepository('WallabagCoreBundle:Tag')
26 ->findOneByLabel($label);
28 if (is_null($tagEntity)) {
29 $tagEntity = new Tag($this->getUser());
30 $tagEntity->setLabel($label);
33 // only add the tag on the entry if the relation doesn't exist
34 if (!$entry->getTags()->contains($tagEntity)) {
35 $entry->addTag($tagEntity);
41 * Retrieve salt for a giver user.
45 * {"name"="username", "dataType"="string", "required"=true, "description"="username"}
50 public function getSaltAction(Request
$request)
54 ->getRepository('WallabagCoreBundle:User')
55 ->findOneByUsername($request->query
->get('username'));
58 throw $this->createNotFoundException();
61 return array($user->getSalt() ?: null);
64 * Retrieve all entries. It could be filtered by many options.
68 * {"name"="archive", "dataType"="boolean", "required"=false, "format"="true or false, all entries by default", "description"="filter by archived status."},
69 * {"name"="star", "dataType"="boolean", "required"=false, "format"="true or false, all entries by default", "description"="filter by starred status."},
70 * {"name"="sort", "dataType"="string", "required"=false, "format"="'created' or 'updated', default 'created'", "description"="sort entries by date."},
71 * {"name"="order", "dataType"="string", "required"=false, "format"="'asc' or 'desc', default 'desc'", "description"="order of sort."},
72 * {"name"="page", "dataType"="integer", "required"=false, "format"="default '1'", "description"="what page you want."},
73 * {"name"="perPage", "dataType"="integer", "required"=false, "format"="default'30'", "description"="results per page."},
74 * {"name"="tags", "dataType"="string", "required"=false, "format"="api%2Crest", "description"="a list of tags url encoded. Will returns entries that matches ALL tags."},
79 public function getEntriesAction(Request
$request)
81 $isArchived = $request->query
->get('archive');
82 $isStarred = $request->query
->get('star');
83 $sort = $request->query
->get('sort', 'created');
84 $order = $request->query
->get('order', 'desc');
85 $page = $request->query
->get('page', 1);
86 $perPage = $request->query
->get('perPage', 30);
87 $tags = $request->query
->get('tags', array());
91 ->getRepository('WallabagCoreBundle:Entry')
92 ->findEntries($this->getUser()->getId(), $isArchived, $isStarred, $sort, $order);
95 throw $this->createNotFoundException();
98 $json = $this->get('serializer')->serialize($entries, 'json');
100 return new JsonResponse($json, 200);
104 * Retrieve a single entry
108 * {"name"="entry", "dataType"="integer", "requirement"="\w+", "description"="The entry ID"}
113 public function getEntryAction(Entry
$entry)
115 if ($entry->getUser()->getId() != $this->getUser()->getId()) {
116 throw $this->createAccessDeniedException();
119 $json = $this->get('serializer')->serialize($entry, 'json');
121 return new JsonResponse($json, 200);
129 * {"name"="url", "dataType"="string", "required"=true, "format"="http://www.test.com/article.html", "description"="Url for the entry."},
130 * {"name"="title", "dataType"="string", "required"=false, "description"="Optional, we'll get the title from the page."},
131 * {"name"="tags", "dataType"="string", "required"=false, "format"="tag1,tag2,tag3", "description"="a comma-separated list of tags."},
136 public function postEntriesAction(Request
$request)
138 $url = $request->request
->get('url');
140 $content = Extractor
::extract($url);
141 $entry = new Entry($this->getUser());
142 $entry->setUrl($url);
143 $entry->setTitle($request->request
->get('title') ?: $content->getTitle());
144 $entry->setContent($content->getBody());
146 $tags = $request->request
->get('tags', '');
148 $this->assignTagsToEntry($entry, $tags);
151 $em = $this->getDoctrine()->getManager();
152 $em->persist($entry);
155 $json = $this->get('serializer')->serialize($entry, 'json');
157 return new JsonResponse($json, 200);
161 * Change several properties of an entry
165 * {"name"="entry", "dataType"="integer", "requirement"="\w+", "description"="The entry ID"}
168 * {"name"="title", "dataType"="string", "required"=false},
169 * {"name"="tags", "dataType"="string", "required"=false, "format"="tag1,tag2,tag3", "description"="a comma-separated list of tags."},
170 * {"name"="archive", "dataType"="boolean", "required"=false, "format"="true or false", "description"="archived the entry."},
171 * {"name"="star", "dataType"="boolean", "required"=false, "format"="true or false", "description"="starred the entry."},
176 public function patchEntriesAction(Entry
$entry, Request
$request)
178 if ($entry->getUser()->getId() != $this->getUser()->getId()) {
179 throw $this->createAccessDeniedException();
182 $title = $request->request
->get("title");
183 $isArchived = $request->request
->get("archive");
184 $isStarred = $request->request
->get("star");
186 if (!is_null($title)) {
187 $entry->setTitle($title);
190 if (!is_null($isArchived)) {
191 $entry->setArchived($isArchived);
194 if (!is_null($isStarred)) {
195 $entry->setStarred($isStarred);
198 $tags = $request->request
->get('tags', '');
200 $this->assignTagsToEntry($entry, $tags);
203 $em = $this->getDoctrine()->getManager();
206 $json = $this->get('serializer')->serialize($entry, 'json');
208 return new JsonResponse($json, 200);
212 * Delete **permanently** an entry
216 * {"name"="entry", "dataType"="integer", "requirement"="\w+", "description"="The entry ID"}
221 public function deleteEntriesAction(Entry
$entry)
223 if ($entry->getUser()->getId() != $this->getUser()->getId()) {
224 throw $this->createAccessDeniedException();
227 $em = $this->getDoctrine()->getManager();
231 $json = $this->get('serializer')->serialize($entry, 'json');
233 return new JsonResponse($json, 200);
237 * Retrieve all tags for an entry
241 * {"name"="entry", "dataType"="integer", "requirement"="\w+", "description"="The entry ID"}
245 public function getEntriesTagsAction(Entry
$entry)
247 if ($entry->getUser()->getId() != $this->getUser()->getId()) {
248 throw $this->createAccessDeniedException();
251 $json = $this->get('serializer')->serialize($entry->getTags(), 'json');
253 return new JsonResponse($json, 200);
257 * Add one or more tags to an entry
261 * {"name"="entry", "dataType"="integer", "requirement"="\w+", "description"="The entry ID"}
264 * {"name"="tags", "dataType"="string", "required"=false, "format"="tag1,tag2,tag3", "description"="a comma-separated list of tags."},
268 public function postEntriesTagsAction(Request
$request, Entry
$entry)
270 if ($entry->getUser()->getId() != $this->getUser()->getId()) {
271 throw $this->createAccessDeniedException();
274 $tags = $request->request
->get('tags', '');
276 $this->assignTagsToEntry($entry, $tags);
279 $em = $this->getDoctrine()->getManager();
280 $em->persist($entry);
283 $json = $this->get('serializer')->serialize($entry, 'json');
285 return new JsonResponse($json, 200);
289 * Permanently remove one tag for an entry
293 * {"name"="tag", "dataType"="string", "requirement"="\w+", "description"="The tag"},
294 * {"name"="entry", "dataType"="integer", "requirement"="\w+", "description"="The entry ID"}
298 public function deleteEntriesTagsAction(Entry
$entry, Tag
$tag)
300 if ($entry->getUser()->getId() != $this->getUser()->getId()) {
301 throw $this->createAccessDeniedException();
304 $entry->removeTag($tag);
305 $em = $this->getDoctrine()->getManager();
306 $em->persist($entry);
309 $json = $this->get('serializer')->serialize($entry, 'json');
311 return new JsonResponse($json, 200);
319 public function getTagsAction()
321 $json = $this->get('serializer')->serialize($this->getUser()->getTags(), 'json');
323 return new JsonResponse($json, 200);
327 * Permanently remove one tag from **every** entry
331 * {"name"="tag", "dataType"="string", "requirement"="\w+", "description"="The tag"}
335 public function deleteTagAction(Tag
$tag)
337 if ($tag->getUser()->getId() != $this->getUser()->getId()) {
338 throw $this->createAccessDeniedException();
341 $em = $this->getDoctrine()->getManager();
345 $json = $this->get('serializer')->serialize($tag, 'json');
347 return new JsonResponse($json, 200);