wallabag/src/Wallabag/AnnotationBundle/Controller/WallabagAnnotationController.php

123 lines
3.4 KiB
PHP
Raw Normal View History

<?php
namespace Wallabag\AnnotationBundle\Controller;
use FOS\RestBundle\Controller\FOSRestController;
2016-10-05 17:16:57 +00:00
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
use Wallabag\AnnotationBundle\Entity\Annotation;
use Wallabag\CoreBundle\Entity\Entry;
class WallabagAnnotationController extends FOSRestController
{
/**
* Retrieve annotations for an entry.
*
2016-03-18 15:41:23 +00:00
* @param Entry $entry
*
* @see Wallabag\ApiBundle\Controller\WallabagRestController
*
2016-10-05 17:16:57 +00:00
* @return JsonResponse
*/
public function getAnnotationsAction(Entry $entry)
{
$annotationRows = $this
2016-10-05 17:16:57 +00:00
->getDoctrine()
->getRepository('WallabagAnnotationBundle:Annotation')
->findAnnotationsByPageId($entry->getId(), $this->getUser()->getId());
$total = count($annotationRows);
2016-10-09 12:01:28 +00:00
$annotations = ['total' => $total, 'rows' => $annotationRows];
$json = $this->get('serializer')->serialize($annotations, 'json');
2016-10-05 17:16:57 +00:00
return (new JsonResponse())->setJson($json);
}
/**
* Creates a new annotation.
*
2016-10-05 17:16:57 +00:00
* @param Request $request
2016-10-06 09:40:14 +00:00
* @param Entry $entry
*
2016-10-05 17:16:57 +00:00
* @return JsonResponse
*
2016-10-06 09:40:14 +00:00
* @see Wallabag\ApiBundle\Controller\WallabagRestController
*/
public function postAnnotationAction(Request $request, Entry $entry)
{
$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');
2016-10-05 17:16:57 +00:00
return (new JsonResponse())->setJson($json);
}
/**
* Updates an annotation.
*
2016-03-18 15:41:23 +00:00
* @see Wallabag\ApiBundle\Controller\WallabagRestController
*
* @ParamConverter("annotation", class="WallabagAnnotationBundle:Annotation")
*
2016-10-05 17:16:57 +00:00
* @param Annotation $annotation
2016-10-06 09:40:14 +00:00
* @param Request $request
*
2016-10-05 17:16:57 +00:00
* @return JsonResponse
*/
public function putAnnotationAction(Annotation $annotation, Request $request)
{
$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');
2016-10-05 17:16:57 +00:00
return (new JsonResponse())->setJson($json);
}
/**
* Removes an annotation.
*
2016-03-18 15:41:23 +00:00
* @see Wallabag\ApiBundle\Controller\WallabagRestController
*
* @ParamConverter("annotation", class="WallabagAnnotationBundle:Annotation")
*
2016-10-05 17:16:57 +00:00
* @param Annotation $annotation
2016-10-06 09:40:14 +00:00
*
2016-10-05 17:16:57 +00:00
* @return JsonResponse
*/
public function deleteAnnotationAction(Annotation $annotation)
{
$em = $this->getDoctrine()->getManager();
$em->remove($annotation);
$em->flush();
$json = $this->get('serializer')->serialize($annotation, 'json');
2016-10-05 17:16:57 +00:00
return (new JsonResponse())->setJson($json);
}
}