src/Controller/BookMarkController.php line 75

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\BookMark;
  4. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  5. use Symfony\Component\HttpFoundation\JsonResponse;
  6. use Symfony\Component\HttpFoundation\Request;
  7. use Symfony\Component\HttpFoundation\Response;
  8. use Symfony\Component\Routing\Annotation\Route;
  9. /**
  10.  * @Route("/book_mark")
  11.  */
  12. class BookMarkController extends AbstractController
  13. {
  14.     /**
  15.      * @Route("/add", name="book_mark_add", methods={"POST"})
  16.      */
  17.     public function add(Request $request)
  18.     {
  19.         if (!$request->isXmlHttpRequest()) {
  20.             return new JsonResponse(array('status' => 'error','message' => 'Access forbidden!'), 400);
  21.         }
  22.         if(isset($request->request))
  23.         {
  24.             $user $this->getUser();
  25.             $path $request->request->get('path');
  26.             $title $request->request->get('title');
  27.             $bookMark = new BookMark();
  28.             $bookMark->setTitle($title);
  29.             $bookMark->setLink($path);
  30.             $bookMark->setUser($user);
  31.             $entityManager $this->getDoctrine()->getManager();
  32.             $entityManager->persist($bookMark);
  33.             $entityManager->flush();
  34.             $response = array('status' => 'ok');
  35.         }
  36.         return new JsonResponse($response,200);
  37.     }
  38.     /**
  39.      * @Route("/remove", name="book_mark_remove", methods={"POST"})
  40.      */
  41.     public function remove(Request $request)
  42.     {
  43.         if (!$request->isXmlHttpRequest()) {
  44.             return new JsonResponse(array('status' => 'error','message' => 'Access forbidden!'), 400);
  45.         }
  46.         $response = array('status' => 'error');
  47.         if(isset($request->request))
  48.         {
  49.             $user $this->getUser();
  50.             $path $request->request->get('path');
  51.             
  52.             $bookMark $this->getDoctrine()->getRepository(BookMark::class)->findOneBy(['user'=>$user,'link'=>$path]);
  53.             if($bookMark)
  54.             {
  55.                 $entityManager $this->getDoctrine()->getManager();
  56.                 $entityManager->remove($bookMark);
  57.                 $entityManager->flush();
  58.                 $response = array('status' => 'ok');
  59.             }
  60.             
  61.         }
  62.         return new JsonResponse($response,200);
  63.     }
  64.     /**
  65.      * @Route("/check", name="book_mark_check", methods={"GET"})
  66.      */
  67.     public function check(Request $request)
  68.     {
  69.         /*
  70.         if (!$request->isXmlHttpRequest()) {
  71.             return new JsonResponse(array('status' => 'error','message' => 'Access forbidden!'), 400);
  72.         }*/
  73.         $response = array('status' => 'error');
  74.         if(isset($request->request))
  75.         {
  76.             $user $this->getUser();
  77.             $path $request->query->get('path');
  78.             
  79.             $bookMark $this->getDoctrine()->getRepository(BookMark::class)->findOneBy(['user'=>$user,'link'=>$path]);
  80.             
  81.             $checked = !empty($bookMark);
  82.             $response = array('status' => 'ok','checked' => $checked);
  83.             
  84.             
  85.         }
  86.         return new JsonResponse($response,200);
  87.     }
  88.     /**
  89.      * @Route("/list", name="bookmarks_list", methods={"GET"})
  90.      */
  91.     public function list(): JsonResponse
  92.     {
  93.         $user $this->getUser();
  94.         if (!$user) {
  95.             return new JsonResponse(['error' => 'User not authenticated'], 401);
  96.         }
  97.         $bookmarks $this->getDoctrine()
  98.             ->getRepository(BookMark::class)
  99.             ->findBy(['user' => $user], ['id' => 'DESC'], 5);
  100.         $bookmarksArray = [];
  101.         foreach ($bookmarks as $bookmark) {
  102.             $bookmarksArray[] = [
  103.                 'id' => $bookmark->getId(),
  104.                 'title' => $bookmark->getTitle(38), // Assuming this method exists
  105.                 'link' => $bookmark->getLink(),
  106.             ];
  107.         }
  108.         return new JsonResponse($bookmarksArray);
  109.     }
  110. }