1
0
Fork 0
mirror of https://github.com/wallabag/wallabag.git synced 2025-06-27 16:36:00 +00:00
wallabag/src/Controller/AnnotationController.php

162 lines
5.2 KiB
PHP
Raw Normal View History

<?php
2024-02-19 01:30:12 +01:00
namespace Wallabag\Controller;
use Doctrine\ORM\EntityManagerInterface;
use FOS\RestBundle\Controller\AbstractFOSRestController;
2022-08-28 02:01:46 +02:00
use JMS\Serializer\SerializerInterface;
2025-03-10 23:05:06 +01:00
use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
2022-08-28 02:01:46 +02:00
use Symfony\Component\Form\FormFactoryInterface;
2016-10-05 19:16:57 +02:00
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
2022-11-23 12:44:55 +01:00
use Symfony\Component\Routing\Annotation\Route;
2024-02-19 01:30:12 +01:00
use Wallabag\Entity\Annotation;
use Wallabag\Entity\Entry;
use Wallabag\Entity\User;
use Wallabag\Form\Type\EditAnnotationType;
use Wallabag\Form\Type\NewAnnotationType;
use Wallabag\Repository\AnnotationRepository;
2023-12-25 18:37:15 +01:00
class AnnotationController extends AbstractFOSRestController
{
protected EntityManagerInterface $entityManager;
protected SerializerInterface $serializer;
protected FormFactoryInterface $formFactory;
public function __construct(EntityManagerInterface $entityManager, SerializerInterface $serializer, FormFactoryInterface $formFactory)
{
$this->entityManager = $entityManager;
$this->serializer = $serializer;
$this->formFactory = $formFactory;
}
/**
* Retrieve annotations for an entry.
*
2023-12-31 09:28:37 +01:00
* @see Api\WallabagRestController
*
2025-03-10 01:15:45 +01:00
* @Route("/annotations/{entry}.{_format}", name="annotations_get_annotations", methods={"GET"}, defaults={"_format": "json"})
2025-03-10 23:05:06 +01:00
* @IsGranted("LIST_ANNOTATIONS", subject="entry")
2022-11-23 12:44:55 +01:00
*
2016-10-05 19:16:57 +02:00
* @return JsonResponse
*/
public function getAnnotationsAction(Entry $entry, AnnotationRepository $annotationRepository)
{
$annotationRows = $annotationRepository->findByEntryIdAndUserId($entry->getId(), $this->getUser()->getId());
$total = \count($annotationRows);
2016-10-09 14:01:28 +02:00
$annotations = ['total' => $total, 'rows' => $annotationRows];
$json = $this->serializer->serialize($annotations, 'json');
2016-10-05 19:16:57 +02:00
return (new JsonResponse())->setJson($json);
}
/**
* Creates a new annotation.
*
2023-12-31 09:28:37 +01:00
* @see Api\WallabagRestController
2022-11-23 12:44:55 +01:00
*
2025-03-10 01:15:45 +01:00
* @Route("/annotations/{entry}.{_format}", name="annotations_post_annotation", methods={"POST"}, defaults={"_format": "json"})
2025-03-10 23:05:06 +01:00
* @IsGranted("CREATE_ANNOTATIONS", subject="entry")
2022-11-23 12:44:55 +01:00
*
* @return JsonResponse
*/
public function postAnnotationAction(Request $request, Entry $entry)
{
$data = json_decode($request->getContent(), true);
$annotation = new Annotation($this->getUser());
$annotation->setEntry($entry);
$form = $this->formFactory->createNamed('', NewAnnotationType::class, $annotation, [
'csrf_protection' => false,
'allow_extra_fields' => true,
]);
$form->submit($data);
if ($form->isValid()) {
$this->entityManager->persist($annotation);
$this->entityManager->flush();
$json = $this->serializer->serialize($annotation, 'json');
return JsonResponse::fromJsonString($json);
}
2025-04-05 16:19:38 +02:00
return new JsonResponse(status: 400);
}
/**
* Updates an annotation.
*
2023-12-31 09:28:37 +01:00
* @see Api\WallabagRestController
*
2025-03-10 01:15:45 +01:00
* @Route("/annotations/{annotation}.{_format}", name="annotations_put_annotation", methods={"PUT"}, defaults={"_format": "json"})
2025-03-10 23:05:06 +01:00
* @IsGranted("EDIT", subject="annotation")
*
2016-10-05 19:16:57 +02:00
* @return JsonResponse
*/
2025-03-10 23:05:06 +01:00
public function putAnnotationAction(Request $request, Annotation $annotation)
{
try {
$data = json_decode($request->getContent(), true, 512, \JSON_THROW_ON_ERROR);
$form = $this->formFactory->createNamed('', EditAnnotationType::class, $annotation, [
'csrf_protection' => false,
'allow_extra_fields' => true,
]);
$form->submit($data);
if ($form->isValid()) {
$this->entityManager->persist($annotation);
$this->entityManager->flush();
$json = $this->serializer->serialize($annotation, 'json');
return JsonResponse::fromJsonString($json);
}
2025-04-05 16:19:38 +02:00
return new JsonResponse(status: 400);
} catch (\InvalidArgumentException $e) {
throw new NotFoundHttpException($e);
}
}
/**
* Removes an annotation.
*
2023-12-31 09:28:37 +01:00
* @see Api\WallabagRestController
*
2025-03-10 01:15:45 +01:00
* @Route("/annotations/{annotation}.{_format}", name="annotations_delete_annotation", methods={"DELETE"}, defaults={"_format": "json"})
2025-03-10 23:05:06 +01:00
* @IsGranted("DELETE", subject="annotation")
*
2016-10-05 19:16:57 +02:00
* @return JsonResponse
*/
2025-03-10 23:05:06 +01:00
public function deleteAnnotationAction(Annotation $annotation)
{
try {
$this->entityManager->remove($annotation);
$this->entityManager->flush();
$json = $this->serializer->serialize($annotation, 'json');
return (new JsonResponse())->setJson($json);
} catch (\InvalidArgumentException $e) {
throw new NotFoundHttpException($e);
}
}
2024-01-01 19:51:22 +01:00
/**
* @return User|null
*/
protected function getUser()
{
$user = parent::getUser();
\assert(null === $user || $user instanceof User);
return $user;
}
}