use Wallabag\CoreBundle\Entity\Entry;
use Wallabag\CoreBundle\Entity\Tag;
use Wallabag\AnnotationBundle\Entity\Annotation;
+use Wallabag\CoreBundle\Event\EntrySavedEvent;
+use Wallabag\CoreBundle\Event\EntryDeletedEvent;
class WallabagRestController extends FOSRestController
{
$em = $this->getDoctrine()->getManager();
$em->persist($entry);
-
$em->flush();
+ // entry saved, dispatch event about it!
+ $this->get('event_dispatcher')->dispatch(EntrySavedEvent::NAME, new EntrySavedEvent($entry));
+
$json = $this->get('serializer')->serialize($entry, 'json');
return (new JsonResponse())->setJson($json);
$this->validateAuthentication();
$this->validateUserAccess($entry->getUser()->getId());
+ // entry deleted, dispatch event about it!
+ $this->get('event_dispatcher')->dispatch(EntryDeletedEvent::NAME, new EntryDeletedEvent($entry));
+
$em = $this->getDoctrine()->getManager();
$em->remove($entry);
$em->flush();
* }
* )
*
- * @return Response
+ * @param Entry $entry
+ *
+ * @return JsonResponse
*/
public function getAnnotationsAction(Entry $entry)
{
-
$this->validateAuthentication();
- $annotationRows = $this
- ->getDoctrine()
- ->getRepository('WallabagAnnotationBundle:Annotation')
- ->findAnnotationsByPageId($entry->getId(), $this->getUser()->getId());
- $total = count($annotationRows);
- $annotations = array('total' => $total, 'rows' => $annotationRows);
-
- $json = $this->get('serializer')->serialize($annotations, 'json');
-
- return $this->renderJsonResponse($json);
+ return $this->forward('WallabagAnnotationBundle:WallabagAnnotation:getAnnotations', [
+ 'entry' => $entry,
+ ]);
}
/**
* Creates a new annotation.
*
- * @param Entry $entry
- *
* @ApiDoc(
* requirements={
* {"name"="ranges", "dataType"="array", "requirement"="\w+", "description"="The range array for the annotation"},
* }
* )
*
- * @return Response
+ * @param Request $request
+ * @param Entry $entry
+ *
+ * @return JsonResponse
*/
public function postAnnotationAction(Request $request, Entry $entry)
{
$this->validateAuthentication();
- $data = json_decode($request->getContent(), true);
-
- $em = $this->getDoctrine()->getManager();
-
- $annotation = new Annotation($this->getUser());
-
- $annotation->setText($data['text']);
- if (array_key_exists('quote', $data)) {
- $annotation->setQuote($data['quote']);
- }
- if (array_key_exists('ranges', $data)) {
- $annotation->setRanges($data['ranges']);
- }
-
- $annotation->setEntry($entry);
-
- $em->persist($annotation);
- $em->flush();
-
- $json = $this->get('serializer')->serialize($annotation, 'json');
-
- return $this->renderJsonResponse($json);
+ return $this->forward('WallabagAnnotationBundle:WallabagAnnotation:postAnnotation', [
+ 'request' => $request,
+ 'entry' => $entry,
+ ]);
}
/**
*
* @ParamConverter("annotation", class="WallabagAnnotationBundle:Annotation")
*
- * @return Response
+ * @param Annotation $annotation
+ * @param Request $request
+ *
+ * @return JsonResponse
*/
public function putAnnotationAction(Annotation $annotation, Request $request)
{
-
$this->validateAuthentication();
- $data = json_decode($request->getContent(), true);
-
- if (!is_null($data['text'])) {
- $annotation->setText($data['text']);
- }
-
- $em = $this->getDoctrine()->getManager();
- $em->flush();
-
- $json = $this->get('serializer')->serialize($annotation, 'json');
-
- return $this->renderJsonResponse($json);
+ return $this->forward('WallabagAnnotationBundle:WallabagAnnotation:putAnnotation', [
+ 'annotation' => $annotation,
+ 'request' => $request,
+ ]);
}
/**
*
* @ParamConverter("annotation", class="WallabagAnnotationBundle:Annotation")
*
- * @return Response
+ * @param Annotation $annotation
+ *
+ * @return JsonResponse
*/
public function deleteAnnotationAction(Annotation $annotation)
{
-
$this->validateAuthentication();
- $em = $this->getDoctrine()->getManager();
- $em->remove($annotation);
- $em->flush();
-
- $json = $this->get('serializer')->serialize($annotation, 'json');
-
- return $this->renderJsonResponse($json);
+ return $this->forward('WallabagAnnotationBundle:WallabagAnnotation:deleteAnnotation', [
+ 'annotation' => $annotation,
+ ]);
}
/**